add plugin service container
This commit is contained in:
25
README.md
25
README.md
@@ -51,6 +51,31 @@ id `ltd.hlaeja.plugin.hlaeja-common-plugin.service`
|
|||||||
|
|
||||||
Default setting and tasks for services.
|
Default setting and tasks for services.
|
||||||
|
|
||||||
|
### Plugin Service Container
|
||||||
|
|
||||||
|
id `ltd.hlaeja.plugin.hlaeja-common-plugin.service-container`
|
||||||
|
|
||||||
|
Configuration for running project in docker locally during development.
|
||||||
|
|
||||||
|
#### Configuration
|
||||||
|
|
||||||
|
* properties `container.network`, environment `CONTAINER_NETWORK`, or default `develop`
|
||||||
|
* properties `container.port.expose`, environment `CONTAINER_PORT_EXPOSE`, or default `8080`
|
||||||
|
* properties `container.port.host`, environment `CONTAINER_PORT_HOST`, or default `8080`
|
||||||
|
* properties `container.profiles`, environment `CONTAINER_PROFILES`, or default `docker`
|
||||||
|
* properties `docker.port.expose`, environment `DOCKER_PORT_EXPOSE`, or default `8080`
|
||||||
|
|
||||||
|
container and docker ports can be a single port (e.g., 8080) or multiple ports separated by commas (e.g., 8080,8443)
|
||||||
|
|
||||||
|
#### Gradle Tasks
|
||||||
|
|
||||||
|
* `containerCreate` create docker container with network and spring boot profile.
|
||||||
|
* `containerStart` starts docker container.
|
||||||
|
* `containerStop` stops docker container.
|
||||||
|
* `containerNetworkCheck` check if network exist.
|
||||||
|
* `containerNetworkCreate` creates network.
|
||||||
|
* `containerNetworkRemove` removes network.
|
||||||
|
|
||||||
## Releasing plugin
|
## Releasing plugin
|
||||||
|
|
||||||
Run `release.sh` script from `master` branch.
|
Run `release.sh` script from `master` branch.
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
implementation(hlaeja.com.bmuschko.docker.gradle.plugin)
|
||||||
implementation(hlaeja.io.gitlab.arturbosch.detekt.gradle.plugin)
|
implementation(hlaeja.io.gitlab.arturbosch.detekt.gradle.plugin)
|
||||||
implementation(hlaeja.ltd.hlaeja.plugin.core)
|
implementation(hlaeja.ltd.hlaeja.plugin.core)
|
||||||
implementation(hlaeja.org.jetbrains.kotlin.gradle.plugin)
|
implementation(hlaeja.org.jetbrains.kotlin.gradle.plugin)
|
||||||
|
|||||||
@@ -0,0 +1,69 @@
|
|||||||
|
import com.bmuschko.gradle.docker.tasks.container.DockerCreateContainer
|
||||||
|
import com.bmuschko.gradle.docker.tasks.container.DockerStartContainer
|
||||||
|
import com.bmuschko.gradle.docker.tasks.container.DockerStopContainer
|
||||||
|
import com.bmuschko.gradle.docker.tasks.network.DockerCreateNetwork
|
||||||
|
import com.bmuschko.gradle.docker.tasks.network.DockerInspectNetwork
|
||||||
|
import com.bmuschko.gradle.docker.tasks.network.DockerRemoveNetwork
|
||||||
|
import java.lang.System.getenv
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
id("com.bmuschko.docker-spring-boot-application")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun configuration(
|
||||||
|
property: String,
|
||||||
|
environment: String,
|
||||||
|
default: String,
|
||||||
|
): String = project.findProperty(property)?.toString() ?: getenv(environment) ?: default
|
||||||
|
|
||||||
|
fun configurationPorts(
|
||||||
|
property: String,
|
||||||
|
environment: String,
|
||||||
|
): List<String> = configuration(property, environment, "8080").split(',')
|
||||||
|
|
||||||
|
fun exposeDockerPorts(): List<Int> = configurationPorts("docker.port.expose", "DOCKER_PORT_EXPOSE")
|
||||||
|
.mapNotNull { it.toIntOrNull() }
|
||||||
|
|
||||||
|
fun exposeContainerPorts(): List<String> = configurationPorts("container.port.host", "CONTAINER_PORT_HOST")
|
||||||
|
.zip(configurationPorts("container.port.expose", "CONTAINER_PORT_EXPOSE"))
|
||||||
|
.map { (containerPort, exposedPort) -> "$containerPort:$exposedPort" }
|
||||||
|
|
||||||
|
docker.springBootApplication {
|
||||||
|
baseImage.set("eclipse-temurin:17-jre-alpine")
|
||||||
|
ports.set(exposeDockerPorts())
|
||||||
|
images.set(listOf("${project.name}:${project.version}"))
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks {
|
||||||
|
register("containerCreate", DockerCreateContainer::class) {
|
||||||
|
group = "hlaeja"
|
||||||
|
targetImageId("${project.name}:${project.version}")
|
||||||
|
containerName.set(project.name)
|
||||||
|
hostConfig.autoRemove.set(true)
|
||||||
|
hostConfig.network.set(configuration("container.network", "CONTAINER_NETWORK", "develop"))
|
||||||
|
hostConfig.portBindings.set(exposeContainerPorts())
|
||||||
|
withEnvVar("SPRING_PROFILES_ACTIVE", configuration("container.profiles", "CONTAINER_PROFILES", "docker"))
|
||||||
|
}
|
||||||
|
register("containerStart", DockerStartContainer::class) {
|
||||||
|
group = "hlaeja"
|
||||||
|
dependsOn(findByPath("containerCreate"))
|
||||||
|
targetContainerId(project.name)
|
||||||
|
}
|
||||||
|
register("containerStop", DockerStopContainer::class) {
|
||||||
|
group = "hlaeja"
|
||||||
|
targetContainerId(project.name)
|
||||||
|
}
|
||||||
|
register("containerNetworkCheck", DockerInspectNetwork::class) {
|
||||||
|
group = "hlaeja"
|
||||||
|
targetNetworkId(configuration("container.network", "CONTAINER_NETWORK", "develop"))
|
||||||
|
onError { println("Network does not exist.") }
|
||||||
|
}
|
||||||
|
register("containerNetworkCreate", DockerCreateNetwork::class) {
|
||||||
|
group = "hlaeja"
|
||||||
|
networkName.set(configuration("container.network", "CONTAINER_NETWORK", "develop"))
|
||||||
|
}
|
||||||
|
register("containerNetworkRemove", DockerRemoveNetwork::class) {
|
||||||
|
group = "hlaeja"
|
||||||
|
targetNetworkId(configuration("container.network", "CONTAINER_NETWORK", "develop"))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id("ltd.hlaeja.plugin.hlaeja-common-plugin.common")
|
id("ltd.hlaeja.plugin.hlaeja-common-plugin.common")
|
||||||
|
id("ltd.hlaeja.plugin.hlaeja-common-plugin.service-container")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user