Skip to content

Commit

Permalink
Backport of PR 2187 in branch 1.24.x (#2188)
Browse files Browse the repository at this point in the history
* [JENKINS-60995] Fix downstream build link

Downstream of jenkinsci/pipeline-build-step-plugin#54

* Update to use pipeline-build-step 2.14-rc430.69345dedbb03
* fix ATH test
* use pipeline-build-step 1.24
* fix some upperBound

Signed-off-by: Olivier Lamy <[email protected]>

Co-authored-by: Vincent Latombe <[email protected]>
Co-authored-by: Liam Newman <[email protected]>
  • Loading branch information
3 people authored Jul 20, 2021
1 parent 7a7070d commit 42acd6f
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 38 deletions.
5 changes: 5 additions & 0 deletions acceptance-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
<artifactId>junit</artifactId>
<version>4.13.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion acceptance-tests/runner/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
import io.blueocean.ath.factory.FreestyleJobFactory;
import io.blueocean.ath.model.ClassicPipeline;
import io.blueocean.ath.sse.SSEClientRule;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;

@RunWith(ATHJUnitRunner.class)
Expand All @@ -25,21 +31,48 @@ public class DownstreamLinkTest extends BlueOceanAcceptanceTest implements WebDr
@Rule
public SSEClientRule sseClient;

@Rule
public TestName testName = new TestName();

@Test
public void downstreamJobLinkAppearsInRunResult() throws Exception {

final String upstreamJobScript = "stage ('stageName') { build 'downstreamJob' }";
ClassicPipeline upstreamJob = pipelineFactory.pipeline("upstreamJob").createPipeline(upstreamJobScript);
ClassicPipeline upstreamJob = pipelineFactory.pipeline("upstreamJob").createPipeline(getJobScript("upstream"));
freestyleJobFactory.pipeline("downstreamJob").create("echo blah");

upstreamJob.build();
sseClient.untilEvents(upstreamJob.buildsFinished);
sseClient.clear();

upstreamJob.getRunDetailsPipelinePage().open(1);

find("//*[contains(text(),'Triggered Builds')]").isVisible(); // Heading for table of builds
find("//*[contains(text(),'downstreamJob')]").isVisible(); // row pointing to downstream build
}

@Test
@Ignore("Work in progress")
public void sequentialStages() throws Exception {

ClassicPipeline upstreamJob = pipelineFactory.pipeline("upstreamJob").createPipeline(getJobScript("upstream"));
freestyleJobFactory.pipeline("downstreamJob").create("echo blah");

upstreamJob.build();
upstreamJob.build();
upstreamJob.build();
sseClient.untilEvents(upstreamJob.buildsFinished);
sseClient.clear();

upstreamJob.getRunDetailsPipelinePage().open(1);
upstreamJob.getRunDetailsPipelinePage().open(2);
upstreamJob.getRunDetailsPipelinePage().open(3);

find("//*[contains(text(),'Triggered Builds')]").isVisible(); // Heading for table of builds
find("//*[contains(text(),'downstreamJob')]").isVisible(); // row pointing to downstream build
}

private String getJobScript(String name) throws IOException {
return IOUtils.toString(getClass().getResource(DownstreamLinkTest.class.getSimpleName() + "/" + testName.getMethodName() + "." + name + ".groovy"),
StandardCharsets.UTF_8);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
stage ('stageName') {
build 'downstreamJob'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
pipeline{
agent any
stages {
stage ('Sequential') {
stages {
stage("Sequential 1 with child in it"){
steps{
build "TestJob"
}
}
stage("Sequential 2 with child in it"){
steps{
build "TestJob"
}
}
stage("Sequential 3 with child in it using Script"){
steps{
build "TestJob"
sh "exit 1"
}
}
stage('Sequential 4 w/o childs'){
steps{
echo "E-mail"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.jenkins.blueocean.listeners;

import hudson.Extension;
import hudson.model.Cause;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.listeners.RunListener;
Expand All @@ -9,7 +10,7 @@
import org.jenkinsci.plugins.workflow.flow.FlowExecution;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.support.steps.build.BuildUpstreamNodeAction;
import org.jenkinsci.plugins.workflow.support.steps.build.BuildUpstreamCause;

import java.io.IOException;
import java.util.logging.Logger;
Expand All @@ -27,45 +28,47 @@ public class DownstreamJobListener extends RunListener<Run<?, ?>> {

@Override
public void onStarted(Run<?, ?> run, TaskListener listener) {
for (Cause cause : run.getCauses()) {
if (cause instanceof BuildUpstreamCause) {
BuildUpstreamCause buildUpstreamCause = (BuildUpstreamCause) cause;
Run triggerRun = buildUpstreamCause.getUpstreamRun();
if (triggerRun instanceof WorkflowRun) {
FlowExecution execution = ((WorkflowRun) triggerRun).getExecution();
FlowNode node;

for (BuildUpstreamNodeAction action : run.getActions(BuildUpstreamNodeAction.class)) {
Run triggerRun = Run.fromExternalizableId(action.getUpstreamRunId());
if (triggerRun instanceof WorkflowRun) {
FlowExecution execution = ((WorkflowRun) triggerRun).getExecution();
FlowNode node;

if (execution == null) {
LOGGER.warning("Could not retrieve upstream FlowExecution");
continue;
}
if (execution == null) {
LOGGER.warning("Could not retrieve upstream FlowExecution");
continue;
}

try {
node = execution.getNode(action.getUpstreamNodeId());
} catch (IOException e) {
LOGGER.warning("Could not retrieve upstream node: " + e);
continue;
}
try {
node = execution.getNode(buildUpstreamCause.getNodeId());
} catch (IOException e) {
LOGGER.warning("Could not retrieve upstream node: " + e);
continue;
}

if (node == null) {
LOGGER.warning("Could not retrieve upstream node (null)");
continue;
}
if (node == null) {
LOGGER.warning("Could not retrieve upstream node (null)");
continue;
}

// Add an action on the triggerRun node pointing to the currently executing run
String description = run.getDescription();
if (description == null) {
description = run.getFullDisplayName();
}
// Add an action on the triggerRun node pointing to the currently executing run
String description = run.getDescription();
if (description == null) {
description = run.getFullDisplayName();
}

Link link = LinkResolver.resolveLink(run);
if (link != null) {
try {
// Also add to the actual trigger node so we can find it later by step
node.addAction(new NodeDownstreamBuildAction(link, description));
node.save();
Link link = LinkResolver.resolveLink(run);
if (link != null) {
try {
// Also add to the actual trigger node so we can find it later by step
node.addAction(new NodeDownstreamBuildAction(link, description));
node.save();

} catch (IOException e) {
LOGGER.severe("Could not persist node: " + e);
} catch (IOException e) {
LOGGER.severe("Could not persist node: " + e);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2425,7 +2425,7 @@ public void graphConsistentWhileExecuting2() throws Exception {

String completeNodeNames = checkConsistencyWhileBuilding("sequential_parallel_stages_long_run_time.jenkinsfile");

assertEquals("node names", expectedNodeNames, completeNodeNames);
assertEquals("node names:" + completeNodeNames, expectedNodeNames, completeNodeNames);
}

private String checkConsistencyWhileBuilding(String jenkinsFileName) throws Exception {
Expand Down
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,30 @@
<type>pom</type>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>pipeline-build-step</artifactId>
<version>2.14</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.23</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci</groupId>
<artifactId>symbol-annotation</artifactId>
<version>1.23</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>script-security</artifactId>
<version>1.77</version>
</dependency>

<!-- Module versions -->
<dependency>
<groupId>${project.groupId}</groupId>
Expand Down

0 comments on commit 42acd6f

Please sign in to comment.