Search in sources :

Example 36 with WorkflowRun

use of org.jenkinsci.plugins.workflow.job.WorkflowRun in project blueocean-plugin by jenkinsci.

the class PipelineNodeUtil method getCauseOfBlockage.

/**
     *  Gives cause of block for declarative style plugin where agent (node block) is declared inside a stage.
     *  <pre>
     *    pipeline {
     *      agent none
     *      stages {
     *          stage ('first') {
     *              agent {
     *                  label 'first'
     *              }
     *              steps{
     *                  sh 'echo "from first"'
     *              }
     *          }
     *      }
     *    }
     *  </pre>
     *
     * @param stage stage's {@link FlowNode}
     * @param nodeBlock agent or node block's {@link FlowNode}
     * @param run {@link WorkflowRun} instance
     * @return cause of block if present, nul otherwise
     * @throws IOException in case of IOException
     * @throws InterruptedException in case of Interrupted exception
     */
@CheckForNull
public static String getCauseOfBlockage(@Nonnull FlowNode stage, @Nullable FlowNode nodeBlock, @Nonnull WorkflowRun run) throws IOException, InterruptedException {
    if (nodeBlock != null) {
        //Check and see if this node block is inside this stage
        for (FlowNode p : nodeBlock.getParents()) {
            if (p.equals(stage)) {
                //see if there is blocked item in queue
                for (Queue.Item i : Jenkins.getInstance().getQueue().getItems()) {
                    if (i.task instanceof ExecutorStepExecution.PlaceholderTask) {
                        ExecutorStepExecution.PlaceholderTask task = (ExecutorStepExecution.PlaceholderTask) i.task;
                        String cause = i.getCauseOfBlockage().getShortDescription();
                        if (task.getCauseOfBlockage() != null) {
                            cause = task.getCauseOfBlockage().getShortDescription();
                        }
                        Run r = task.runForDisplay();
                        //Set cause if its there and run and node block in the queue is same as the one we
                        if (cause != null && r != null && r.equals(run) && task.getNode() != null && task.getNode().equals(nodeBlock)) {
                            return cause;
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : ExecutorStepExecution(org.jenkinsci.plugins.workflow.support.steps.ExecutorStepExecution) BlueRun(io.jenkins.blueocean.rest.model.BlueRun) Run(hudson.model.Run) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Queue(hudson.model.Queue) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) CheckForNull(javax.annotation.CheckForNull)

Example 37 with WorkflowRun

use of org.jenkinsci.plugins.workflow.job.WorkflowRun in project blueocean-plugin by jenkinsci.

the class MultiBranchTest method getMultiBranchPipelineRunStages.

@Test
public void getMultiBranchPipelineRunStages() throws Exception {
    WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
    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());
    }
    WorkflowJob p = scheduleAndFindBranchProject(mp, "master");
    j.waitUntilNoActivity();
    WorkflowRun b1 = p.getLastBuild();
    assertEquals(1, b1.getNumber());
    assertEquals(3, mp.getItems().size());
    j.waitForCompletion(b1);
    List<Map> nodes = get("/organizations/jenkins/pipelines/p/branches/master/runs/1/nodes", List.class);
    Assert.assertEquals(3, nodes.size());
}
Also used : WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) GitSCMSource(jenkins.plugins.git.GitSCMSource) SCMSource(jenkins.scm.api.SCMSource) GitSCMSource(jenkins.plugins.git.GitSCMSource) BranchSource(jenkins.branch.BranchSource) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) DefaultBranchPropertyStrategy(jenkins.branch.DefaultBranchPropertyStrategy) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Example 38 with WorkflowRun

use of org.jenkinsci.plugins.workflow.job.WorkflowRun in project blueocean-plugin by jenkinsci.

the class PipelineNodeTest method BlockStageNodesFailureTest1.

@Test
public void BlockStageNodesFailureTest1() throws Exception {
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition("node{\n" + "    stage ('Build') {\n" + "            sh 'echo1 \"Building\"'\n" + "    }\n" + "    stage ('Test') {\n" + "            sh 'echo testing'\n" + "    }\n" + "    stage ('Deploy') {\n" + "            sh 'echo deploy'\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(1, nodes.size());
    Assert.assertEquals("FAILURE", nodes.get(0).get("result"));
    Assert.assertEquals("FINISHED", nodes.get(0).get("state"));
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Example 39 with WorkflowRun

use of org.jenkinsci.plugins.workflow.job.WorkflowRun in project blueocean-plugin by jenkinsci.

the class PipelineNodeTest method BlockStageNodesFailureTest3.

@Test
public void BlockStageNodesFailureTest3() throws Exception {
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition("node{\n" + "    stage ('Build') {\n" + "            sh 'echo \"Building\"'\n" + "    }\n" + "    stage ('Test') {\n" + "            sh 'echo testing'\n" + "    }\n" + "    stage ('Deploy') {\n" + "            sh 'echo1 deploy'\n" + "    }\n" + "}"));
    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatus(Result.FAILURE, b1);
    get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);
    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("SUCCESS", nodes.get(1).get("result"));
    Assert.assertEquals("FINISHED", nodes.get(1).get("state"));
    Assert.assertEquals("FAILURE", nodes.get(2).get("result"));
    Assert.assertEquals("FINISHED", nodes.get(2).get("state"));
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Example 40 with WorkflowRun

use of org.jenkinsci.plugins.workflow.job.WorkflowRun in project blueocean-plugin by jenkinsci.

the class PipelineNodeTest method nodesFailureTest.

@Test
public void nodesFailureTest() throws Exception {
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition("stage \"Build\"\n" + "    node {\n" + "       sh \"echo here\"\n" + "    }\n" + "\n" + "stage \"Test\"\n" + "    parallel (\n" + "        \"Firefox\" : {\n" + "            node {\n" + "                sh \"echo ffox\"\n" + "            }\n" + "        },\n" + "        \"Chrome\" : {\n" + "            node {\n" + "                sh \"echo chrome\"\n" + "            }\n" + "        }\n" + "    )\n" + "\n" + "stage \"CrashyMcgee\"\n" + "  parallel (\n" + "    \"SlowButSuccess\" : {\n" + "        node {\n" + "            echo 'This is time well spent.'\n" + "        }\n" + "    },\n" + "    \"DelayThenFail\" : {\n" + "        node {\n" + "            echo 'Not yet.'\n" + "        }\n" + "    }\n" + "  )\n" + "\n" + "\n" + "stage \"Deploy\"\n" + "    node {\n" + "        sh \"echo deploying\"\n" + "    }"));
    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatusSuccess(b1);
    job1.setDefinition(new CpsFlowDefinition("throw stage \"Build\"\n" + "    node {\n" + "       sh \"echo here\"\n" + "    }\n" + "\n" + "stage \"Test\"\n" + "    parallel (\n" + "        \"Firefox\" : {\n" + "            node {\n" + "                sh \"echo ffox\"\n" + "            }\n" + "        },\n" + "        \"Chrome\" : {\n" + "            node {\n" + "                sh \"echo chrome\"\n" + "            }\n" + "        }\n" + "    )\n" + "\n" + "stage \"CrashyMcgee\"\n" + "  parallel (\n" + "    \"SlowButSuccess\" : {\n" + "        node {\n" + "            echo 'This is time well spent.'\n" + "        }\n" + "    },\n" + "    \"DelayThenFail\" : {\n" + "        node {\n" + "            echo 'Not yet.'\n" + "        }\n" + "    }\n" + "  )\n" + "\n" + "\n" + "stage \"Deploy\"\n" + "    node {\n" + "        sh \"echo deploying\"\n" + "    }"));
    job1.scheduleBuild2(0);
    WorkflowRun b2 = job1.scheduleBuild2(0).get();
    j.assertBuildStatus(Result.FAILURE, b2);
    List<Map> resp = get("/organizations/jenkins/pipelines/pipeline1/runs/2/nodes/", List.class);
    Assert.assertEquals(8, resp.size());
    for (int i = 0; i < resp.size(); i++) {
        Map rn = resp.get(i);
        List<Map> edges = (List<Map>) rn.get("edges");
        if (rn.get("displayName").equals("Test")) {
            Assert.assertEquals(2, edges.size());
            Assert.assertNull(rn.get("result"));
            Assert.assertNull(rn.get("state"));
        } else if (rn.get("displayName").equals("Firefox")) {
            Assert.assertEquals(1, edges.size());
            Assert.assertNull(rn.get("result"));
            Assert.assertNull(rn.get("state"));
        } else if (rn.get("displayName").equals("Chrome")) {
            Assert.assertEquals(1, edges.size());
            Assert.assertNull(rn.get("result"));
            Assert.assertNull(rn.get("state"));
        } else if (rn.get("displayName").equals("CrashyMcgee")) {
            Assert.assertEquals(2, edges.size());
            Assert.assertNull(rn.get("result"));
            Assert.assertNull(rn.get("state"));
        } else if (rn.get("displayName").equals("SlowButSuccess")) {
            Assert.assertEquals(1, edges.size());
            Assert.assertNull(rn.get("result"));
            Assert.assertNull(rn.get("state"));
        } else if (rn.get("displayName").equals("DelayThenFail")) {
            Assert.assertEquals(1, edges.size());
            Assert.assertNull(rn.get("result"));
            Assert.assertNull(rn.get("state"));
        } else if (rn.get("displayName").equals("build")) {
            Assert.assertEquals(1, edges.size());
            Assert.assertNull(rn.get("result"));
            Assert.assertNull(rn.get("state"));
        } else if (rn.get("displayName").equals("Deploy")) {
            Assert.assertEquals(0, edges.size());
            Assert.assertNull(rn.get("result"));
            Assert.assertNull(rn.get("state"));
        }
    }
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) RunList(hudson.util.RunList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Aggregations

WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)77 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)72 Test (org.junit.Test)70 Map (java.util.Map)58 CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)53 ImmutableMap (com.google.common.collect.ImmutableMap)45 List (java.util.List)22 ImmutableList (com.google.common.collect.ImmutableList)20 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)19 RunList (hudson.util.RunList)18 BranchSource (jenkins.branch.BranchSource)12 GitSCMSource (jenkins.plugins.git.GitSCMSource)12 WorkflowMultiBranchProject (org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject)12 SCMSource (jenkins.scm.api.SCMSource)11 DefaultBranchPropertyStrategy (jenkins.branch.DefaultBranchPropertyStrategy)8 CpsFlowExecution (org.jenkinsci.plugins.workflow.cps.CpsFlowExecution)7 InputAction (org.jenkinsci.plugins.workflow.support.steps.input.InputAction)6 Issue (org.jvnet.hudson.test.Issue)6 Run (hudson.model.Run)4 JSONObject (net.sf.json.JSONObject)4