Skip to content

Commit

Permalink
Add skipDockerPush and skipDocker properties (fabric8io#205)
Browse files Browse the repository at this point in the history
* Add `skipDockerPush` to skip image push when push goal is bound to deploy phase.

* Add `skipDocker` to skip any Docker goal (useful when goals are bound to maven phases)

* Test `BuildMojo` for skip actions.

* Test `TagMojo` for skip actions.

* Test `PushMojo` for skip actions.

* Fix checkstyle errors

* - Rebase on master
- Expand imports
  • Loading branch information
cthiebault authored and mattnworb committed Aug 24, 2016
1 parent 35c8fb1 commit 127928e
Show file tree
Hide file tree
Showing 21 changed files with 531 additions and 121 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,16 @@ will need to do this binding so the image gets built when maven is run from the
</executions>
</plugin>

You can skip Docker build with `-DskipDockerBuild` and skip Docker tag with `-DskipDockerTag`.
You can skip Docker goals bound to Maven phases with:

* `-DskipDockerBuild` to skip image build
* `-DskipDockerTag` to skip image tag
* `-DskipDockerPush` to skip image push
* `-DskipDocker` to skip any Docker goals

To remove the image named `foobar` run the following command:

mvn docker:removeImage -DimageName=foobar
mvn docker:removeImage -DimageName=foobar

For a complete list of configuration options run:
`mvn com.spotify:docker-maven-plugin:<version>:help -Ddetail=true`
Expand Down
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>2.0.0</version>
<version>2.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -207,6 +207,12 @@
<version>4.1.6.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.19</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
71 changes: 50 additions & 21 deletions src/main/java/com/spotify/docker/AbstractDockerMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ abstract class AbstractDockerMojo extends AbstractMojo {
@Parameter(property = "retryPushTimeout", defaultValue = "10000")
private int retryPushTimeout;

/**
* Flag to skip docker goal, making goal a no-op. This can be useful when docker goal
* is bound to Maven phase, and you want to skip Docker command. Defaults to false.
*/
@Parameter(property = "skipDocker", defaultValue = "false")
private boolean skipDocker;

/**
* Flag to skip docker push, making push goal a no-op. This can be useful when docker:push
* is bound to deploy goal, and you want to deploy a jar but not a container. Defaults to false.
*/
@Parameter(property = "skipDockerPush", defaultValue = "false")
private boolean skipDockerPush;

public int getRetryPushTimeout() {
return retryPushTimeout;
}
Expand All @@ -105,33 +119,26 @@ public int getRetryPushCount() {
return retryPushCount;
};

public void execute() throws MojoExecutionException {
DockerClient client = null;
try {
final DefaultDockerClient.Builder builder = getBuilder();
public boolean isSkipDocker() {
return skipDocker;
}

final String dockerHost = rawDockerHost();
if (!isNullOrEmpty(dockerHost)) {
builder.uri(dockerHost);
}
final Optional<DockerCertificates> certs = dockerCertificates();
if (certs.isPresent()) {
builder.dockerCertificates(certs.get());
}
public boolean isSkipDockerPush() {
return skipDockerPush;
}

final AuthConfig authConfig = authConfig();
if (authConfig != null) {
builder.authConfig(authConfig);
}
@Override
public void execute() throws MojoExecutionException {

client = builder.build();
if (skipDocker) {
getLog().info("Skipping docker goal");
return;
}

try (DockerClient client = buildDockerClient()) {
execute(client);
} catch (Exception e) {
throw new MojoExecutionException("Exception caught", e);
} finally {
if (client != null) {
client.close();
}
}
}

Expand All @@ -140,6 +147,28 @@ protected DefaultDockerClient.Builder getBuilder() throws DockerCertificateExcep
.readTimeoutMillis(0);
}

protected DockerClient buildDockerClient()
throws DockerCertificateException, SecDispatcherException, MojoExecutionException {

final DefaultDockerClient.Builder builder = getBuilder();

final String dockerHost = rawDockerHost();
if (!isNullOrEmpty(dockerHost)) {
builder.uri(dockerHost);
}
final Optional<DockerCertificates> certs = dockerCertificates();
if (certs.isPresent()) {
builder.dockerCertificates(certs.get());
}

final AuthConfig authConfig = authConfig();
if (authConfig != null) {
builder.authConfig(authConfig);
}

return builder.build();
}

protected abstract void execute(final DockerClient dockerClient) throws Exception;

protected String rawDockerHost() {
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/spotify/docker/BuildMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ public boolean getForceTags() {
return forceTags;
}

public boolean isSkipDockerBuild() {
return skipDockerBuild;
}

@Override
protected void execute(final DockerClient docker)
throws MojoExecutionException, GitAPIException, IOException, DockerException,
Expand Down Expand Up @@ -356,11 +360,12 @@ protected void execute(final DockerClient docker)

// Push specific tags specified in pom rather than all images
if (pushImageTag) {
pushImageTag(docker, imageName, imageTags, getLog());
pushImageTag(docker, imageName, imageTags, getLog(), isSkipDockerPush());
}

if (pushImage) {
pushImage(docker, imageName, getLog(), buildInfo, getRetryPushCount(), getRetryPushTimeout());
pushImage(docker, imageName, getLog(), buildInfo, getRetryPushCount(), getRetryPushTimeout(),
isSkipDockerPush());
}

// Write image info file
Expand Down Expand Up @@ -800,4 +805,6 @@ private DockerClient.BuildParam[] buildParams()
}
return buildParams.toArray(new DockerClient.BuildParam[buildParams.size()]);
}


}
5 changes: 4 additions & 1 deletion src/main/java/com/spotify/docker/PushMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ public class PushMojo extends AbstractDockerMojo {
@Parameter(property = "imageName", required = true)
private String imageName;

@Override
protected void execute(DockerClient docker)
throws MojoExecutionException, DockerException, IOException, InterruptedException {
pushImage(docker, imageName, getLog(), null, getRetryPushCount(), getRetryPushTimeout());

pushImage(docker, imageName, getLog(), null, getRetryPushCount(), getRetryPushTimeout(),
isSkipDockerPush());
}

}
1 change: 1 addition & 0 deletions src/main/java/com/spotify/docker/RemoveImageMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class RemoveImageMojo extends AbstractDockerMojo {
@Parameter(property = "dockerImageTags")
private List<String> imageTags;

@Override
protected void execute(final DockerClient docker)
throws MojoExecutionException, DockerException, IOException, InterruptedException {
final String imageNameWithoutTag = parseImageName(imageName)[0];
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/spotify/docker/TagMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public class TagMojo extends AbstractDockerMojo {
@Parameter(property = "useGitCommitId", defaultValue = "false")
private boolean useGitCommitId;

public boolean isSkipDockerTag() {
return skipDockerTag;
}

@Override
protected void execute(DockerClient docker)
throws MojoExecutionException, DockerException,
Expand Down Expand Up @@ -116,7 +120,8 @@ protected void execute(DockerClient docker)
final DockerBuildInformation buildInfo = new DockerBuildInformation(normalizedName, getLog());

if (pushImage) {
pushImage(docker, newName, getLog(), buildInfo, getRetryPushCount(), getRetryPushTimeout());
pushImage(docker, newName, getLog(), buildInfo, getRetryPushCount(), getRetryPushTimeout(),
isSkipDockerPush());
}

writeImageInfoFile(buildInfo, tagInfoFile);
Expand Down
27 changes: 19 additions & 8 deletions src/main/java/com/spotify/docker/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,14 @@ public static String[] parseImageName(String imageName) throws MojoExecutionExce

public static void pushImage(DockerClient docker, String imageName, Log log,
final DockerBuildInformation buildInfo,
int retryPushCount, int retryPushTimeout)
int retryPushCount, int retryPushTimeout, boolean skipPush)
throws MojoExecutionException, DockerException, IOException, InterruptedException {

if (skipPush) {
log.info("Skipping docker push");
return;
}

int attempt = 0;
do {
final AnsiProgressHandler ansiProgressHandler = new AnsiProgressHandler();
Expand Down Expand Up @@ -107,14 +113,19 @@ public static void pushImage(DockerClient docker, String imageName, Log log,

// push just the tags listed in the pom rather than all images using imageName
public static void pushImageTag(DockerClient docker, String imageName,
List<String> imageTags, Log log)
List<String> imageTags, Log log, boolean skipPush)
throws MojoExecutionException, DockerException, IOException, InterruptedException {
// tags should not be empty if you have specified the option to push tags
if (imageTags.isEmpty()) {
throw new MojoExecutionException("You have used option \"pushImageTag\" but have"
+ " not specified an \"imageTag\" in your"
+ " docker-maven-client's plugin configuration");
}

if (skipPush) {
log.info("Skipping docker push");
return;
}
// tags should not be empty if you have specified the option to push tags
if (imageTags.isEmpty()) {
throw new MojoExecutionException("You have used option \"pushImageTag\" but have"
+ " not specified an \"imageTag\" in your"
+ " docker-maven-client's plugin configuration");
}
final CompositeImageName compositeImageName = CompositeImageName.create(imageName, imageTags);
for (final String imageTag : compositeImageName.getImageTags()) {
final String imageNameWithTag = compositeImageName.getName() + ":" + imageTag;
Expand Down
Loading

0 comments on commit 127928e

Please sign in to comment.