在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):avast/gradle-docker-compose-plugin开源软件地址(OpenSource Url):https://github.com/avast/gradle-docker-compose-plugin开源编程语言(OpenSource Language):Groovy 100.0%开源软件介绍(OpenSource Introduction):gradle-docker-compose-pluginSimplifies usage of Docker Compose for local development and integration testing in Gradle environment.
Quick startbuildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "com.avast.gradle:gradle-docker-compose-plugin:$versionHere"
}
}
apply plugin: 'docker-compose'
// Or use the new Gradle Portal plugins (then you don't have to add the dependency as above):
// plugins {
// id 'com.avast.gradle.docker-compose' version "$versionHere"
// }
dockerCompose.isRequiredBy(test)
Why to use Docker Compose?
Why this plugin?You could easily ensure that
UsageThe plugin must be applied on project that contains
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "com.avast.gradle:gradle-docker-compose-plugin:$versionHere"
}
}
apply plugin: 'docker-compose'
dockerCompose.isRequiredBy(test) // hooks 'dependsOn composeUp' and 'finalizedBy composeDown', and exposes environment variables and system properties (if possible)
dockerCompose {
useComposeFiles = ['docker-compose.yml', 'docker-compose.prod.yml'] // like 'docker-compose -f <file>'; default is empty
startedServices = ['web'] // list of services to execute when calling 'docker-compose up' or 'docker-compose pull' (when not specified, all services are executed)
scale = [${serviceName1}: 5, ${serviceName2}: 2] // Pass docker compose --scale option like 'docker-compose up --scale serviceName1=5 --scale serviceName2=2'
forceRecreate = false // pass '--force-recreate' and '--renew-anon-volumes' when calling 'docker-compose up' when set to 'true`
noRecreate = false // pass '--no-recreate' when calling 'docker-compose up' when set to 'true`
buildBeforeUp = true // performs 'docker-compose build' before calling the 'up' command; default is true
buildBeforePull = true // performs 'docker-compose build' before calling the 'pull' command; default is true
ignorePullFailure = false // when set to true, pass '--ignore-pull-failure' to 'docker-compose pull'
ignorePushFailure = false // when set to true, pass '--ignore-push-failure' to 'docker-compose push'
pushServices = [] // which services should be pushed, if not defined then upon `composePush` task all defined services in compose file will be pushed (default behaviour)
buildAdditionalArgs = ['--force-rm']
pullAdditionalArgs = ['--ignore-pull-failures']
upAdditionalArgs = ['--no-deps']
downAdditionalArgs = ['--some-switch']
composeAdditionalArgs = ['--context', 'remote', '--verbose', "--log-level", "DEBUG"] // for adding more [options] in docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
waitForTcpPorts = true // turns on/off the waiting for exposed TCP ports opening; default is true
waitForTcpPortsTimeout = java.time.Duration.ofMinutes(15) // how long to wait until all exposed TCP become open; default is 15 minutes
waitAfterTcpProbeFailure = java.time.Duration.ofSeconds(1) // how long to sleep before next attempt to check if a TCP is open; default is 1 second
tcpPortsToIgnoreWhenWaiting = [1234] // list of TCP ports what will be ignored when waiting for exposed TCP ports opening; default: empty list
waitForHealthyStateTimeout = java.time.Duration.ofMinutes(15) // how long to wait until a container becomes healthy; default is 15 minutes
waitAfterHealthyStateProbeFailure = java.time.Duration.ofSeconds(5) // how long to sleep before next attempt to check healthy status; default is 5 seconds
checkContainersRunning = true // turns on/off checking if container is running or restarting (during waiting for open TCP port and healthy state); default is true
captureContainersOutput = false // if true, prints output of all containers to Gradle output - very useful for debugging; default is false
captureContainersOutputToFile = project.file('/path/to/logFile') // sends output of all containers to a log file
captureContainersOutputToFiles = project.file('/path/to/directory') // sends output of all services to a dedicated log file in the directory specified, e.g. 'web.log' for service named 'log'
composeLogToFile = project.file('build/my-logs.txt') // redirect output of composeUp and composeDown tasks to this file; default is null (ouput is not redirected)
containerLogToDir = project.file('build/logs') // directory where composeLogs task stores output of the containers; default: build/containers-logs
includeDependencies = false // calculates services dependencies of startedServices and includes those when gathering logs or removing containers; default is false
stopContainers = true // doesn't call `docker-compose down` if set to false - see below the paragraph about reconnecting; default is true
removeContainers = true // default is true
retainContainersOnStartupFailure = false // if set to true, skips running ComposeDownForced task when ComposeUp fails - useful for troubleshooting; default is false
removeImages = com.avast.gradle.dockercompose.RemoveImages.None // Other accepted values are All and Local
removeVolumes = true // default is true
removeOrphans = false // removes containers for services not defined in the Compose file; default is false
projectName = 'my-project' // allow to set custom docker-compose project name (defaults to a stable name derived from absolute path of the project and nested settings name), set to null to Docker Compose default (directory name)
projectNamePrefix = 'my_prefix_' // allow to set custom prefix of docker-compose project name, the final project name has nested configuration name appended
executable = '/path/to/docker-compose' // allow to set the path of the docker-compose executable (useful if not present in PATH)
dockerExecutable = '/path/to/docker' // allow to set the path of the docker executable (useful if not present in PATH)
dockerComposeWorkingDirectory = project.file('/path/where/docker-compose/is/invoked/from')
dockerComposeStopTimeout = java.time.Duration.ofSeconds(20) // time before docker-compose sends SIGTERM to the running containers after the composeDown task has been started
environment.put 'BACKEND_ADDRESS', '192.168.1.100' // environment variables to be used when calling 'docker-compose', e.g. for substitution in compose file
}
test.doFirst {
// exposes "${serviceName}_HOST" and "${serviceName}_TCP_${exposedPort}" environment variables
// for example exposes "WEB_HOST" and "WEB_TCP_80" environment variables for service named `web` with exposed port `80`
// if service is scaled using scale option, environment variables will be exposed for each service instance like "WEB_1_HOST", "WEB_1_TCP_80", "WEB_2_HOST", "WEB_2_TCP_80" and so on
dockerCompose.exposeAsEnvironment(test)
// exposes "${serviceName}.host" and "${serviceName}.tcp.${exposedPort}" system properties
// for example exposes "web.host" and "web.tcp.80" system properties for service named `web` with exposed port `80`
// if service is scaled using scale option, environment variables will be exposed for each service instance like "web_1.host", "web_1.tcp.80", "web_2.host", "web_2.tcp.80" and so on
dockerCompose.exposeAsSystemProperties(test)
// get information about container of service `web` (declared in docker-compose.yml)
def webInfo = dockerCompose.servicesInfos.web.firstContainer
// in case scale option is used, dockerCompose.servicesInfos.containerInfos will contain information about all running containers of service. Particular container can be retrieved either by iterating the values of containerInfos map (key is service instance name, for example 'web_1')
def webInfo = dockerCompose.servicesInfos.web.'web_1'
// pass host and exposed TCP port 80 as custom-named Java System properties
systemProperty 'myweb.host', webInfo.host
systemProperty 'myweb.port', webInfo.ports[80]
// it's possible to read information about exposed UDP ports using webInfo.updPorts[1234]
} Nested configurationsIt is possible to create a new set of GroovydockerCompose {
// settings as usual
myNested {
useComposeFiles = ['docker-compose-for-integration-tests.yml']
isRequiredBy(project.tasks.myTask)
}
}
When exposing service info from test.doFirst {
dockerCompose.myNested.exposeAsEnvironment(test)
} Kotlintest.doFirst {
dockerCompose.nested("myNested").exposeAsEnvironment(project.tasks.named("test").get())
} It's also possible to use this simplified syntax: dockerCompose {
isRequiredByMyTask 'docker-compose-for-integration-tests.yml'
} ReconnectingIf you specify It's very handy in scenarios when you iterate quickly and e.g. don't want to wait for Postgres to start again and again. Because you don't want to check-in this change to your VCS, you can take advantage of this init.gradle initialization script (in short, copy this file to your Usage from Kotlin DSLThis plugin can be used also from Kotlin DSL, see the example: import com.avast.gradle.dockercompose.ComposeExtension
apply(plugin = "docker-compose")
configure<ComposeExtension> {
includeDependencies.set(true)
createNested("local").apply {
projectName.set("foo")
environment.putAll(mapOf("TAGS" to "feature-test,local"))
startedServices.set(listOf("foo-api", "foo-integration"))
upAdditionalArgs.set(listOf("--no-deps"))
}
} Tips
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论