Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-30770] Force latest tag when skip build is checked
  • Loading branch information
carlossg committed Nov 18, 2015
1 parent 4b4b8eb commit 161ab14
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 15 deletions.
31 changes: 16 additions & 15 deletions src/main/java/com/cloudbees/dockerpublish/DockerBuilder.java
Expand Up @@ -265,8 +265,8 @@ private Perform(AbstractBuild build, Launcher launcher, BuildListener listener)
private boolean exec() {
try {
if (!isSkipDecorate()) {
for (String tag: getNameAndTag()) {
build.setDisplayName(build.getDisplayName() + " " + tag);
for (ImageTag imageTag : getImageTags()) {
build.setDisplayName(build.getDisplayName() + " " + imageTag);
}
}

Expand All @@ -290,16 +290,16 @@ private String expandAll(String s) throws MacroEvaluationException, IOException,
/**
* This tag is what is used to build, tag and push the registry.
*/
private List<String> getNameAndTag() throws MacroEvaluationException, IOException, InterruptedException {
List<String> tags = new ArrayList<String>();
private List<ImageTag> getImageTags() throws MacroEvaluationException, IOException, InterruptedException {
List<ImageTag> tags = new ArrayList<ImageTag>();
if (!defined(getRepoTag())) {
tags.add(expandAll(getRepo()));
tags.add(new ImageTag(expandAll(getRepo())));
} else {
for (String rt: expandAll(getRepoTag()).trim().split(",")) {
tags.add(expandAll(getRepo() + ":" + rt));
for (String rt : expandAll(getRepoTag()).trim().split(",")) {
tags.add(new ImageTag(expandAll(getRepo()), expandAll(rt)));
}
if (!isSkipTagLatest()) {
tags.add(expandAll(getRepo() + ":latest"));
tags.add(new ImageTag(expandAll(getRepo()), "latest"));
}
}
return tags;
Expand All @@ -310,17 +310,18 @@ private boolean maybeTagOnly() throws MacroEvaluationException, IOException, Int
if (!defined(getRepoTag())) {
result.add("echo 'Nothing to build or tag'");
} else {
for (String tag : getNameAndTag()) {
result.add("docker tag " + getRepo() + " " + tag);
for (ImageTag imageTag : getImageTags()) {
result.add(
"docker tag " + (imageTag.isLatest() ? "--force=true " : "") + getRepo() + " " + imageTag);
}
}
return executeCmd(result);
}

private boolean buildAndTag() throws MacroEvaluationException, IOException, InterruptedException {
FilePath context = defined(getBuildContext()) ?
new FilePath(new File(getBuildContext())) : build.getWorkspace();
Iterator<String> i = getNameAndTag().iterator();
FilePath context = defined(getBuildContext()) ? new FilePath(new File(getBuildContext()))
: build.getWorkspace();
Iterator<ImageTag> i = getImageTags().iterator();
Result lastResult = new Result();
if (i.hasNext()) {
lastResult = executeCmd("docker build -t " + i.next()
Expand Down Expand Up @@ -353,8 +354,8 @@ private boolean buildAndTag() throws MacroEvaluationException, IOException, Inte

private boolean dockerPushCommand() throws InterruptedException, MacroEvaluationException, IOException {
List<String> result = new ArrayList<String>();
for (String tag: getNameAndTag()) {
result.add("docker push " + tag);
for (ImageTag imageTag : getImageTags()) {
result.add("docker push " + imageTag.toString());
}
return executeCmd(result);
}
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/com/cloudbees/dockerpublish/ImageTag.java
@@ -0,0 +1,76 @@
/*
* The MIT License
*
* Copyright (c) 2015, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.cloudbees.dockerpublish;

import org.jenkinsci.plugins.docker.commons.credentials.DockerRegistryEndpoint;

/**
* Image names
*
* @author Carlos Sanchez <carlos@apache.org>
*
*/
public class ImageTag {

private String name;
private String tag;

public ImageTag(String name) {
this(name, null);
}

public ImageTag(String name, String tag) {
this.name = name;
this.tag = tag;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getTag() {
return tag;
}

public void setTag(String tag) {
this.tag = tag;
}

public boolean isLatest() {
return "latest".equals(tag);
}

public String toString() {
if (tag == null) {
return name;
} else {
return name + ":" + tag;
}
}
}

0 comments on commit 161ab14

Please sign in to comment.