Skip to content

Commit

Permalink
Version 0.4: New property to remove local images after push to registry
Browse files Browse the repository at this point in the history
  • Loading branch information
sglahn committed Apr 29, 2017
1 parent cf9ba1d commit 99f1881
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 30 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Gradle Dockerfile Plugin
Gradle plugin to build and push Docker images using an external Dockerfile and without the need of inline configuration.
It is available through [Maven Central](http://mvnrepository.com/artifact/org.sglahn/gradle-dockerfile-plugin) and the [Gradle Plugin Portal](https://plugins.gradle.org).
Gradle plugin to build and push Docker images using an external Dockerfile and without the need of inline configuration. This means you can use a normal Dockerfile and put it in your project.
The plugin is available through [Maven Central](https://search.maven.org/#search%7Cga%7C1%7Cgradle-dockerfile-plugin) and the [Gradle Plugin Portal](https://plugins.gradle.org).

[![CircleCI](https://circleci.com/gh/sglahn/gradle-dockerfile-plugin/tree/master.svg?style=svg)](https://circleci.com/gh/sglahn/gradle-dockerfile-plugin/tree/master)

Expand All @@ -9,14 +9,14 @@ To use the plugin add a build script dependency to your Gradle build file:
```gradle
buildscript {
repositories { mavenCentral() }
dependencies { classpath('org.sglahn:gradle-dockerfile-plugin:0.3') }
dependencies { classpath('org.sglahn:gradle-dockerfile-plugin:0.4') }
}
apply plugin: 'dockerfile'
```
or via the new plugin mechanism introduced in Gradle 2.1:
```
plugins {
id "org.sglahn.gradle-dockerfile-plugin" version "0.3"
id "org.sglahn.gradle-dockerfile-plugin" version "0.4"
}
```
The plugin will add the following tasks to your project:
Expand All @@ -38,7 +38,10 @@ For more information see `Configuration` section.
### The dockerPush task
The `dockerPush` task will push the Docker image to a Docker repository.
If authentication is required use [docker login](https://docs.docker.com/engine/reference/commandline/login/) to
add the credential to your `$HOME/.docker/config.json` file.
add the credential to your `$HOME/.docker/config.json` file. [This](https://hub.docker.com/r/sglahn/gradle-dockerfile-plugin-example-project/)
is how it looks like when the example project is pushed to DockerHub. When the property "removeImagesAfterPush" is set to `true`,
the image will be removed from the local registry after the push to the remote registry. This is useful e.g. for builds
on CI agents.
### Configuration
The following configuration can be added to your Gradle build file:
```gradle
Expand Down Expand Up @@ -67,5 +70,7 @@ docker {
pull = true
// Suppress the build output and print image ID on success. Optional, default = true
quiet = false
// Remove image on local registry after push to remote registry, useful for builds on CI agents. Optional, default = false
removeImagesAfterPush = true
}
```
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apply plugin: 'groovy'

group = 'org.sglahn'
version = "0.3"
version = "0.4"

buildscript {
repositories {
Expand Down Expand Up @@ -35,12 +35,12 @@ repositories {
}

task wrapper(type: Wrapper) {
gradleVersion = '3.4.1'
gradleVersion = '3.5.0'
}

dependencies {
compile gradleApi()
compile 'org.codehaus.groovy:groovy-all:2.4.7'
compile 'org.codehaus.groovy:groovy-all:2.4.10'

testCompile gradleTestKit()
testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
Expand Down
8 changes: 5 additions & 3 deletions example-project/build.gradle
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
buildscript {
repositories { mavenCentral() }
dependencies { classpath('org.sglahn:gradle-dockerfile-plugin:0.3') }
dependencies { classpath('org.sglahn:gradle-dockerfile-plugin:0.4') }
}
apply plugin: 'dockerfile'

// or via plugin mechanism:
//plugins {
// id "org.sglahn.gradle-dockerfile-plugin" version "0.3"
// id "org.sglahn.gradle-dockerfile-plugin" version "0.4"
//}

task wrapper(type: Wrapper) {
gradleVersion = '3.4.1'
gradleVersion = '3.5'
}

apply plugin: 'java'
Expand Down Expand Up @@ -52,6 +52,8 @@ docker {
pull = true
// Suppress the build output and print image ID on success. Optional, default = true
quiet = false
// Remove image on local registry after push to remote registry, useful for builds on CI agents. Optional, default = false
removeImagesAfterPush = true
}
dockerBuild.dependsOn(build)
dockerPush.dependsOn(dockerBuild)
Expand Down
Binary file modified example-project/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions example-project/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Apr 03 13:17:51 CEST 2017
#Fri Apr 28 20:06:22 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-bin.zip
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sat Apr 01 20:42:14 CEST 2017
#Fri Apr 28 20:04:03 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-bin.zip
22 changes: 7 additions & 15 deletions gradlew
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
#!/usr/bin/env bash

##############################################################################
##
Expand Down Expand Up @@ -154,19 +154,11 @@ if $cygwin ; then
esac
fi

# Escape application args
save ( ) {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
function splitJvmOpts() {
JVM_OPTS=("$@")
}
APP_ARGS=$(save "$@")
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"

# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"

# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
cd "$(dirname "$0")"
fi

exec "$JAVACMD" "$@"
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
6 changes: 6 additions & 0 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ goto fail
@rem Get command-line arguments, handling Windows variants

if not "%OS%" == "Windows_NT" goto win9xME_args
if "%@eval[2+2]" == "4" goto 4NT_args

:win9xME_args
@rem Slurp the command line arguments.
Expand All @@ -59,6 +60,11 @@ set _SKIP=2
if "x%~1" == "x" goto execute

set CMD_LINE_ARGS=%*
goto execute

:4NT_args
@rem Get arguments from the 4NT Shell from JP Software
set CMD_LINE_ARGS=%$

:execute
@rem Setup the command line
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ class DockerFilePluginExtension {
boolean noCache = false
boolean pull = false
boolean quiet = true
boolean removeImagesAfterPush = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class DockerPushTask extends DefaultTask {
def action() {
project.getExtensions().docker.tags.each {
DockerHelper.dockerPush(project, it)
if (project.docker.removeImagesAfterPush) {
DockerHelper.removeDockerImage(project, it)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ class DockerHelper {
executeCmd(project, cmd)
}

static void removeDockerImage(project, tag) {
def dockerRepository = project.docker.dockerRepository ?: ''
if (!dockerRepository.empty && !dockerRepository.endsWith('/')) dockerRepository = dockerRepository + '/'
def imageName = project.docker.imageName ?: project.getName()
def cmd = "docker rmi ${dockerRepository}${imageName}:${tag}"

executeCmd(project, cmd)
}

static List<String> dockerBuildParameter(project) {
List<String> arguments = new ArrayList<>()

Expand Down

0 comments on commit 99f1881

Please sign in to comment.