Skip to content
Permalink
Browse files
Merge pull request #29 from jenkinsci/JENKINS-30770
[JENKINS-30770] Force latest tag when skip build is checked
  • Loading branch information
carlossg committed Nov 19, 2015
2 parents 4b4b8eb + 161ab14 commit c3d13c11b33ce6e053593e372734fa4454950ff7
Showing with 92 additions and 15 deletions.
  1. +16 −15 src/main/java/com/cloudbees/dockerpublish/DockerBuilder.java
  2. +76 −0 src/main/java/com/cloudbees/dockerpublish/ImageTag.java
@@ -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);
}
}

@@ -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;
@@ -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()
@@ -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);
}
@@ -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 c3d13c1

Please sign in to comment.