Skip to content

Commit

Permalink
Merge pull request #51 from double16/composepull
Browse files Browse the repository at this point in the history
Add ComposePull task Closes #30 #40
  • Loading branch information
augi authored Dec 19, 2016
2 parents c818215 + 319680f commit 1c2abb3
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Simplifies usage of [Docker Compose](https://www.docker.com/docker-compose) for

`composeDown` task stops the application and removes the containers.

`composePull` task pulls and optionally builds the images required by the application. This is useful, for example, with a CI platform that caches docker images to decrease build times.

## Why to use Docker Compose?
1. I want to be able to run my application on my computer, and it must work for my colleagues as well. Just execute `docker-compose up` and I'm done.
2. I want to be able to test my application on my computer - I don't wanna wait till my application is deployed into dev/testing environment and acceptance/end2end tests get executed. I want to execute these tests on my computer - it means execute `docker-compose up` before these tests.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.avast.gradle.dockercompose

import com.avast.gradle.dockercompose.tasks.ComposeDown
import com.avast.gradle.dockercompose.tasks.ComposePull
import com.avast.gradle.dockercompose.tasks.ComposeUp
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand All @@ -9,10 +10,12 @@ class DockerComposePlugin implements Plugin<Project> {
@Override
void apply(Project project) {
ComposeUp upTask = project.tasks.create('composeUp', ComposeUp)
ComposePull pullTask = project.tasks.create('composePull', ComposePull)
ComposeDown downTask = project.tasks.create('composeDown', ComposeDown)
ComposeExtension extension = project.extensions.create('dockerCompose', ComposeExtension, project, upTask, downTask)
upTask.extension = extension
upTask.downTask = downTask
downTask.extension = extension
pullTask.extension = extension
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.avast.gradle.dockercompose.tasks

import com.avast.gradle.dockercompose.ComposeExtension
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import org.gradle.process.ExecSpec

class ComposePull extends DefaultTask {

ComposeExtension extension

ComposePull() {
group = 'docker'
description = 'Pulls and builds all images of docker-compose project'
}

@TaskAction
void pull() {
if (extension.buildBeforeUp) {
project.exec { ExecSpec e ->
e.environment = extension.environment
e.commandLine extension.composeCommand('build')
}
}
project.exec { ExecSpec e ->
e.environment = extension.environment
e.commandLine extension.composeCommand('pull')
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.avast.gradle.dockercompose

import com.avast.gradle.dockercompose.tasks.ComposeDown
import com.avast.gradle.dockercompose.tasks.ComposeUp
import com.avast.gradle.dockercompose.tasks.ComposePull
import org.gradle.api.Task
import org.gradle.api.internal.file.TmpDirTemporaryFileProvider
import org.gradle.api.tasks.testing.Test
Expand All @@ -17,6 +18,7 @@ class DockerComposePluginTest extends Specification {
then:
project.tasks.composeUp instanceof ComposeUp
project.tasks.composeDown instanceof ComposeDown
project.tasks.composePull instanceof ComposePull
project.extensions.findByName('dockerCompose') instanceof ComposeExtension
}

Expand Down Expand Up @@ -74,6 +76,29 @@ class DockerComposePluginTest extends Specification {
}
}

def "allows pull from integration test"() {
def projectDir = new TmpDirTemporaryFileProvider().createTemporaryDirectory("gradle", "projectDir")
new File(projectDir, 'docker-compose.yml') << '''
web:
image: nginx
command: bash -c "sleep 5 && nginx -g 'daemon off;'"
ports:
- 80
'''
def project = ProjectBuilder.builder().withProjectDir(projectDir).build()
project.plugins.apply 'docker-compose'
when:
project.tasks.composePull.pull()
then:
noExceptionThrown()
cleanup:
try {
projectDir.delete()
} catch(ignored) {
projectDir.deleteOnExit()
}
}

def "can specify compose files to use"() {
def projectDir = new TmpDirTemporaryFileProvider().createTemporaryDirectory("gradle", "projectDir")
new File(projectDir, 'original.yml') << '''
Expand Down

0 comments on commit 1c2abb3

Please sign in to comment.