diff --git a/src/main/java/hudson/plugins/git/GitPublisher.java b/src/main/java/hudson/plugins/git/GitPublisher.java index 5fe50cca4c..dee268950c 100644 --- a/src/main/java/hudson/plugins/git/GitPublisher.java +++ b/src/main/java/hudson/plugins/git/GitPublisher.java @@ -1,21 +1,12 @@ package hudson.plugins.git; -import hudson.EnvVars; -import hudson.Extension; -import hudson.FilePath; +import hudson.*; import hudson.FilePath.FileCallable; -import hudson.Launcher; -import hudson.Util; import hudson.matrix.MatrixAggregatable; import hudson.matrix.MatrixAggregator; import hudson.matrix.MatrixBuild; import hudson.matrix.MatrixRun; -import hudson.model.AbstractBuild; -import hudson.model.AbstractDescribableImpl; -import hudson.model.AbstractProject; -import hudson.model.BuildListener; -import hudson.model.Descriptor; -import hudson.model.Result; +import hudson.model.*; import hudson.plugins.git.opt.PreBuildMergeOptions; import hudson.remoting.VirtualChannel; import hudson.scm.SCM; @@ -24,12 +15,6 @@ import hudson.tasks.Publisher; import hudson.tasks.Recorder; import hudson.util.FormValidation; -import java.io.File; -import java.io.IOException; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; -import javax.servlet.ServletException; import org.apache.commons.lang.StringUtils; import org.eclipse.jgit.transport.RemoteConfig; import org.kohsuke.stapler.AncestorInPath; @@ -37,6 +22,13 @@ import org.kohsuke.stapler.QueryParameter; import org.kohsuke.stapler.StaplerRequest; +import javax.servlet.ServletException; +import java.io.File; +import java.io.IOException; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + public class GitPublisher extends Recorder implements Serializable, MatrixAggregatable { private static final long serialVersionUID = 1L; @@ -281,8 +273,8 @@ public Boolean invoke(File workspace, return false; } - if (t.isCreateTag()) { - if (git.tagExists(tagName)) { + if (t.isCreateTag() || t.isUpdateTag()) { + if (git.tagExists(tagName) && !t.isUpdateTag()) { listener.getLogger().println("Tag " + tagName + " already exists and Create Tag is specified, so failing."); return false; } @@ -297,7 +289,7 @@ else if (!git.tagExists(tagName)) { listener.getLogger().println("Tag " + tagName + " does not exist and Create Tag is not specified, so failing."); return false; } - + listener.getLogger().println("Pushing tag " + tagName + " to repo " + targetRepo); git.push(remote, tagName); @@ -589,6 +581,7 @@ public static final class TagToPush extends PushConfig { private String tagName; private String tagMessage; private boolean createTag; + private boolean updateTag; public String getTagName() { return tagName; @@ -602,12 +595,17 @@ public boolean isCreateTag() { return createTag; } + public boolean isUpdateTag() { + return updateTag; + } + @DataBoundConstructor - public TagToPush(String targetRepoName, String tagName, String tagMessage, boolean createTag) { + public TagToPush(String targetRepoName, String tagName, String tagMessage, boolean createTag, boolean updateTag) { super(targetRepoName); this.tagName = Util.fixEmptyAndTrim(tagName); this.tagMessage = tagMessage; this.createTag = createTag; + this.updateTag = updateTag; } @Extension diff --git a/src/main/resources/hudson/plugins/git/GitPublisher/config.jelly b/src/main/resources/hudson/plugins/git/GitPublisher/config.jelly index f7c4e568d7..b82bbde812 100644 --- a/src/main/resources/hudson/plugins/git/GitPublisher/config.jelly +++ b/src/main/resources/hudson/plugins/git/GitPublisher/config.jelly @@ -42,6 +42,10 @@ title="${%Create new tag}"> + + + Specify tags to push at the completion of the build.
- If the "Create Tag" option is selected, the tag will be created and pushed at the completion + If the "Create Tag" or "Update tag" option is selected, the tag will be created or updated and pushed at the completion of the build, and the push will fail if a tag with the given name already exists. If the "Create Tag" option is not selected, the push will fail if the tag does not already exist.
Environment variables may be used in the tag name - they will be replaced at build time.
diff --git a/src/test/java/hudson/plugins/git/GitPublisherTest.java b/src/test/java/hudson/plugins/git/GitPublisherTest.java index 6db52d72a0..2e7ece1880 100644 --- a/src/test/java/hudson/plugins/git/GitPublisherTest.java +++ b/src/test/java/hudson/plugins/git/GitPublisherTest.java @@ -31,10 +31,9 @@ import hudson.model.AbstractBuild; import hudson.model.BuildListener; import hudson.model.Hudson; -import hudson.model.Result; import hudson.plugins.git.GitPublisher.BranchToPush; -import hudson.plugins.git.GitPublisher.TagToPush; import hudson.plugins.git.GitPublisher.NoteToPush; +import hudson.plugins.git.GitPublisher.TagToPush; import hudson.scm.NullSCM; import hudson.tasks.BuildStepDescriptor; import org.jvnet.hudson.test.Bug; @@ -57,7 +56,7 @@ public void testMatrixBuild() throws Exception { mp.setAxes(new AxisList(new Axis("VAR","a","b"))); mp.setScm(new GitSCM(workDir.getAbsolutePath())); mp.getPublishersList().add(new GitPublisher( - Collections.singletonList(new TagToPush("origin","foo","message",true)), + Collections.singletonList(new TagToPush("origin","foo","message",true, false)), Collections.emptyList(), Collections.emptyList(), true, true) {