Skip to content

Commit

Permalink
[FIXED JENKINS-42952] Make currentBuild.duration meaningful
Browse files Browse the repository at this point in the history
...and add currentBuild.durationString.
  • Loading branch information
abayer committed Mar 21, 2017
1 parent 494446b commit fc61d88
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Expand Up @@ -133,7 +133,12 @@ public long getStartTimeInMillis() throws AbortException {

@Whitelisted
public long getDuration() throws AbortException {
return build().getDuration();
return System.currentTimeMillis() - build().getStartTimeInMillis();
}

@Whitelisted
public String getDurationString() throws AbortException {
return build().getDurationString();
}

@Whitelisted
Expand Down
Expand Up @@ -24,6 +24,7 @@

package org.jenkinsci.plugins.workflow.support.steps.build;

import hudson.model.Messages;
import hudson.model.Result;
import java.util.regex.Pattern;
import jenkins.plugins.git.GitSampleRepoRule;
Expand Down Expand Up @@ -166,6 +167,21 @@ public class RunWrapperTest {
});
}

@Issue("JENKINS-42952")
@Test public void duration() {
r.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
WorkflowJob p = r.j.createProject(WorkflowJob.class, "this-job");
p.setDefinition(new CpsFlowDefinition(
"echo \"currentBuild.duration='${currentBuild.duration}'\"\n" +
"echo \"currentBuild.durationString='${currentBuild.durationString}'\"\n", true));
WorkflowRun b = r.j.assertBuildStatusSuccess(p.scheduleBuild2(0).get());
r.j.assertLogNotContains("currentBuild.duration='0'", b);
r.j.assertLogNotContains("currentBuild.durationString='" + Messages.Run_NotStartedYet() + "'", b);
}
});
}

// Like org.hamcrest.text.MatchesPattern.matchesPattern(String) but doing a substring, not whole-string, match:
private static Matcher<String> containsRegexp(final String rx) {
return new SubstringMatcher(rx) {
Expand Down

0 comments on commit fc61d88

Please sign in to comment.