Skip to content

Commit

Permalink
Add the ability to attach a saved image archive as a project artifact (
Browse files Browse the repository at this point in the history
…#1210)

This PR is to add the ability to attach an image archive produced by docker:save to the project as an artifact. Currently this is not possible and archives have to be attached using something like build-helper-maven-plugin.

To avoid conflict with docker:source, the default attach classifier is `archive` (or `archive-<saveAlias>`), instead of `docker` (or `docker-<alias>`) as used by that mojo.
  • Loading branch information
wrosenuance authored and rhuss committed Apr 21, 2019
1 parent 86034f7 commit 885954a
Show file tree
Hide file tree
Showing 4 changed files with 304 additions and 5 deletions.
1 change: 1 addition & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Restore ANSI color to Maven logging if disabled during plugin execution and enable color for Windows with Maven 3.5.0 or later. Color logging is enabled by default, but disabled if the Maven CLI disables color (e.g. in batch mode) ([#1108](https://github.com/fabric8io/docker-maven-plugin/issues/1108))
- Fix NPE if docker:save is called with -Dfile=file-name-only.tar ([#1203](https://github.com/fabric8io/docker-maven-plugin/issues/1203))
- Improve GZIP compression performance for docker:save ([#1205](https://github.com/fabric8io/docker-maven-plugin/issues/1205))
- Allow docker:save to attach image archive as a project artifact ([#1210](https://github.com/fabric8io/docker-maven-plugin/pull/1210))
- Use pattern to detect image name in archive loaded during build and tag with image name from the project configuration ([#1207](https://github.com/fabric8io/docker-maven-plugin/issues/1207))

* **0.29.0** (2019-04-08)
Expand Down
29 changes: 29 additions & 0 deletions src/main/asciidoc/inc/_docker-save.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@ If the option `saveFile` is not set, the file name is calculated automatically:

Please note that the exported image contains all image layers and can be quite large (also, it takes a bit to export the image).

.Controlling image compression
The file name extension is used to select a compression method for the output.
[cols="3,2,1"]
|===
| Extensions | Compression | Type

| .tar or unrecognized
| No compression
| .tar

| .tar.gz, .tgz
| GZIP compression
| .tar.gz

| .tar.bz, .tar.bz2, .tar.bzip2
| BZIP2 compression
| .tar.bz

|===

.Attaching the saved image as an artifact
If `saveClassifier` is set, the saved archive will be attached to the project using the provided classifier and the type determined from the file name. The placeholder `%a` will be replaced with the image alias.

Note that using overriding the default to use `docker` or `docker-%a` may lead to a conflict if a source archive is also attached with <<{plugin}:source>>.

.Save options
[cols="1,5,1"]
|===
Expand All @@ -27,6 +52,10 @@ Please note that the exported image contains all image layers and can be quite l
| The filename to save.
| `docker.save.file` or `docker.file` or `file`

| *saveClassifier*
| If set, attach the the saved archive to the project with the provided classifier. A placeholder of `%a` will be replaced with the image alias.
| `docker.save.classifier`

| *skipSave*
| A boolean flag whether to skip execution of the goal.
| `docker.skip.save`
Expand Down
29 changes: 24 additions & 5 deletions src/main/java/io/fabric8/maven/docker/SaveMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public class SaveMojo extends AbstractDockerMojo {
@Parameter(property = "docker.skip.save", defaultValue = "false")
private boolean skipSave;

@Parameter(property = "docker.save.classifier")
private String saveClassifier;

@Override
protected void executeInternal(ServiceHub serviceHub) throws DockerAccessException, MojoExecutionException {

Expand All @@ -47,7 +50,8 @@ protected void executeInternal(ServiceHub serviceHub) throws DockerAccessExcepti
return;
}

String imageName = getImageName(images);
ImageConfiguration image = getImageToSave(images);
String imageName = image.getName();
String fileName = getFileName(imageName);
ensureSaveDir(fileName);
log.info("Saving image %s to %s", imageName, fileName);
Expand All @@ -56,8 +60,14 @@ protected void executeInternal(ServiceHub serviceHub) throws DockerAccessExcepti
}

long time = System.currentTimeMillis();
serviceHub.getDockerAccess().saveImage(imageName, fileName, ArchiveCompression.fromFileName(fileName));
ArchiveCompression compression = ArchiveCompression.fromFileName(fileName);
serviceHub.getDockerAccess().saveImage(imageName, fileName, compression);
log.info("%s: Saved image to %s in %s", imageName, fileName, EnvUtil.formatDurationTill(time));

String classifier = getClassifier(image);
if(classifier != null) {
projectHelper.attachArtifact(project, compression.getFileSuffix(), classifier, new File(fileName));
}
}

private boolean skipSaveFor(List<ImageConfiguration> images) {
Expand Down Expand Up @@ -117,12 +127,12 @@ private void ensureSaveDir(String fileName) throws MojoExecutionException {
}
}

private String getImageName(List<ImageConfiguration> images) throws MojoExecutionException {
private ImageConfiguration getImageToSave(List<ImageConfiguration> images) throws MojoExecutionException {
// specify image by name or alias
if (saveName == null && saveAlias == null) {
List<ImageConfiguration> buildImages = getImagesWithBuildConfig(images);
if (buildImages.size() == 1) {
return buildImages.get(0).getName();
return buildImages.get(0);
}
throw new MojoExecutionException("More than one image with build configuration is defined. Please specify the image with 'docker.name' or 'docker.alias'.");
}
Expand All @@ -131,7 +141,7 @@ private String getImageName(List<ImageConfiguration> images) throws MojoExecutio
}
for (ImageConfiguration ic : images) {
if (equalName(ic) || equalAlias(ic)) {
return ic.getName();
return ic;
}
}
throw new MojoExecutionException(saveName != null ?
Expand All @@ -149,6 +159,15 @@ private List<ImageConfiguration> getImagesWithBuildConfig(List<ImageConfiguratio
return ret;
}

private String getClassifier(ImageConfiguration image) {
if(saveClassifier == null || saveClassifier.length() == 0) {
return null;
}

return saveClassifier.replace("%a", image.getAlias() == null ? "" : image.getAlias());
}


private boolean equalAlias(ImageConfiguration ic) {
return saveAlias != null && saveAlias.equals(ic.getAlias());
}
Expand Down
Loading

0 comments on commit 885954a

Please sign in to comment.