use of org.jenkinsci.plugins.workflow.job.WorkflowJob in project blueocean-plugin by jenkinsci.
the class PipelineNodeTest method KyotoNodesFailureTest2.
@Test
public void KyotoNodesFailureTest2() throws Exception {
WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
job1.setDefinition(new CpsFlowDefinition("pipeline {\n" + " agent any\n" + " stages {\n" + " stage ('Build') {\n" + "steps{\n" + " sh 'echo \"Building\"'\n" + "}\n" + " }\n" + " stage ('Test') {\n" + "steps{\n" + " sh 'echo \"Building\"'\n" + " sh 'echo2 \"Building finished\"'\n" + "}\n" + " }\n" + " stage ('Deploy') {\n" + "steps{\n" + " sh 'echo \"Building\"'\n" + "}\n" + " }\n" + " }\n" + "}\n"));
WorkflowRun b1 = job1.scheduleBuild2(0).get();
j.assertBuildStatus(Result.FAILURE, b1);
List<Map> nodes = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);
Assert.assertEquals(3, nodes.size());
Assert.assertEquals("SUCCESS", nodes.get(0).get("result"));
Assert.assertEquals("FINISHED", nodes.get(0).get("state"));
Assert.assertEquals("FAILURE", nodes.get(1).get("result"));
Assert.assertEquals("FINISHED", nodes.get(1).get("state"));
}
use of org.jenkinsci.plugins.workflow.job.WorkflowJob in project blueocean-plugin by jenkinsci.
the class PipelineRunImpl method getCommitUrl.
@Exported(name = "commitUrl")
public String getCommitUrl() {
String commitId = getCommitId();
if (commitId != null) {
Container<BlueChangeSetEntry> changeSets = getChangeSet();
int buildNumber = this.run.number;
WorkflowJob job = this.run.getParent();
while (buildNumber > 0) {
BlueChangeSetEntry entry = changeSets.get(commitId);
if (entry != null) {
return entry.getUrl();
}
buildNumber--;
Run<?, ?> run = job.getBuildByNumber(buildNumber);
if (run == null) {
continue;
}
BlueRun blueRun = BlueRunFactory.getRun(run, parent);
if (blueRun == null) {
continue;
}
changeSets = blueRun.getChangeSet();
}
}
return null;
}
use of org.jenkinsci.plugins.workflow.job.WorkflowJob in project blueocean-plugin by jenkinsci.
the class BlueMessageEnricher method maybeEnrichMessage.
private void maybeEnrichMessage(@Nonnull Message message) {
String channelName = message.getChannelName();
if (channelName.equals(Events.JobChannel.NAME) && message instanceof JobChannelMessage) {
JobChannelMessage jobChannelMessage = (JobChannelMessage) message;
Item jobChannelItem = jobChannelMessage.getJobChannelItem();
if (jobChannelItem == null) {
return;
}
Link jobUrl = LinkResolver.resolveLink(jobChannelItem);
if (jobUrl == null) {
return;
}
BlueOrganization org = OrganizationFactory.getInstance().getContainingOrg(jobChannelItem);
if (org != null) {
message.set(EventProps.Jenkins.jenkins_org, org.getName());
}
jobChannelMessage.set(BlueEventProps.blueocean_job_rest_url, jobUrl.getHref());
jobChannelMessage.set(BlueEventProps.blueocean_job_pipeline_name, AbstractPipelineImpl.getFullName(org, jobChannelItem));
if (jobChannelItem instanceof WorkflowJob) {
ItemGroup<? extends Item> parent = jobChannelItem.getParent();
if (parent instanceof WorkflowMultiBranchProject) {
String multiBranchProjectName = AbstractPipelineImpl.getFullName(org, (WorkflowMultiBranchProject) parent);
jobChannelMessage.set(BlueEventProps.blueocean_job_pipeline_name, multiBranchProjectName);
jobChannelMessage.set(BlueEventProps.blueocean_job_branch_name, jobChannelItem.getName());
}
}
if (message.containsKey("job_run_queueId") && jobChannelItem instanceof hudson.model.Job) {
String queueIdStr = message.get("job_run_queueId");
if (queueIdStr == null) {
return;
}
final long queueId = Long.parseLong(queueIdStr);
Queue.Item queueItem = Jenkins.getInstance().getQueue().getItem(queueId);
if (queueItem == null) {
return;
}
hudson.model.Job job = (hudson.model.Job) jobChannelItem;
BlueQueueItem blueQueueItem = QueueUtil.getQueuedItem(null, queueItem, job);
if (blueQueueItem != null) {
jobChannelMessage.set(BlueEventProps.blueocean_queue_item_expected_build_number, Integer.toString(blueQueueItem.getExpectedBuildNumber()));
} else {
// If build is already running, we simply take the run id and pass it on
if (message.get("job_run_status") != null) {
String buildNumberStr = message.get("jenkins_object_id");
if (StringUtils.isNotBlank(buildNumberStr)) {
jobChannelMessage.set(BlueEventProps.blueocean_queue_item_expected_build_number, message.get("jenkins_object_id"));
}
}
}
}
}
}
use of org.jenkinsci.plugins.workflow.job.WorkflowJob in project blueocean-plugin by jenkinsci.
the class SseEventTest method multiBranchJobEvents.
@Test
public void multiBranchJobEvents() throws Exception {
setupScm();
final OneShotEvent success = new OneShotEvent();
// index: 0 QUEUED, index 1: ALLOCATED, index: 2 INDEXING, index: 3 COMPLETED
final boolean[] mbpStatus = { false, false, false, false };
// index: 0 created, index: 1 QUEUED, index 2: ALLOCATED, index: 3 RUNNING, index: 4 SUCCESS
final boolean[] masterBranchStatus = { false, false, false, false, false };
final boolean[] feature1BranchStatus = { false, false, false, false, false };
final boolean[] masterBranchPipelineEvent = { false };
final boolean[] feature1BranchPipelineEvent = { false };
final boolean[] masterBranchNodeBlockEvent = { false };
final boolean[] feature1BranchNodeBlockEvent = { false };
final AssertionHelper assertionHelper = new AssertionHelper();
SSEConnection con = new SSEConnection(j.getURL(), "me", new ChannelSubscriber() {
@Override
public void onMessage(@Nonnull Message message) {
System.out.println(message);
if ("job".equals(message.get(jenkins_channel))) {
if ("org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject".equals(message.get(jenkins_object_type))) {
assertionHelper.isEquals("/blue/rest/organizations/jenkins/pipelines/pipeline1/", message.get(blueocean_job_rest_url));
// assertEquals("/blue/rest/organizations/jenkins/pipelines/pipeline1/", message.get(blueocean_job_rest_url));
assertionHelper.isEquals("pipeline1", message.get(blueocean_job_pipeline_name));
assertionHelper.isEquals("job", message.get(jenkins_channel));
assertionHelper.isEquals("jenkins", message.get(jenkins_org));
assertionHelper.isEquals("true", message.get(job_ismultibranch));
assertionHelper.isEquals("pipeline1", message.get(job_name));
assertionHelper.isNull(message.get(job_orgfolder_indexing_status));
assertionHelper.isNull(message.get(job_orgfolder_indexing_result));
if ("QUEUED".equals(message.get(job_run_status))) {
// queued
mbpStatus[0] = true;
}
if ("ALLOCATED".equals(message.get(job_run_status))) {
// allocated or left queue
mbpStatus[1] = true;
}
if ("INDEXING".equals(message.get(job_multibranch_indexing_status))) {
// indexing started
mbpStatus[2] = true;
assertionHelper.isNotNull(message.get(job_run_queueId));
}
if ("SUCCESS".equals(message.get(job_multibranch_indexing_result)) || ("COMPLETED".equals(message.get(job_multibranch_indexing_status)) && "job_run_queue_task_complete".equals(message.get(jenkins_event)))) {
// indexing completed
mbpStatus[3] = true;
}
} else if ("org.jenkinsci.plugins.workflow.job.WorkflowJob".equals(message.get(jenkins_object_type))) {
assertionHelper.isEquals("pipeline1", message.get(blueocean_job_pipeline_name));
if ("pipeline1/master".equals(message.get(job_name))) {
System.out.println("job_run_status::::: " + message.get(job_run_status));
assertionHelper.isEquals("/blue/rest/organizations/jenkins/pipelines/pipeline1/branches/master/", message.get(blueocean_job_rest_url));
assertionHelper.isEquals("master", message.get(blueocean_job_branch_name));
if ("job_crud_created".equals(message.get(jenkins_event))) {
masterBranchStatus[0] = true;
}
if ("QUEUED".equals(message.get(job_run_status))) {
masterBranchStatus[1] = true;
} else if ("ALLOCATED".equals(message.get(job_run_status))) {
masterBranchStatus[2] = true;
} else if ("RUNNING".equals(message.get(job_run_status))) {
System.out.println("in master running.....");
assertionHelper.isEquals("1", message.get(blueocean_queue_item_expected_build_number));
masterBranchStatus[3] = true;
}
if ("SUCCESS".equals(message.get(job_run_status)) || "job_run_queue_task_complete".equals(message.get(jenkins_event))) {
masterBranchStatus[4] = true;
}
} else if ("pipeline1/feature%2Fux-1".equals(message.get(job_name))) {
assertionHelper.isEquals("/blue/rest/organizations/jenkins/pipelines/pipeline1/branches/feature%252Fux-1/", message.get(blueocean_job_rest_url));
assertionHelper.isEquals("feature%2Fux-1", message.get(blueocean_job_branch_name));
if ("job_crud_created".equals(message.get(jenkins_event))) {
feature1BranchStatus[0] = true;
}
System.out.println("job_run_status::::: " + message.get(job_run_status));
if ("QUEUED".equals(message.get(job_run_status))) {
feature1BranchStatus[1] = true;
} else if ("ALLOCATED".equals(message.get(job_run_status))) {
feature1BranchStatus[2] = true;
}
if ("SUCCESS".equals(message.get(job_run_status)) || "job_run_queue_task_complete".equals(message.get(jenkins_event))) {
feature1BranchStatus[4] = true;
}
}
} else if ("org.jenkinsci.plugins.workflow.job.WorkflowRun".equals(message.get(jenkins_object_type))) {
if ("pipeline1/master".equals(message.get(job_name))) {
if ("RUNNING".equals(message.get(job_run_status))) {
assertionHelper.isEquals("1", message.get(blueocean_queue_item_expected_build_number));
masterBranchStatus[3] = true;
}
} else if ("pipeline1/feature%2Fux-1".equals(message.get(job_name))) {
if ("RUNNING".equals(message.get(job_run_status))) {
assertionHelper.isEquals("1", message.get(blueocean_queue_item_expected_build_number));
feature1BranchStatus[3] = true;
}
}
}
// all completed
if (mbpStatus[3] && masterBranchStatus[4] && feature1BranchStatus[4]) {
success.signal();
}
} else if ("pipeline".equals(message.get(jenkins_channel))) {
if ("pipeline1/master".equals(message.get(pipeline_job_name))) {
assertionHelper.isEquals("1", message.get(pipeline_run_id));
masterBranchPipelineEvent[0] = true;
if ("pipeline_step".equals(message.get(jenkins_event)) && "node".equals(message.get(pipeline_step_name))) {
masterBranchNodeBlockEvent[0] = true;
}
} else if ("pipeline1/feature%2Fux-1".equals(message.get(pipeline_job_name))) {
feature1BranchPipelineEvent[0] = true;
assertionHelper.isEquals("1", message.get(pipeline_run_id));
if ("pipeline_step".equals(message.get(jenkins_event)) && "node".equals(message.get(pipeline_step_name))) {
feature1BranchNodeBlockEvent[0] = true;
}
}
if ("pipeline_stage".equals(message.get(jenkins_event))) {
assertionHelper.isNotNull(message.get(pipeline_step_stage_name));
assertionHelper.isEquals("build", message.get(pipeline_step_stage_name));
assertionHelper.isNotNull(message.get(pipeline_step_flownode_id));
} else if ("pipeline_step".equals(message.get(jenkins_event))) {
assertionHelper.isNotNull(message.get(pipeline_step_flownode_id));
assertionHelper.isEquals("false", message.get(pipeline_step_is_paused));
}
}
}
});
con.subscribe("pipeline");
con.subscribe("job");
final WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "pipeline1");
mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
for (SCMSource source : mp.getSCMSources()) {
assertEquals(mp, source.getOwner());
}
mp.scheduleBuild2(0).getFuture().get();
WorkflowJob p = mp.getItem("master");
if (p == null) {
mp.getIndexing().writeWholeLogTo(System.out);
fail("master project not found");
}
j.waitUntilNoActivity();
WorkflowRun b1 = p.getLastBuild();
assertEquals(1, b1.getNumber());
assertEquals(2, mp.getItems().size());
success.block(5000);
con.close();
if (success.isSignaled()) {
assertTrue(masterBranchPipelineEvent[0]);
assertTrue(feature1BranchPipelineEvent[0]);
assertTrue(masterBranchNodeBlockEvent[0]);
assertTrue(feature1BranchNodeBlockEvent[0]);
assertArrayEquals(mbpStatus, new boolean[] { true, true, true, true });
assertArrayEquals(masterBranchStatus, new boolean[] { true, true, true, true, true });
assertArrayEquals(feature1BranchStatus, new boolean[] { true, true, true, true, true });
}
if (assertionHelper.totalErrors() > 0) {
fail("There were errors: " + assertionHelper.totalErrors());
}
}
use of org.jenkinsci.plugins.workflow.job.WorkflowJob in project blueocean-plugin by jenkinsci.
the class SseEventTest method pipelineWithInput.
@Test
public void pipelineWithInput() throws IOException, ExecutionException, InterruptedException, TimeoutException {
final OneShotEvent success = new OneShotEvent();
String script = "node {\n" + " stage(\"build\"){\n" + " echo \"running\"\n" + " input message: 'Please input branch to test against', parameters: [[$class: 'StringParameterDefinition', defaultValue: 'master', description: '', name: 'branch']]\n" + " }\n" + "}";
final boolean[] wasPaused = { false };
final boolean[] wasUnPaused = { false };
final AssertionHelper assertionHelper = new AssertionHelper();
SSEConnection con = new SSEConnection(j.getURL(), "me", new ChannelSubscriber() {
@Override
public void onMessage(@Nonnull Message message) {
System.out.println(message);
if ("job".equals(message.get(jenkins_channel))) {
assertionHelper.isEquals("/blue/rest/organizations/jenkins/pipelines/pipeline1/", message.get(blueocean_job_rest_url));
assertionHelper.isEquals("pipeline1", message.get(blueocean_job_pipeline_name));
if (message.get(jenkins_event).equals(Events.JobChannel.job_run_queue_left.name())) {
assertionHelper.isEquals("1", message.get(blueocean_queue_item_expected_build_number));
assertionHelper.isNotNull(message.get(Job.job_run_queueId));
assertionHelper.isNotNull(message.get(Job.job_run_status));
}
assertionHelper.isEquals("pipeline1", message.get(job_name));
assertionHelper.isEquals("job", message.get(jenkins_channel));
assertionHelper.isEquals("jenkins", message.get(jenkins_org));
assertionHelper.isNull(message.get(job_ismultibranch));
assertionHelper.isNull(message.get(job_multibranch_indexing_result));
assertionHelper.isNull(message.get(job_multibranch_indexing_status));
if ("job_run_unpaused".equals(message.get(jenkins_event))) {
wasUnPaused[0] = true;
}
} else if ("pipeline".equals(message.get(jenkins_channel))) {
assertionHelper.isEquals("1", message.get(pipeline_run_id));
if (message.get(jenkins_event).equals(pipeline_stage.name())) {
assertionHelper.isEquals("build", message.get(pipeline_step_stage_name));
}
if ("input".equals(message.get(pipeline_step_name))) {
wasPaused[0] = true;
assertionHelper.isEquals("true", message.get(pipeline_step_is_paused));
}
}
if (wasPaused[0] && wasUnPaused[0]) {
// signal finish only when both conditions are met
success.signal();
}
}
});
con.subscribe("pipeline");
con.subscribe("job");
WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
job1.setDefinition(new CpsFlowDefinition(script, false));
QueueTaskFuture<WorkflowRun> buildTask = job1.scheduleBuild2(0);
WorkflowRun run = buildTask.getStartCondition().get();
CpsFlowExecution e = (CpsFlowExecution) run.getExecutionPromise().get();
while (run.getAction(InputAction.class) == null) {
e.waitForSuspension();
}
// Now that flow is paused, send a signal that it's un-paused
ExtensionList<PipelineEventListener.InputStepPublisher> inputStepPublisherList = ExtensionList.lookup(PipelineEventListener.InputStepPublisher.class);
assertFalse(inputStepPublisherList.isEmpty());
InputAction inputAction = run.getAction(InputAction.class);
List<InputStepExecution> executionList = inputAction.getExecutions();
assertFalse(executionList.isEmpty());
InputStep inputStep = executionList.get(0).getInput();
inputStepPublisherList.get(0).onStepContinue(inputStep, run);
success.block(5000);
con.close();
if (success.isSignaled()) {
assertTrue(wasPaused[0]);
assertTrue(wasUnPaused[0]);
}
if (assertionHelper.totalErrors() > 0) {
fail("There were errors: " + assertionHelper.totalErrors());
}
}
Aggregations