From e3643012c6a1f965fcfbb44fb974ac4ad8db06f4 Mon Sep 17 00:00:00 2001 From: Eugene Date: Mon, 14 Sep 2026 22:35:39 +0300 Subject: [PATCH] add backend publication ext --- apps/giga-wear/backend/build.gradle.kts | 42 +++---------------- .../ru/shadowsparky/BackendPublicationExt.kt | 38 +++++++++++++++++ 2 files changed, 43 insertions(+), 37 deletions(-) create mode 100644 build-logic/convention/src/main/kotlin/ru/shadowsparky/BackendPublicationExt.kt diff --git a/apps/giga-wear/backend/build.gradle.kts b/apps/giga-wear/backend/build.gradle.kts index 14c121f..cdb6bc7 100644 --- a/apps/giga-wear/backend/build.gradle.kts +++ b/apps/giga-wear/backend/build.gradle.kts @@ -1,3 +1,5 @@ +import ru.shadowsparky.setupBackendPublication + plugins { alias(libs.plugins.convention.backend.app) alias(libs.plugins.ktor) @@ -60,43 +62,9 @@ application { } ktor { - fatJar { - archiveFileName.set("server-fat.jar") + docker { + localImageName = "giga-wear-backend" } } -tasks { - named("build") { - dependsOn("buildFatJar") - } - register("deployRemote") { - dependsOn("buildFatJar") - commandLine( - getCommandLine( - "docker context use remote", - "docker build . -t gigawear-server", - "docker context use default" - ) - ) - notCompatibleWithConfigurationCache("This task uses Exec which is not compatible with configuration cache") - } - register("deployLocal") { - dependsOn("buildFatJar") - commandLine( - getCommandLine( - "docker compose down", - "docker compose up -d --build --force-recreate" - ) - ) - notCompatibleWithConfigurationCache("This task uses Exec which is not compatible with configuration cache") - } -} - -private fun getCommandLine(vararg commands: String): List { - val osName = System.getProperty("os.name").lowercase() - return if (osName.contains("win")) { - listOf("cmd", "/c") + commands.joinToString(" && ") - } else { - listOf("sh", "-c") + commands.joinToString(" && ") - } -} +setupBackendPublication() diff --git a/build-logic/convention/src/main/kotlin/ru/shadowsparky/BackendPublicationExt.kt b/build-logic/convention/src/main/kotlin/ru/shadowsparky/BackendPublicationExt.kt new file mode 100644 index 0000000..cb06ebf --- /dev/null +++ b/build-logic/convention/src/main/kotlin/ru/shadowsparky/BackendPublicationExt.kt @@ -0,0 +1,38 @@ +package ru.shadowsparky + +import org.gradle.api.Project +import org.gradle.api.tasks.Exec +import org.gradle.kotlin.dsl.invoke + +fun Project.setupBackendPublication() { + val switchToRemoteContext = tasks.register("switchToRemoteContext", Exec::class.java) { + group = "publishing" + description = "Переключает Docker-контекст на remote" + commandLine("docker", "context", "use", "remote") + } + val switchToDefaultContext = tasks.register("switchToDefaultContext",Exec::class.java) { + group = "publishing" + description = "Возвращает Docker-контекст на default" + commandLine("docker", "context", "use", "default") + } + tasks.register("publishImageRemote") { + group = "publishing" + description = "Публикует Docker image через remote context" + dependsOn( + switchToRemoteContext, + "publishImageToLocalRegistry" + ) + finalizedBy(switchToDefaultContext) + } + tasks.register("publishImageLocal") { + group = "publishing" + description = "Публикует Docker image" + dependsOn("publishImageToLocalRegistry") + } + tasks.named("publishImageToLocalRegistry") { + mustRunAfter(switchToRemoteContext) + } + switchToDefaultContext { + mustRunAfter("publishImageToLocalRegistry") + } +}