Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1 from christ66/JENKINS-26626
[JENKINS-26626] Switch to use BUILD_TIMESTAMP environment variable
  • Loading branch information
gboissinot committed Apr 7, 2015
2 parents c04098b + 3bbe8cf commit d048982
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 24 deletions.
10 changes: 9 additions & 1 deletion pom.xml
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.410</version>
<version>1.597</version>
</parent>

<artifactId>zentimestamp</artifactId>
Expand Down Expand Up @@ -66,6 +66,14 @@
<developerConnection>scm:git:git@github.com:jenkinsci/zentimestamp-plugin.git</developerConnection>
</scm>

<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>matrix-project</artifactId>
<version>1.3</version>
</dependency>
</dependencies>

<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
Expand Down
Expand Up @@ -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);
}

}
Expand Up @@ -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;
}
Expand All @@ -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() {
Expand Down
Expand Up @@ -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);
}
}

Expand Down
Expand Up @@ -41,15 +41,15 @@ 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());

return new Environment() {

@Override
public void buildEnvVars(Map<String, String> env) {
env.put("BUILD_ID", newBUILDIDStr);
env.put(ZenTimestampAction.BUILD_TIMESTAMP_VARIABLE, newBUILDIDStr);
}
};
}
Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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<String, String> env) {
env.put("BUILD_ID", formattedBuildValue);
env.put(ZenTimestampAction.BUILD_TIMESTAMP_VARIABLE, formattedBuildValue);
}
};
}
Expand Down
@@ -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}
Expand Up @@ -2,8 +2,7 @@
<p>
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.
</p>
</div>
@@ -1 +1 @@
BUILDIDOptionLabel=Change date pattern for the BUILD_ID (build timestamp) variable
BUILDIDOptionLabel=Change date pattern for the BUILD_TIMESTAMP (build timestamp) variable
@@ -1,6 +1,6 @@
<div>
<p>
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.<br/>
You can change this format by providing your own pattern.
</p>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/index.jelly
@@ -1,3 +1,3 @@
<div>
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.
</div>
2 changes: 1 addition & 1 deletion src/main/webapp/help-node.html
@@ -1,6 +1,6 @@
<div>
<p>
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.<br/>
You can change this format by providing your own pattern for all jobs on this node. <br/>
You always are able to override the date pattern in your job.
Expand Down

0 comments on commit d048982

Please sign in to comment.