From 3bbe8cfc2c77ee663d68e2f64914edaee5981add Mon Sep 17 00:00:00 2001 From: christ66 Date: Mon, 6 Apr 2015 23:06:48 -0700 Subject: [PATCH] [JENKINS-26626] Switch to use BUILD_TIMESTAMP environment variable instead of the BUILD_ID. Newer versions of Jenkins use the BUID_ID as being the build number. --- pom.xml | 10 ++++- .../ZenTimestampJobPropertyTest.java | 40 +++++++++++++++---- .../zentimestamp/ZenTimestampAction.java | 4 +- .../ZenTimestampEnvironmentContributor.java | 4 +- .../ZenTimestampFormatBuildWrapper.java | 4 +- .../ZenTimestampNodeProperty.java | 2 +- .../zentimestamp/ZenTimestampRunListener.java | 4 +- .../plugins/zentimestamp/Messages.properties | 2 +- .../help-pattern.html | 5 +-- .../ZenTimestampJobProperty/config.properties | 2 +- .../help-changeBUILDID.html | 2 +- src/main/resources/index.jelly | 2 +- src/main/webapp/help-node.html | 2 +- 13 files changed, 59 insertions(+), 24 deletions(-) diff --git a/pom.xml b/pom.xml index 61329e3..fd80a2e 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.jenkins-ci.plugins plugin - 1.410 + 1.597 zentimestamp @@ -66,6 +66,14 @@ scm:git:git@github.com:jenkinsci/zentimestamp-plugin.git + + + org.jenkins-ci.plugins + matrix-project + 1.3 + + + repo.jenkins-ci.org diff --git a/src/ittest/java/hudson/plugins/zentimestamp/ZenTimestampJobPropertyTest.java b/src/ittest/java/hudson/plugins/zentimestamp/ZenTimestampJobPropertyTest.java index 627c385..0fe5cfb 100644 --- a/src/ittest/java/hudson/plugins/zentimestamp/ZenTimestampJobPropertyTest.java +++ b/src/ittest/java/hudson/plugins/zentimestamp/ZenTimestampJobPropertyTest.java @@ -4,34 +4,60 @@ import hudson.model.FreeStyleProject; import hudson.model.Result; import hudson.tasks.Shell; +import org.junit.Rule; +import org.junit.Test; import org.jvnet.hudson.test.HudsonTestCase; +import org.jvnet.hudson.test.Issue; +import org.jvnet.hudson.test.JenkinsRule; import java.text.SimpleDateFormat; +import java.util.Properties; -public class ZenTimestampJobPropertyTest extends HudsonTestCase { +public class ZenTimestampJobPropertyTest { + @Rule public JenkinsRule r = new JenkinsRule(); + @Test + public void changeBuildID() throws Exception { - public void testChangeBuildID() throws Exception { - - final String BUILD_ID = "BUILD_ID"; + final String BUILD_ID = ZenTimestampAction.BUILD_TIMESTAMP_VARIABLE; String pattern = "yyyyMMddHHmmss"; - FreeStyleProject project = createFreeStyleProject(); + FreeStyleProject project = r.createFreeStyleProject(); project.getBuildersList().add(new Shell("echo ${" + BUILD_ID + "}")); project.addProperty(new ZenTimestampJobProperty(true, pattern)); FreeStyleBuild build = project.scheduleBuild2(0).get(); //Build status - assertBuildStatus(Result.SUCCESS, build); + r.assertBuildStatus(Result.SUCCESS, build); //Build log SimpleDateFormat dateFormat = new SimpleDateFormat(pattern); StringBuffer expectedLog = new StringBuffer().append("echo ").append(dateFormat.format(build.getTime())); - assertLogContains(expectedLog.toString(), build); + r.assertLogContains(expectedLog.toString(), build); } + @Test + @Issue("JENKINS-26626") + public void changeTimestampVariable() throws Exception { + final String BUILD_ID=ZenTimestampAction.BUILD_TIMESTAMP_VARIABLE; + + String pattern = "yyyyMMddHHmmss"; + FreeStyleProject p = r.createFreeStyleProject(); + p.getBuildersList().add(new Shell("echo $BUILD_ID\necho $" + BUILD_ID)); + p.addProperty(new ZenTimestampJobProperty(true, pattern)); + FreeStyleBuild build = p.scheduleBuild2(0).get(); + r.assertBuildStatus(Result.SUCCESS, build); + + SimpleDateFormat dateFormat = new SimpleDateFormat(pattern); + + // BUILD_ID should return 1 in newer versions of Jenkins + r.assertLogContains("echo 1", build); + + // BUILD_TIMESTAMP now returns the correct build time. + r.assertLogContains("echo " + dateFormat.format(build.getTime()), build); + } } \ No newline at end of file diff --git a/src/main/java/hudson/plugins/zentimestamp/ZenTimestampAction.java b/src/main/java/hudson/plugins/zentimestamp/ZenTimestampAction.java index aa0b6ed..29d21cc 100644 --- a/src/main/java/hudson/plugins/zentimestamp/ZenTimestampAction.java +++ b/src/main/java/hudson/plugins/zentimestamp/ZenTimestampAction.java @@ -12,6 +12,8 @@ public class ZenTimestampAction implements EnvironmentContributingAction { private String pattern; + public static final String BUILD_TIMESTAMP_VARIABLE="BUILD_TIMESTAMP"; + public ZenTimestampAction(String pattern) { this.pattern = pattern; } @@ -21,7 +23,7 @@ public void buildEnvVars(AbstractBuild build, EnvVars env) { Calendar buildTimestamp = build.getTimestamp(); SimpleDateFormat sdf = new SimpleDateFormat(pattern); String formattedBUILDID = sdf.format(buildTimestamp.getTime()); - env.put("BUILD_ID", formattedBUILDID); + env.put(BUILD_TIMESTAMP_VARIABLE, formattedBUILDID); } public String getDisplayName() { diff --git a/src/main/java/hudson/plugins/zentimestamp/ZenTimestampEnvironmentContributor.java b/src/main/java/hudson/plugins/zentimestamp/ZenTimestampEnvironmentContributor.java index 0075502..556e8ac 100644 --- a/src/main/java/hudson/plugins/zentimestamp/ZenTimestampEnvironmentContributor.java +++ b/src/main/java/hudson/plugins/zentimestamp/ZenTimestampEnvironmentContributor.java @@ -56,10 +56,10 @@ public void buildEnvironmentFor(Run r, EnvVars envs, TaskListener listener) thro if (pattern != null) { final PrintStream logger = listener.getLogger(); Calendar buildTimestamp = build.getTimestamp(); - logger.println(String.format("Changing BUILD_ID variable (job build time) with the date pattern %s.", pattern)); + logger.println(String.format("Changing " + ZenTimestampAction.BUILD_TIMESTAMP_VARIABLE + " variable (job build time) with the date pattern %s.", pattern)); SimpleDateFormat sdf = new SimpleDateFormat(pattern); final String formattedBuildValue = sdf.format(buildTimestamp.getTime()); - envs.put("BUILD_ID", formattedBuildValue); + envs.put(ZenTimestampAction.BUILD_TIMESTAMP_VARIABLE, formattedBuildValue); } } diff --git a/src/main/java/hudson/plugins/zentimestamp/ZenTimestampFormatBuildWrapper.java b/src/main/java/hudson/plugins/zentimestamp/ZenTimestampFormatBuildWrapper.java index 31274f5..ba9c42d 100644 --- a/src/main/java/hudson/plugins/zentimestamp/ZenTimestampFormatBuildWrapper.java +++ b/src/main/java/hudson/plugins/zentimestamp/ZenTimestampFormatBuildWrapper.java @@ -41,7 +41,7 @@ public hudson.tasks.BuildWrapper.Environment setUp(AbstractBuild build, Launcher final PrintStream logger = listener.getLogger(); Calendar buildTimestamp = build.getTimestamp(); - logger.println("Formating the BUILD_ID variable with'" + pattern + "' pattern."); + logger.println("Formating the " + ZenTimestampAction.BUILD_TIMESTAMP_VARIABLE + " variable with'" + pattern + "' pattern."); SimpleDateFormat sdf = new SimpleDateFormat(pattern); final String newBUILDIDStr = sdf.format(buildTimestamp.getTime()); @@ -49,7 +49,7 @@ public hudson.tasks.BuildWrapper.Environment setUp(AbstractBuild build, Launcher @Override public void buildEnvVars(Map env) { - env.put("BUILD_ID", newBUILDIDStr); + env.put(ZenTimestampAction.BUILD_TIMESTAMP_VARIABLE, newBUILDIDStr); } }; } diff --git a/src/main/java/hudson/plugins/zentimestamp/ZenTimestampNodeProperty.java b/src/main/java/hudson/plugins/zentimestamp/ZenTimestampNodeProperty.java index fbdd70d..575df5b 100644 --- a/src/main/java/hudson/plugins/zentimestamp/ZenTimestampNodeProperty.java +++ b/src/main/java/hudson/plugins/zentimestamp/ZenTimestampNodeProperty.java @@ -26,7 +26,7 @@ public String getPattern() { public static class ZenTimestampNodePropertyDescriptor extends NodePropertyDescriptor { @Override public String getDisplayName() { - return "Date pattern for the BUILD_ID (build timestamp) variable"; + return "Date pattern for the " + ZenTimestampAction.BUILD_TIMESTAMP_VARIABLE + " (build timestamp) variable"; } @Override diff --git a/src/main/java/hudson/plugins/zentimestamp/ZenTimestampRunListener.java b/src/main/java/hudson/plugins/zentimestamp/ZenTimestampRunListener.java index 00ba03a..c4138da 100644 --- a/src/main/java/hudson/plugins/zentimestamp/ZenTimestampRunListener.java +++ b/src/main/java/hudson/plugins/zentimestamp/ZenTimestampRunListener.java @@ -51,14 +51,14 @@ public Environment setUpEnvironment(AbstractBuild build, Launcher launcher, Buil final PrintStream logger = listener.getLogger(); Calendar buildTimestamp = build.getTimestamp(); - logger.println("Formatting the BUILD_ID variable with'" + pattern + "' pattern."); + logger.println("Formatting the " + ZenTimestampAction.BUILD_TIMESTAMP_VARIABLE + " variable with'" + pattern + "' pattern."); SimpleDateFormat sdf = new SimpleDateFormat(pattern); final String formattedBuildValue = sdf.format(buildTimestamp.getTime()); return new Environment() { @Override public void buildEnvVars(Map env) { - env.put("BUILD_ID", formattedBuildValue); + env.put(ZenTimestampAction.BUILD_TIMESTAMP_VARIABLE, formattedBuildValue); } }; } diff --git a/src/main/resources/hudson/plugins/zentimestamp/Messages.properties b/src/main/resources/hudson/plugins/zentimestamp/Messages.properties index ece2832..b79e951 100644 --- a/src/main/resources/hudson/plugins/zentimestamp/Messages.properties +++ b/src/main/resources/hudson/plugins/zentimestamp/Messages.properties @@ -1,3 +1,3 @@ -ZenTimestampFormatBuildWrapper.displayName=Change BUILD_ID format +ZenTimestampFormatBuildWrapper.displayName=Change BUILD_TIMESTAMP format ZenTimestampFormatBuildWrapper.emptyPattern=You must provide a pattern value ZenTimestampFormatBuildWrapper.invalidInput=Invalid input {0} \ No newline at end of file diff --git a/src/main/resources/hudson/plugins/zentimestamp/ZenTimestampFormatBuildWrapper/help-pattern.html b/src/main/resources/hudson/plugins/zentimestamp/ZenTimestampFormatBuildWrapper/help-pattern.html index 66cbdb5..a04c259 100644 --- a/src/main/resources/hudson/plugins/zentimestamp/ZenTimestampFormatBuildWrapper/help-pattern.html +++ b/src/main/resources/hudson/plugins/zentimestamp/ZenTimestampFormatBuildWrapper/help-pattern.html @@ -2,8 +2,7 @@

This wrapper is displayed due to the usage of an old configuration job instance. At the next save, the old value will be converted in the new job property value. - Attention: Do not try to change the BUILD_ID value here. If you want to change the BUILD_ID value, use the job - property - section for the plugin. + Attention: Do not try to change the BUILD_TIMESTAMP value here. If you want to change the BUILD_TIMESTAMP value, use the job + property section for the plugin.

\ No newline at end of file diff --git a/src/main/resources/hudson/plugins/zentimestamp/ZenTimestampJobProperty/config.properties b/src/main/resources/hudson/plugins/zentimestamp/ZenTimestampJobProperty/config.properties index e2ce769..c4e434c 100644 --- a/src/main/resources/hudson/plugins/zentimestamp/ZenTimestampJobProperty/config.properties +++ b/src/main/resources/hudson/plugins/zentimestamp/ZenTimestampJobProperty/config.properties @@ -1 +1 @@ -BUILDIDOptionLabel=Change date pattern for the BUILD_ID (build timestamp) variable \ No newline at end of file +BUILDIDOptionLabel=Change date pattern for the BUILD_TIMESTAMP (build timestamp) variable \ No newline at end of file diff --git a/src/main/resources/hudson/plugins/zentimestamp/ZenTimestampJobProperty/help-changeBUILDID.html b/src/main/resources/hudson/plugins/zentimestamp/ZenTimestampJobProperty/help-changeBUILDID.html index b0c2ebc..85e8ab1 100644 --- a/src/main/resources/hudson/plugins/zentimestamp/ZenTimestampJobProperty/help-changeBUILDID.html +++ b/src/main/resources/hudson/plugins/zentimestamp/ZenTimestampJobProperty/help-changeBUILDID.html @@ -1,6 +1,6 @@

- The Jenkins BUILD_ID variable uses by default the date and time 'YYYY-MM-DD_hh-mm-ss' of the international + The Jenkins BUILD_TIMESTAMP variable uses by default the date and time 'YYYY-MM-DD_hh-mm-ss' of the international standard.
You can change this format by providing your own pattern.

diff --git a/src/main/resources/index.jelly b/src/main/resources/index.jelly index e1ba18a..e05c999 100644 --- a/src/main/resources/index.jelly +++ b/src/main/resources/index.jelly @@ -1,3 +1,3 @@
- Plugin that allows the customization of the date and time pattern for the Jenkins BUILD_ID variable. + Plugin that allows the customization of the date and time pattern for the Jenkins BUILD_TIMESTAMP variable.
diff --git a/src/main/webapp/help-node.html b/src/main/webapp/help-node.html index 1968774..2751f4e 100644 --- a/src/main/webapp/help-node.html +++ b/src/main/webapp/help-node.html @@ -1,6 +1,6 @@

- The Jenkins BUILD_ID variable uses by default the date and time 'YYYY-MM-DD_hh-mm-ss' of the international + The Jenkins BUILD_TIMESTAMP variable uses by default the date and time 'YYYY-MM-DD_hh-mm-ss' of the international standard.
You can change this format by providing your own pattern for all jobs on this node.
You always are able to override the date pattern in your job.