Skip to content

Commit

Permalink
[FIXED JENKINS-37897] Switch to block scoped stages.
Browse files Browse the repository at this point in the history
Also create synthetic stages for post-build and notifications. Maybe
more of those to come in the future, but haven't decided yet. Also
bumped plugin versions.
  • Loading branch information
abayer committed Sep 1, 2016
1 parent 1ddd0ff commit 04ad0c2
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 45 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Expand Up @@ -134,7 +134,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>pipeline-stage-step</artifactId>
<version>2.1</version>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand All @@ -144,7 +144,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.4</version>
<version>1.5</version>
</dependency>

<!-- JSON schema stuff -->
Expand Down Expand Up @@ -183,7 +183,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps-global-lib</artifactId>
<version>2.1</version>
<version>2.2</version>
<scope>test</scope>
</dependency>

Expand Down
Expand Up @@ -72,25 +72,28 @@ public class ModelInterpreter implements Serializable {
for (int i = 0; i < root.stages.getStages().size(); i++) {
Stage thisStage = root.stages.getStages().get(i)

script.stage thisStage.name

Closure closureToCall = thisStage.closureWrapper.closure
closureToCall.delegate = script
closureToCall.resolveStrategy = Closure.DELEGATE_FIRST
closureToCall.call()
script.stage(thisStage.name) {
Closure closureToCall = thisStage.closureWrapper.closure
closureToCall.delegate = script
closureToCall.resolveStrategy = Closure.DELEGATE_FIRST
closureToCall.call()
}
}
}.call()
}
// Now run the post-build actions wrapped in catchError.
script.catchError {
catchRequiredContextForNode(root.agent) {
List<Closure> postBuildClosures = root.satisfiedPostBuilds(script.getProperty("currentBuild"))

for (int i = 0; i < postBuildClosures.size(); i++) {
Closure c = postBuildClosures.get(i)
c.delegate = script
c.resolveStrategy = Closure.DELEGATE_FIRST
c.call()
if (postBuildClosures.size() > 0) {
script.stage("Post Build Actions") {
for (int i = 0; i < postBuildClosures.size(); i++) {
Closure c = postBuildClosures.get(i)
c.delegate = script
c.resolveStrategy = Closure.DELEGATE_FIRST
c.call()
}
}
}
}.call()
}
Expand All @@ -102,11 +105,15 @@ public class ModelInterpreter implements Serializable {
List<Closure> notificationClosures = root.satisfiedNotifications(script.getProperty("currentBuild"))

catchRequiredContextForNode(root.agent, true) {
for (int i = 0; i < notificationClosures.size(); i++) {
Closure c = notificationClosures.get(i)
c.delegate = script
c.resolveStrategy = Closure.DELEGATE_FIRST
c.call()
if (notificationClosures.size() > 0) {
script.stage("Notifications") {
for (int i = 0; i < notificationClosures.size(); i++) {
Closure c = notificationClosures.get(i)
c.delegate = script
c.resolveStrategy = Closure.DELEGATE_FIRST
c.call()
}
}
}
}.call()
}
Expand Down
Expand Up @@ -44,7 +44,7 @@ public void agentLabel() throws Exception {

WorkflowRun b = getAndStartBuild();
j.assertBuildStatusSuccess(j.waitForCompletion(b));
j.assertLogContains("Entering stage foo", b);
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("ONSLAVE=true", b);
}

Expand All @@ -56,7 +56,7 @@ public void noCheckoutScmInWrongContext() throws Exception {

WorkflowRun b = getAndStartNonRepoBuild("noCheckoutScmInWrongContext");
j.assertBuildStatusSuccess(j.waitForCompletion(b));
j.assertLogContains("Entering stage foo", b);
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("ONSLAVE=true", b);
}

Expand All @@ -73,7 +73,7 @@ public void agentDocker() throws Exception {

WorkflowRun b = getAndStartBuild();
j.assertBuildStatusSuccess(j.waitForCompletion(b));
j.assertLogContains("Entering stage foo", b);
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("The answer is 42", b);
}

Expand Down Expand Up @@ -105,7 +105,7 @@ public void agentNoneWithNode() throws Exception {

WorkflowRun b = getAndStartBuild();
j.assertBuildStatusSuccess(j.waitForCompletion(b));
j.assertLogContains("Entering stage foo", b);
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("ONSLAVE=true", b);
}
}
Expand Up @@ -44,7 +44,9 @@ public void simplePipeline() throws Exception {

WorkflowRun b = getAndStartBuild();
j.assertBuildStatusSuccess(j.waitForCompletion(b));
j.assertLogContains("Entering stage foo", b);
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogNotContains("[Pipeline] { (Post Build Actions)", b);
j.assertLogNotContains("[Pipeline] { (Notifications)", b);
j.assertLogContains("hello", b);
}

Expand All @@ -54,9 +56,9 @@ public void twoStagePipeline() throws Exception {

WorkflowRun b = getAndStartBuild();
j.assertBuildStatusSuccess(j.waitForCompletion(b));
j.assertLogContains("Entering stage foo", b);
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("hello", b);
j.assertLogContains("Entering stage bar", b);
j.assertLogContains("[Pipeline] { (bar)", b);
j.assertLogContains("goodbye", b);
}

Expand All @@ -66,7 +68,7 @@ public void validStepParameters() throws Exception {

WorkflowRun b = getAndStartBuild();
j.assertBuildStatusSuccess(j.waitForCompletion(b));
j.assertLogContains("Entering stage foo", b);
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("[Pipeline] timeout", b);
j.assertLogContains("hello", b);
}
Expand All @@ -81,7 +83,7 @@ public void metaStepSyntax() throws Exception {

WorkflowRun b = getAndStartBuild();
j.assertBuildStatusSuccess(j.waitForCompletion(b));
j.assertLogContains("Entering stage foo", b);
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("ONSLAVE=true", b);

VirtualFile archivedFile = b.getArtifactManager().root().child("msg.out");
Expand All @@ -99,7 +101,7 @@ public void legacyMetaStepSyntax() throws Exception {

WorkflowRun b = getAndStartBuild();
j.assertBuildStatusSuccess(j.waitForCompletion(b));
j.assertLogContains("Entering stage foo", b);
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("ONSLAVE=true", b);

VirtualFile archivedFile = b.getArtifactManager().root().child("msg.out");
Expand All @@ -113,7 +115,7 @@ public void parallelPipeline() throws Exception {

WorkflowRun b = getAndStartBuild();
j.assertBuildStatusSuccess(j.waitForCompletion(b));
j.assertLogContains("Entering stage foo", b);
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("[first] { (Branch: first)", b);
j.assertLogContains("[second] { (Branch: second)", b);
}
Expand All @@ -131,7 +133,7 @@ public void dockerGlobalVariable() throws Exception {

WorkflowRun b = getAndStartBuild();
j.assertBuildStatusSuccess(j.waitForCompletion(b));
j.assertLogContains("Entering stage foo", b);
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("image: ubuntu", b);
}

Expand Down
Expand Up @@ -41,8 +41,10 @@ public void simpleNotification() throws Exception {

WorkflowRun b = getAndStartBuild();
j.assertBuildStatusSuccess(j.waitForCompletion(b));
j.assertLogContains("Entering stage foo", b);
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("hello", b);
j.assertLogContains("[Pipeline] { (Notifications)", b);
j.assertLogNotContains("[Pipeline] { (Post Build Actions)", b);
j.assertLogContains("I HAVE FINISHED", b);
j.assertLogContains("MOST DEFINITELY FINISHED", b);
j.assertLogNotContains("I FAILED", b);
Expand All @@ -54,8 +56,10 @@ public void simplePostBuild() throws Exception {

WorkflowRun b = getAndStartBuild();
j.assertBuildStatusSuccess(j.waitForCompletion(b));
j.assertLogContains("Entering stage foo", b);
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("hello", b);
j.assertLogContains("[Pipeline] { (Post Build Actions)", b);
j.assertLogNotContains("[Pipeline] { (Notifications)", b);
j.assertLogContains("I HAVE FINISHED", b);
j.assertLogContains("MOST DEFINITELY FINISHED", b);
j.assertLogNotContains("I FAILED", b);
Expand All @@ -67,8 +71,10 @@ public void postBuildAndNotifications() throws Exception {

WorkflowRun b = getAndStartBuild();
j.assertBuildStatus(Result.FAILURE, j.waitForCompletion(b));
j.assertLogContains("Entering stage foo", b);
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("hello", b);
j.assertLogContains("[Pipeline] { (Post Build Actions)", b);
j.assertLogContains("[Pipeline] { (Notifications)", b);
j.assertLogContains("I AM FAILING", b);
j.assertLogContains("I HAVE FAILED", b);
j.assertLogNotContains("I HAVE SUCCEEDED", b);
Expand All @@ -78,21 +84,21 @@ public void postBuildAndNotifications() throws Exception {
public void notificationOnChanged() throws Exception {
WorkflowRun b = getAndStartNonRepoBuild("notificationOnChangeFailed");
j.assertBuildStatus(Result.FAILURE, j.waitForCompletion(b));
j.assertLogContains("Entering stage foo", b);
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("I FAILED", b);

WorkflowJob job = b.getParent();
job.setDefinition(new CpsFlowDefinition(pipelineSourceFromResources("notificationOnChangeChanged")));
WorkflowRun b2 = job.scheduleBuild2(0).waitForStart();
j.assertBuildStatusSuccess(j.waitForCompletion(b2));
j.assertLogContains("Entering stage foo", b2);
j.assertLogContains("[Pipeline] { (foo)", b2);
j.assertLogContains("hello", b2);
j.assertLogContains("I CHANGED", b2);

// Now make sure we don't get any alert this time.
WorkflowRun b3 = job.scheduleBuild2(0).waitForStart();
j.assertBuildStatusSuccess(j.waitForCompletion(b3));
j.assertLogContains("Entering stage foo", b3);
j.assertLogContains("[Pipeline] { (foo)", b3);
j.assertLogContains("hello", b3);
j.assertLogNotContains("I CHANGED", b3);
j.assertLogNotContains("I FAILED", b3);
Expand All @@ -105,7 +111,7 @@ public void unstableNotification() throws Exception {
WorkflowRun b = getAndStartBuild();

j.assertBuildStatus(Result.UNSTABLE, j.waitForCompletion(b));
j.assertLogContains("Entering stage foo", b);
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("hello", b);
j.assertLogContains("I AM UNSTABLE", b);
j.assertLogNotContains("I FAILED", b);
Expand All @@ -120,7 +126,7 @@ public void abortedNotification() throws Exception {
b.getExecutor().interrupt();
j.assertBuildStatus(Result.ABORTED, j.waitForCompletion(b));

j.assertLogContains("Entering stage foo", b);
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("hello", b);
j.assertLogContains("I ABORTED", b);
j.assertLogNotContains("I FAILED", b);
Expand Down
Expand Up @@ -41,7 +41,7 @@ public void simpleEnvironment() throws Exception {

WorkflowRun b = getAndStartBuild();
j.assertBuildStatusSuccess(j.waitForCompletion(b));
j.assertLogContains("Entering stage foo", b);
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("FOO is BAR", b);
}

Expand Down
Expand Up @@ -39,7 +39,7 @@ public void simpleScript() throws Exception {

WorkflowRun b = getAndStartBuild();
j.assertBuildStatusSuccess(j.waitForCompletion(b));
j.assertLogContains("Entering stage foo", b);
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("In a script step", b);
}

Expand All @@ -57,7 +57,7 @@ public void dockerGlobalVariableInScript() throws Exception {

WorkflowRun b = getAndStartBuild();
j.assertBuildStatusSuccess(j.waitForCompletion(b));
j.assertLogContains("Entering stage foo", b);
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("image: ubuntu", b);
}
}
Expand Up @@ -49,7 +49,7 @@ public void simpleTools() throws Exception {

WorkflowRun b = getAndStartBuild();
j.assertBuildStatusSuccess(j.waitForCompletion(b));
j.assertLogContains("Entering stage foo", b);
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("Apache Maven 3.0.1", b);
}

Expand All @@ -72,7 +72,7 @@ public void buildPluginParentPOM() throws Exception {

WorkflowRun b = getAndStartBuild();
j.assertBuildStatusSuccess(j.waitForCompletion(b));
j.assertLogContains("Entering stage build", b);
j.assertLogContains("[Pipeline] { (build)", b);
j.assertLogContains("[INFO] BUILD SUCCESS", b);
j.assertLogContains("M2_HOME: " + m3.getHome(), b);
j.assertLogContains("JAVA_HOME: " + thisJdk.getHome(), b);
Expand Down

0 comments on commit 04ad0c2

Please sign in to comment.