Skip to content

Commit

Permalink
Add useDockerComposeV2 property
Browse files Browse the repository at this point in the history
  • Loading branch information
joecotton-wk authored and augi committed Aug 8, 2023
1 parent 5c67f60 commit 38a1e72
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ dockerCompose {
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)
useDockerComposeV2 = true // Use Docker Compose V2 instead of Docker Compose V1. All invocations will be done using `docker compose` instead of `docker-compose`.
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ abstract class ComposeExecutor implements BuildService<Parameters>, AutoCloseabl
abstract DirectoryProperty getDockerComposeWorkingDirectory()
abstract MapProperty<String, Object> getEnvironment()
abstract Property<String> getExecutable()
abstract Property<String> getDockerExecutable()
abstract Property<Boolean> getUseDockerComposeV2()
abstract Property<String> getProjectName()
abstract ListProperty<String> getComposeAdditionalArgs()
abstract Property<Boolean> getRemoveOrphans()
Expand All @@ -48,6 +50,8 @@ abstract class ComposeExecutor implements BuildService<Parameters>, AutoCloseabl
it.parameters.dockerComposeWorkingDirectory.set(settings.dockerComposeWorkingDirectory)
it.parameters.environment.set(settings.environment)
it.parameters.executable.set(settings.executable)
it.parameters.dockerExecutable.set(settings.dockerExecutable)
it.parameters.useDockerComposeV2.set(settings.useDockerComposeV2)
it.parameters.projectName.set(settings.projectName)
it.parameters.composeAdditionalArgs.set(settings.composeAdditionalArgs)
it.parameters.removeOrphans.set(settings.removeOrphans)
Expand Down Expand Up @@ -79,7 +83,9 @@ abstract class ComposeExecutor implements BuildService<Parameters>, AutoCloseabl
e.setWorkingDir(parameters.projectDirectory)
}
e.environment = System.getenv() + parameters.environment.get()
def finalArgs = [parameters.executable.get()]

def finalArgs = []
finalArgs.addAll(getDockerComposeBinaryArgs())
finalArgs.addAll(parameters.composeAdditionalArgs.get())
if (noAnsi) {
if (version >= VersionNumber.parse('1.28.0')) {
Expand Down Expand Up @@ -275,4 +281,11 @@ abstract class ComposeExecutor implements BuildService<Parameters>, AutoCloseabl
}
!parameters.scale.get().isEmpty()
}

// Determines whether to use docker-compose (V1) or docker compose (V2)
List<String> getDockerComposeBinaryArgs() {
parameters.useDockerComposeV2.get()
? [parameters.dockerExecutable.get(), "compose"]
: [parameters.executable.get()]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ abstract class ComposeSettings {

abstract Property<String> getExecutable()
abstract Property<String> getDockerExecutable()
abstract Property<Boolean> getUseDockerComposeV2()
abstract MapProperty<String, Object> getEnvironment()

abstract DirectoryProperty getDockerComposeWorkingDirectory()
Expand Down Expand Up @@ -147,6 +148,7 @@ abstract class ComposeSettings {

executable.set('docker-compose')
dockerExecutable.set('docker')
useDockerComposeV2.set(false)

dockerComposeStopTimeout.set(Duration.ofSeconds(10))

Expand Down Expand Up @@ -203,6 +205,7 @@ abstract class ComposeSettings {

r.executable.set(this.executable.get())
r.dockerExecutable.set(this.dockerExecutable.get())
r.useDockerComposeV2.set(this.useDockerComposeV2.get())
r.environment.set(new HashMap<String, Object>(this.environment.get()))

r.dockerComposeWorkingDirectory.set(this.dockerComposeWorkingDirectory.getOrNull())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,32 @@ class ComposeExecutorTest extends Specification {
true | ["webMaster", "web0", "web1"]
false | ["webMaster"]
}

@Unroll
def "getDockerComposeBinaryArgs returns correct values when useDockerComposeV2 is #useDockerComposeV2" () {
def f = Fixture.withHelloWorld()
f.project.plugins.apply 'java'

if(useDockerComposeV2 != null) {
f.project.dockerCompose.useDockerComposeV2 = useDockerComposeV2
}

f.project.plugins.apply 'docker-compose'

when:
def actual = ComposeExecutor.getInstance(f.project, f.project.dockerCompose).get().getDockerComposeBinaryArgs()

then:
expectedDockerComposeBinaryArgs.size() == actual.size()
actual.containsAll(expectedDockerComposeBinaryArgs)

cleanup:
f.close()

where:
useDockerComposeV2 | expectedDockerComposeBinaryArgs
true | ["docker", "compose"]
false | ["docker-compose"]
null | ["docker-compose"]
}
}

0 comments on commit 38a1e72

Please sign in to comment.