Java Code Examples for org.jenkinsci.plugins.workflow.job.WorkflowJob#getLastBuild()

The following examples show how to use org.jenkinsci.plugins.workflow.job.WorkflowJob#getLastBuild() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultsBinderTest.java    From pipeline-multibranch-defaults-plugin with MIT License 6 votes vote down vote up
@Test
public void testDefaultJenkinsFileLoadFromWorkspace() throws Exception {
    GlobalConfigFiles globalConfigFiles = r.jenkins.getExtensionList(GlobalConfigFiles.class).get(GlobalConfigFiles.class);
    ConfigFileStore store = globalConfigFiles.get();
    Config config = new GroovyScript("Jenkinsfile", "Jenkinsfile", "",
        "semaphore 'wait'; node {checkout scm; load 'Jenkinsfile'}");
    store.save(config);


    sampleGitRepo.init();
    sampleGitRepo.write("Jenkinsfile", "echo readFile('file')");
    sampleGitRepo.git("add", "Jenkinsfile");
    sampleGitRepo.write("file", "initial content");
    sampleGitRepo.git("commit", "--all", "--message=flow");
    WorkflowMultiBranchProject mp = r.jenkins.createProject(PipelineMultiBranchDefaultsProject.class, "p");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleGitRepo.toString(), "", "*", "", false),
        new DefaultBranchPropertyStrategy(new BranchProperty[0])));
    WorkflowJob p = PipelineMultiBranchDefaultsProjectTest.scheduleAndFindBranchProject(mp, "master");
    SemaphoreStep.waitForStart("wait/1", null);
    WorkflowRun b1 = p.getLastBuild();
    assertNotNull(b1);
    assertEquals(1, b1.getNumber());
    SemaphoreStep.success("wait/1", null);
    r.assertLogContains("initial content", r.waitForCompletion(b1));
}
 
Example 2
Source File: MultiBranchTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@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());
}
 
Example 3
Source File: DefaultsBinderTest.java    From pipeline-multibranch-defaults-plugin with MIT License 6 votes vote down vote up
@Test
public void testDefaultJenkinsFile() throws Exception {
    GlobalConfigFiles globalConfigFiles = r.jenkins.getExtensionList(GlobalConfigFiles.class).get(GlobalConfigFiles.class);
    ConfigFileStore store = globalConfigFiles.get();

    Config config = new GroovyScript("Jenkinsfile", "Jenkinsfile", "",
        "semaphore 'wait'; node {checkout scm; echo readFile('file')}");
    store.save(config);

    sampleGitRepo.init();
    sampleGitRepo.write("file", "initial content");
    sampleGitRepo.git("commit", "--all", "--message=flow");
    WorkflowMultiBranchProject mp = r.jenkins.createProject(PipelineMultiBranchDefaultsProject.class, "p");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleGitRepo.toString(), "", "*", "", false),
        new DefaultBranchPropertyStrategy(new BranchProperty[0])));
    WorkflowJob p = PipelineMultiBranchDefaultsProjectTest.scheduleAndFindBranchProject(mp, "master");
    SemaphoreStep.waitForStart("wait/1", null);
    WorkflowRun b1 = p.getLastBuild();
    assertNotNull(b1);
    assertEquals(1, b1.getNumber());
    SemaphoreStep.success("wait/1", null);
    r.assertLogContains("initial content", r.waitForCompletion(b1));
}
 
Example 4
Source File: WorkflowITest.java    From github-integration-plugin with MIT License 5 votes vote down vote up
@Test
public void testContextStatuses() throws Exception {
    final WorkflowJob workflowJob = jRule.jenkins.createProject(WorkflowJob.class, "testContextStatuses");

    workflowJob.addProperty(getPreconfiguredProperty(ghRule.getGhRepo()));
    workflowJob.addTrigger(getPreconfiguredPRTrigger());

    workflowJob.setDefinition(
            new CpsFlowDefinition(classpath(this.getClass(), "testContextStatuses.groovy"), false)
    );
    workflowJob.save();

    basicTest(workflowJob);

    GHPullRequest pullRequest = ghRule.getGhRepo().getPullRequest(1);
    assertThat(pullRequest, notNullValue());

    WorkflowRun lastBuild = workflowJob.getLastBuild();
    assertThat(lastBuild, notNullValue());

    GitHubPRCause cause = lastBuild.getCause(GitHubPRCause.class);
    assertThat(cause, notNullValue());

    List<GHCommitStatus> statuses = pullRequest.getRepository()
            .listCommitStatuses(cause.getHeadSha())
            .asList();
    assertThat(statuses, hasSize(7));

    // final statuses
    assertThat("workflow Run.getResult() strange",
            statuses,
            hasItem(commitStatus("testContextStatuses", GHCommitState.ERROR, "Run #2 ended normally"))
    );
    assertThat(statuses, hasItem(commitStatus("custom-context1", GHCommitState.SUCCESS, "Tests passed")));
    assertThat(statuses, hasItem(commitStatus("custom-context2", GHCommitState.SUCCESS, "Tests passed")));
}
 
Example 5
Source File: PipelineNodeTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Test
@Issue("JENKINS-38339")
public void downstreamBuildLinks() throws Exception {
    FreeStyleProject downstream1 = j.createFreeStyleProject("downstream1");
    FreeStyleProject downstream2 = j.createFreeStyleProject("downstream2");

    WorkflowJob upstream = j.createProject(WorkflowJob.class, "upstream");

    URL resource = Resources.getResource(getClass(), "downstreamBuildLinks.jenkinsfile");
    String jenkinsFile = Resources.toString(resource, Charsets.UTF_8);
    upstream.setDefinition(new CpsFlowDefinition(jenkinsFile, true));

    j.assertBuildStatus(Result.SUCCESS, upstream.scheduleBuild2(0));

    WorkflowRun r = upstream.getLastBuild();

    List<Map> resp = get("/organizations/jenkins/pipelines/upstream/runs/" + r.getId() + "/nodes/", List.class);

    assertEquals("number of nodes", 5, resp.size());

    Matcher actionMatcher1 = new NodeDownstreamBuildActionMatcher("downstream1");
    Matcher actionMatcher2 = new NodeDownstreamBuildActionMatcher("downstream2");

    List<Map> actions = (List<Map>) resp.get(2).get("actions");
    assertThat("node #2 contains a link to downstream1", actions, hasItem(actionMatcher1));

    actions = (List<Map>) resp.get(3).get("actions");
    assertThat("node #3 contains a link to downstream2", actions, hasItem(actionMatcher2));

    actions = (List<Map>) resp.get(4).get("actions");
    assertThat("node #4 contains a link to downstream1", actions, hasItem(actionMatcher1));
    assertThat("node #4 contains a link to downstream1", actions, hasItem(actionMatcher2));
}
 
Example 6
Source File: PipelineNodeTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Test
@Issue("JENKINS-38339")
public void downstreamBuildLinksDeclarative() throws Exception {
    FreeStyleProject downstream1 = j.createFreeStyleProject("downstream1");
    FreeStyleProject downstream2 = j.createFreeStyleProject("downstream2");

    WorkflowJob upstream = j.createProject(WorkflowJob.class, "upstream");

    URL resource = Resources.getResource(getClass(), "downstreamBuildLinksDecl.jenkinsfile");
    String jenkinsFile = Resources.toString(resource, Charsets.UTF_8);
    upstream.setDefinition(new CpsFlowDefinition(jenkinsFile, true));

    j.assertBuildStatus(Result.SUCCESS, upstream.scheduleBuild2(0));

    WorkflowRun r = upstream.getLastBuild();

    List<Map> resp = get("/organizations/jenkins/pipelines/upstream/runs/" + r.getId() + "/nodes/", List.class);

    assertEquals("number of nodes", 5, resp.size());

    Matcher actionMatcher1 = new NodeDownstreamBuildActionMatcher("downstream1");
    Matcher actionMatcher2 = new NodeDownstreamBuildActionMatcher("downstream2");

    List<Map> actions = (List<Map>) resp.get(2).get("actions");
    assertThat("node #2 contains a link to downstream1", actions, hasItem(actionMatcher1));

    actions = (List<Map>) resp.get(3).get("actions");
    assertThat("node #3 contains a link to downstream2", actions, hasItem(actionMatcher2));

    actions = (List<Map>) resp.get(4).get("actions");
    assertThat("node #4 contains a link to downstream1", actions, hasItem(actionMatcher1));
    assertThat("node #4 contains a link to downstream1", actions, hasItem(actionMatcher2));
}
 
Example 7
Source File: JobAnalyticsTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
private void createMultiBranch(GitSampleRepoRule rule) throws Exception {
    WorkflowMultiBranchProject mp = j.createProject(WorkflowMultiBranchProject.class, UUID.randomUUID().toString());
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, rule.toString(), "", "*", "", false),
        new DefaultBranchPropertyStrategy(new BranchProperty[0])));
    mp.save();
    mp.scheduleBuild2(0).getFuture().get();
    mp.getIndexing().getLogText().writeLogTo(0, System.out);
    j.waitUntilNoActivity();
    WorkflowJob master = mp.getItemByBranchName("master");
    assertNotNull(master);
    WorkflowRun lastBuild = master.getLastBuild();
    assertNotNull(lastBuild);
    assertEquals(Result.SUCCESS, lastBuild.getResult());
}
 
Example 8
Source File: MultiBranchTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void getMultiBranchPipelineActivityRuns() 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());
    }
    scheduleAndFindBranchProject(mp);
    j.waitUntilNoActivity();

    WorkflowJob p = findBranchProject(mp, "master");

    WorkflowRun b1 = p.getLastBuild();
    assertEquals(1, b1.getNumber());
    assertEquals(3, mp.getItems().size());

    //execute feature/ux-1 branch build
    p = findBranchProject(mp, "feature%2Fux-1");
    WorkflowRun b2 = p.getLastBuild();
    assertEquals(1, b2.getNumber());


    //execute feature 2 branch build
    p = findBranchProject(mp, "feature2");
    WorkflowRun b3 = p.getLastBuild();
    assertEquals(1, b3.getNumber());


    List<Map> resp = get("/organizations/jenkins/pipelines/p/runs", List.class);
    Assert.assertEquals(3, resp.size());
    Date d1 = new SimpleDateFormat(DATE_FORMAT_STRING).parse((String)resp.get(0).get("startTime"));
    Date d2 = new SimpleDateFormat(DATE_FORMAT_STRING).parse((String)resp.get(1).get("startTime"));
    Date d3 = new SimpleDateFormat(DATE_FORMAT_STRING).parse((String)resp.get(2).get("startTime"));

    Assert.assertTrue(d1.compareTo(d2) >= 0);
    Assert.assertTrue(d2.compareTo(d3) >= 0);

    for(Map m: resp){
        BuildData d;
        WorkflowRun r;
        if(m.get("pipeline").equals("master")){
            r = b1;
            d = b1.getAction(BuildData.class);
        } else if(m.get("pipeline").equals("feature2")){
            r = b3;
            d = b3.getAction(BuildData.class);
        } else{
            r = b2;
            d = b2.getAction(BuildData.class);
        }
        validateRun(r,m);
        String commitId = "";
        if(d != null) {
            commitId = d.getLastBuiltRevision().getSha1String();
            Assert.assertEquals(commitId, m.get("commitId"));
        }
    }
}
 
Example 9
Source File: DependencyGraphTest.java    From pipeline-maven-plugin with MIT License 4 votes vote down vote up
/**
 * NBM dependencies
 */
@Test
public void verify_nbm_downstream_simple_pipeline_trigger() throws Exception {
    System.out.println("gitRepoRule: " + gitRepoRule);
    loadNbmDependencyMavenJarProjectInGitRepo(this.gitRepoRule);
    System.out.println("downstreamArtifactRepoRule: " + downstreamArtifactRepoRule);
    loadNbmBaseMavenProjectInGitRepo(this.downstreamArtifactRepoRule);

    String mavenJarPipelineScript = "node('master') {\n"
            + "    git($/" + gitRepoRule.toString() + "/$)\n"
            + "    withMaven() {\n"
            + "        sh 'mvn install'\n"
            + "    }\n"
            + "}";
    String mavenWarPipelineScript = "node('master') {\n"
            + "    git($/" + downstreamArtifactRepoRule.toString() + "/$)\n"
            + "    withMaven() {\n"
            + "        sh 'mvn install'\n"
            + "    }\n"
            + "}";

    WorkflowJob mavenNbmDependency = jenkinsRule.createProject(WorkflowJob.class, "build-nbm-dependency");
    mavenNbmDependency.setDefinition(new CpsFlowDefinition(mavenJarPipelineScript, true));
    mavenNbmDependency.addTrigger(new WorkflowJobDependencyTrigger());

    WorkflowRun mavenJarPipelineFirstRun = jenkinsRule.assertBuildStatus(Result.SUCCESS, mavenNbmDependency.scheduleBuild2(0));
    // TODO check in DB that the generated artifact is recorded

    WorkflowJob mavenNbmBasePipeline = jenkinsRule.createProject(WorkflowJob.class, "build-nbm-base");
    mavenNbmBasePipeline.setDefinition(new CpsFlowDefinition(mavenWarPipelineScript, true));
    mavenNbmBasePipeline.addTrigger(new WorkflowJobDependencyTrigger());
    WorkflowRun mavenWarPipelineFirstRun = jenkinsRule.assertBuildStatus(Result.SUCCESS, mavenNbmBasePipeline.scheduleBuild2(0));
    // TODO check in DB that the dependency on the war project is recorded
    System.out.println("build-nbm-dependencyFirstRun: " + mavenWarPipelineFirstRun);

    WorkflowRun mavenJarPipelineSecondRun = jenkinsRule.assertBuildStatus(Result.SUCCESS, mavenNbmDependency.scheduleBuild2(0));

    jenkinsRule.waitUntilNoActivity();

    WorkflowRun mavenWarPipelineLastRun = mavenNbmBasePipeline.getLastBuild();

    System.out.println("build-nbm-baseLastBuild: " + mavenWarPipelineLastRun + " caused by " + mavenWarPipelineLastRun.getCauses());

    assertThat(mavenWarPipelineLastRun.getNumber(), is(mavenWarPipelineFirstRun.getNumber() + 1));
    Cause.UpstreamCause upstreamCause = mavenWarPipelineLastRun.getCause(Cause.UpstreamCause.class);
    assertThat(upstreamCause, notNullValue());
}
 
Example 10
Source File: DependencyGraphTest.java    From pipeline-maven-plugin with MIT License 4 votes vote down vote up
/**
 * The maven-war-app has a dependency on the maven-jar-app
 */
@Test
public void verify_downstream_pipeline_triggered_on_parent_pom_build() throws Exception {
    System.out.println("gitRepoRule: " + gitRepoRule);
    loadMavenJarProjectInGitRepo(this.gitRepoRule);
    System.out.println("downstreamArtifactRepoRule: " + downstreamArtifactRepoRule);
    loadMavenWarProjectInGitRepo(this.downstreamArtifactRepoRule);

    String mavenJarPipelineScript = "node('master') {\n" +
            "    git($/" + gitRepoRule.toString() + "/$)\n" +
            "    withMaven() {\n" +
            "        sh 'mvn install'\n" +
            "    }\n" +
            "}";
    String mavenWarPipelineScript = "node('master') {\n" +
            "    git($/" + downstreamArtifactRepoRule.toString() + "/$)\n" +
            "    withMaven() {\n" +
            "        sh 'mvn install'\n" +
            "    }\n" +
            "}";


    WorkflowJob mavenJarPipeline = jenkinsRule.createProject(WorkflowJob.class, "build-maven-jar");
    mavenJarPipeline.setDefinition(new CpsFlowDefinition(mavenJarPipelineScript, true));
    mavenJarPipeline.addTrigger(new WorkflowJobDependencyTrigger());

    WorkflowRun mavenJarPipelineFirstRun = jenkinsRule.assertBuildStatus(Result.SUCCESS, mavenJarPipeline.scheduleBuild2(0));
    // TODO check in DB that the generated artifact is recorded


    WorkflowJob mavenWarPipeline = jenkinsRule.createProject(WorkflowJob.class, "build-maven-war");
    mavenWarPipeline.setDefinition(new CpsFlowDefinition(mavenWarPipelineScript, true));
    mavenWarPipeline.addTrigger(new WorkflowJobDependencyTrigger());
    WorkflowRun mavenWarPipelineFirstRun = jenkinsRule.assertBuildStatus(Result.SUCCESS, mavenWarPipeline.scheduleBuild2(0));
    // TODO check in DB that the dependency on the war project is recorded
    System.out.println("mavenWarPipelineFirstRun: " + mavenWarPipelineFirstRun);

    WorkflowRun mavenJarPipelineSecondRun = jenkinsRule.assertBuildStatus(Result.SUCCESS, mavenJarPipeline.scheduleBuild2(0));

    jenkinsRule.waitUntilNoActivity();

    WorkflowRun mavenWarPipelineLastRun = mavenWarPipeline.getLastBuild();

    System.out.println("mavenWarPipelineLastBuild: " + mavenWarPipelineLastRun + " caused by " + mavenWarPipelineLastRun.getCauses());

    assertThat(mavenWarPipelineLastRun.getNumber(), is(mavenWarPipelineFirstRun.getNumber() + 1));
    Cause.UpstreamCause upstreamCause = mavenWarPipelineLastRun.getCause(Cause.UpstreamCause.class);
    assertThat(upstreamCause, notNullValue());


}
 
Example 11
Source File: DependencyGraphTest.java    From pipeline-maven-plugin with MIT License 4 votes vote down vote up
/**
 * The maven-war-app has a dependency on the maven-jar-app
 */
@Test
public void verify_downstream_multi_branch_pipeline_trigger() throws Exception {
    System.out.println("gitRepoRule: " + gitRepoRule);
    loadMavenJarProjectInGitRepo(this.gitRepoRule);
    System.out.println("downstreamArtifactRepoRule: " + downstreamArtifactRepoRule);
    loadMavenWarProjectInGitRepo(this.downstreamArtifactRepoRule);

    String script = "node('master') {\n" +
            "    checkout scm\n" +
            "    withMaven() {\n" +
            "        sh 'mvn install'\n" +
            "    }\n" +
            "}";
    gitRepoRule.write("Jenkinsfile", script);
    gitRepoRule.git("add", "Jenkinsfile");
    gitRepoRule.git("commit", "--message=jenkinsfile");


    downstreamArtifactRepoRule.write("Jenkinsfile", script);
    downstreamArtifactRepoRule.git("add", "Jenkinsfile");
    downstreamArtifactRepoRule.git("commit", "--message=jenkinsfile");

    // TRIGGER maven-jar#1 to record that "build-maven-jar" generates this jar and install this maven jar in the local maven repo
    WorkflowMultiBranchProject mavenJarPipeline = jenkinsRule.createProject(WorkflowMultiBranchProject.class, "build-maven-jar");
    mavenJarPipeline.addTrigger(new WorkflowJobDependencyTrigger());
    mavenJarPipeline.getSourcesList().add(new BranchSource(new GitSCMSource(null, gitRepoRule.toString(), "", "*", "", false)));
    System.out.println("trigger maven-jar#1...");
    WorkflowJob mavenJarPipelineMasterPipeline = WorkflowMultibranchProjectTestsUtils.scheduleAndFindBranchProject(mavenJarPipeline, "master");
    assertEquals(1, mavenJarPipeline.getItems().size());
    System.out.println("wait for maven-jar#1...");
    jenkinsRule.waitUntilNoActivity();

    assertThat(mavenJarPipelineMasterPipeline.getLastBuild().getNumber(), is(1));
    // TODO check in DB that the generated artifact is recorded

    // TRIGGER maven-war#1 to record that "build-maven-war" has a dependency on "build-maven-jar"
    WorkflowMultiBranchProject mavenWarPipeline = jenkinsRule.createProject(WorkflowMultiBranchProject.class, "build-maven-war");
    mavenWarPipeline.addTrigger(new WorkflowJobDependencyTrigger());
    mavenWarPipeline.getSourcesList().add(new BranchSource(new GitSCMSource(null, downstreamArtifactRepoRule.toString(), "", "*", "", false)));
    System.out.println("trigger maven-war#1...");
    WorkflowJob mavenWarPipelineMasterPipeline = WorkflowMultibranchProjectTestsUtils.scheduleAndFindBranchProject(mavenWarPipeline, "master");
    assertEquals(1, mavenWarPipeline.getItems().size());
    System.out.println("wait for maven-war#1...");
    jenkinsRule.waitUntilNoActivity();
    WorkflowRun mavenWarPipelineFirstRun = mavenWarPipelineMasterPipeline.getLastBuild();

    // TODO check in DB that the dependency on the war project is recorded


    // TRIGGER maven-jar#2 so that it triggers "maven-war" and creates maven-war#2
    System.out.println("trigger maven-jar#2...");
    Future<WorkflowRun> mavenJarPipelineMasterPipelineSecondRunFuture = mavenJarPipelineMasterPipeline.scheduleBuild2(0, new CauseAction(new Cause.RemoteCause("127.0.0.1", "junit test")));
    System.out.println("wait for maven-jar#2...");
    mavenJarPipelineMasterPipelineSecondRunFuture.get();
    jenkinsRule.waitUntilNoActivity();


    WorkflowRun mavenWarPipelineLastRun = mavenWarPipelineMasterPipeline.getLastBuild();

    System.out.println("mavenWarPipelineLastBuild: " + mavenWarPipelineLastRun + " caused by " + mavenWarPipelineLastRun.getCauses());

    assertThat(mavenWarPipelineLastRun.getNumber(), is(mavenWarPipelineFirstRun.getNumber() + 1));
    Cause.UpstreamCause upstreamCause = mavenWarPipelineLastRun.getCause(Cause.UpstreamCause.class);
    assertThat(upstreamCause, notNullValue());

}
 
Example 12
Source File: DependencyGraphTest.java    From pipeline-maven-plugin with MIT License 4 votes vote down vote up
/**
 * The maven-war-app has a dependency on the maven-jar-app
 */
@Test
public void verify_downstream_simple_pipeline_trigger() throws Exception {
    System.out.println("gitRepoRule: " + gitRepoRule);
    loadMavenJarProjectInGitRepo(this.gitRepoRule);
    System.out.println("downstreamArtifactRepoRule: " + downstreamArtifactRepoRule);
    loadMavenWarProjectInGitRepo(this.downstreamArtifactRepoRule);

    String mavenJarPipelineScript = "node('master') {\n" +
            "    git($/" + gitRepoRule.toString() + "/$)\n" +
            "    withMaven() {\n" +
            "        sh 'mvn install'\n" +
            "    }\n" +
            "}";
    String mavenWarPipelineScript = "node('master') {\n" +
            "    git($/" + downstreamArtifactRepoRule.toString() + "/$)\n" +
            "    withMaven() {\n" +
            "        sh 'mvn install'\n" +
            "    }\n" +
            "}";


    WorkflowJob mavenJarPipeline = jenkinsRule.createProject(WorkflowJob.class, "build-maven-jar");
    mavenJarPipeline.setDefinition(new CpsFlowDefinition(mavenJarPipelineScript, true));
    mavenJarPipeline.addTrigger(new WorkflowJobDependencyTrigger());

    WorkflowRun mavenJarPipelineFirstRun = jenkinsRule.assertBuildStatus(Result.SUCCESS, mavenJarPipeline.scheduleBuild2(0));
    // TODO check in DB that the generated artifact is recorded


    WorkflowJob mavenWarPipeline = jenkinsRule.createProject(WorkflowJob.class, "build-maven-war");
    mavenWarPipeline.setDefinition(new CpsFlowDefinition(mavenWarPipelineScript, true));
    mavenWarPipeline.addTrigger(new WorkflowJobDependencyTrigger());
    WorkflowRun mavenWarPipelineFirstRun = jenkinsRule.assertBuildStatus(Result.SUCCESS, mavenWarPipeline.scheduleBuild2(0));
    // TODO check in DB that the dependency on the war project is recorded
    System.out.println("mavenWarPipelineFirstRun: " + mavenWarPipelineFirstRun);

    WorkflowRun mavenJarPipelineSecondRun = jenkinsRule.assertBuildStatus(Result.SUCCESS, mavenJarPipeline.scheduleBuild2(0));

    jenkinsRule.waitUntilNoActivity();

    WorkflowRun mavenWarPipelineLastRun = mavenWarPipeline.getLastBuild();
    SqlTestsUtils.dump("select * from job_generated_artifacts", ((PipelineMavenPluginJdbcDao)GlobalPipelineMavenConfig.get().getDao()).getDataSource(), System.out);
    SqlTestsUtils.dump("select * from job_dependencies", ((PipelineMavenPluginJdbcDao)GlobalPipelineMavenConfig.get().getDao()).getDataSource(), System.out);

    System.out.println("mavenWarPipelineLastBuild: " + mavenWarPipelineLastRun + " caused by " + mavenWarPipelineLastRun.getCauses());

    assertThat(mavenWarPipelineLastRun.getNumber(), is(mavenWarPipelineFirstRun.getNumber() + 1));
    Cause.UpstreamCause upstreamCause = mavenWarPipelineLastRun.getCause(Cause.UpstreamCause.class);
    assertThat(upstreamCause, notNullValue());
}
 
Example 13
Source File: SseEventTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void multiBranchJobEventsWithCustomOrg() throws Exception {
    MockFolder folder = j.createFolder("TestOrgFolderName");
    assertNotNull(folder);

    setupScm();

    final OneShotEvent success = new OneShotEvent();

    final Boolean[] pipelineNameMatched = {null};
    final Boolean[] mbpPipelineNameMatched = {null};

    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))) {
                    if("pipeline1".equals(message.get(blueocean_job_pipeline_name))) {
                        mbpPipelineNameMatched[0] = true;
                    }else{
                        mbpPipelineNameMatched[0] = false;
                    }
                } else if ("org.jenkinsci.plugins.workflow.job.WorkflowJob".equals(message.get(jenkins_object_type))) {
                    if("pipeline1".equals(message.get(blueocean_job_pipeline_name))) {
                        pipelineNameMatched[0] = true;
                    }else {
                        pipelineNameMatched[0] = false;
                    }
                }
            }
            if(pipelineNameMatched[0] != null && mbpPipelineNameMatched[0] != null){
                success.signal();
            }
        }
    });
    con.subscribe("pipeline");
    con.subscribe("job");

    final WorkflowMultiBranchProject mp = folder.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(pipelineNameMatched[0]);
        assertTrue(mbpPipelineNameMatched[0]);
    }
}
 
Example 14
Source File: GraphBuilderTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
private WorkflowRun createAndRunJob(String jobName, String jenkinsFileName, Result expectedResult) throws Exception {
    WorkflowJob job = createJob(jobName, jenkinsFileName);
    j.assertBuildStatus(expectedResult, job.scheduleBuild2(0));
    return job.getLastBuild();
}
 
Example 15
Source File: PipelineNodeTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
@Issue("JENKINS-53900")
public void singleStageSequentialLastInParallel() throws Exception {
    final String jenkinsfile =
        "pipeline {\n" +
            "    agent any\n" +
            "    stages {\n" +
            "        stage('Alpha') {\n" +
            "            parallel {\n" +
            "                stage('Blue') {\n" +
            "                    steps {\n" +
            "                        script {\n" +
            "                            println \"XXXX\"\n" +
            "                        }\n" +
            "                    }\n" +
            "                }\n" +
            "                stage('Red') {\n" +
            "                    stages {\n" +
            "                        stage('Green') {\n" +
            "                            steps {\n" +
            "                                script {\n" +
            "                                    println \"XXXX\"\n" +
            "                                }\n" +
            "                            }\n" +
            "                        }\n" +
            "                    }\n" +
            "                }\n" +
            "            }\n" +
            "        }\n" +
            "        stage('Bravo') {\n" +
            "            steps {\n" +
            "                script {\n" +
            "                    println \"XXXX\"\n" +
            "                }\n" +
            "            }\n" +
            "        }\n" +
            "    }\n" +
            "}\n";

    WorkflowJob project1 = j.createProject(WorkflowJob.class, "project1");
    project1.setDefinition(new CpsFlowDefinition(jenkinsfile, true));

    j.assertBuildStatus(Result.SUCCESS, project1.scheduleBuild2(0));

    WorkflowRun r = project1.getLastBuild();
    List<Map> resp = get("/organizations/jenkins/pipelines/project1/runs/" + r.getId() + "/nodes/", List.class);

    assertEquals("number of nodes", 5, resp.size());

    final Map<String, Object> bravoNode = resp.get(3);
    final Map<String, Object> greenNode = resp.get(4);

    assertEquals("includes Alpha node", "Alpha", resp.get(0).get("displayName"));
    assertEquals("includes Blue node", "Blue", resp.get(1).get("displayName"));
    assertEquals("includes Red node", "Red", resp.get(2).get("displayName"));
    assertEquals("includes Bravo node", "Bravo", bravoNode.get("displayName"));
    assertEquals("includes Green node", "Green", greenNode.get("displayName"));

    String bravoId = bravoNode.get("id").toString();

    List<Map<String, Object>> greenEdges = (List<Map<String, Object>>) greenNode.get("edges");
    assertEquals("green has edges", 1, greenEdges.size());
    assertEquals("green has edge pointing to bravo", bravoId, greenEdges.get(0).get("id"));
}
 
Example 16
Source File: PipelineNodeTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
@Issue("JENKINS-38339")
public void downstreamBuildLinksSequential() throws Exception {
    FreeStyleProject downstream1 = j.createFreeStyleProject("downstream1");
    FreeStyleProject downstream2 = j.createFreeStyleProject("downstream2");
    FreeStyleProject downstream3 = j.createFreeStyleProject("downstream3");
    FreeStyleProject downstream4 = j.createFreeStyleProject("downstream4");

    WorkflowJob upstream = j.createProject(WorkflowJob.class, "upstream");

    URL resource = Resources.getResource(getClass(), "downstreamBuildLinksSeq.jenkinsfile");
    String jenkinsFile = Resources.toString(resource, Charsets.UTF_8);
    upstream.setDefinition(new CpsFlowDefinition(jenkinsFile, true));

    j.assertBuildStatus(Result.SUCCESS, upstream.scheduleBuild2(0));

    WorkflowRun r = upstream.getLastBuild();

    List<Map> resp = get("/organizations/jenkins/pipelines/upstream/runs/" + r.getId() + "/nodes/", List.class);

    assertEquals("number of nodes", 9, resp.size());

    // Find the nodes we're interested in
    Map node1 = null, node2 = null, node3 = null, node4 = null;
    for (Map node : resp) {
        String displayName = (String) node.get("displayName");
        if ("Single stage branch".equals(displayName)) {
            node1 = node;
        } else if ("Inner".equals(displayName)) {
            node2 = node;
        } else if ("build-ds3".equals(displayName)) {
            node3 = node;
        } else if ("build-ds4".equals(displayName)) {
            node4 = node;
        }
    }

    // Check they all have downstream links

    assertNotNull("missing node1", node1);
    assertThat("node1 contains a link to downstream1",
               (List<Map>) node1.get("actions"),
               hasItem(new NodeDownstreamBuildActionMatcher("downstream1")));

    assertNotNull("missing node2", node2);
    assertThat("node2 contains a link to downstream2",
               (List<Map>) node2.get("actions"),
               hasItem(new NodeDownstreamBuildActionMatcher("downstream2")));

    assertNotNull("missing node3", node3);
    assertThat("node3 contains a link to downstream3",
               (List<Map>) node3.get("actions"),
               hasItem(new NodeDownstreamBuildActionMatcher("downstream3")));

    assertNotNull("missing node4", node4);
    assertThat("node4 contains a link to downstream4",
               (List<Map>) node4.get("actions"),
               hasItem(new NodeDownstreamBuildActionMatcher("downstream4")));

}
 
Example 17
Source File: PipelineNodeTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void encodedStepDescription() throws Exception {
    setupScm("pipeline {\n" +
                 "  agent any\n" +
                 "  stages {\n" +
                 "    stage('Build') {\n" +
                 "      steps {\n" +
                 "          sh 'echo \"\\033[32m some text \\033[0m\"'    \n" +
                 "      }\n" +
                 "    }\n" +
                 "  }\n" +
                 "}");
    WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false)));
    for (SCMSource source : mp.getSCMSources()) {
        assertEquals(mp, source.getOwner());
    }

    mp.scheduleBuild2(0).getFuture().get();

    j.waitUntilNoActivity();

    WorkflowJob p = scheduleAndFindBranchProject(mp, "master");
    j.waitUntilNoActivity();
    WorkflowRun b1 = p.getLastBuild();
    Assert.assertEquals(Result.SUCCESS, b1.getResult());

    List<FlowNode> stages = getStages(NodeGraphBuilder.NodeGraphBuilderFactory.getInstance(b1));

    Assert.assertEquals(1, stages.size());

    Assert.assertEquals("Build", stages.get(0).getDisplayName());

    List<Map> resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/nodes/", List.class);
    Assert.assertEquals(1, resp.size());
    Assert.assertEquals("Build", resp.get(0).get("displayName"));

    resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/steps/", List.class);
    Assert.assertEquals(2, resp.size());

    assertNotNull(resp.get(0).get("displayName"));

    assertEquals("Shell Script", resp.get(1).get("displayName"));
    assertEquals("echo \"\u001B[32m some text \u001B[0m\"", resp.get(1).get("displayDescription"));
}
 
Example 18
Source File: PipelineNodeTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void declarativeSyntheticSkippedStage() throws Exception {

    setupScm("pipeline {\n" +
                 "    agent any\n" +
                 "    stages {\n" +
                 "        stage(\"build\") {\n" +
                 "            steps{\n" +
                 "              sh 'echo \"Start Build\"'\n" +
                 "              echo 'End Build'\n" +
                 "            }\n" +
                 "        }\n" +
                 "        stage(\"SkippedStage\") {\n" +
                 "            when {\n" +
                 "        expression {\n" +
                 "                return false\n" +
                 "        }\n" +
                 "            }\n" +
                 "            steps {\n" +
                 "                script {\n" +
                 "                    echo \"World\"\n" +
                 "                    echo \"Heal it\"\n" +
                 "                }\n" +
                 "\n" +
                 "            }\n" +
                 "        }\n" +
                 "        stage(\"deploy\") {\n" +
                 "            steps{\n" +
                 "              sh 'echo \"Start Deploy\"'\n" +
                 "              sh 'echo \"Deploying...\"'\n" +
                 "              sh 'echo \"End Deploy\"'\n" +
                 "            }           \n" +
                 "        }\n" +
                 "    }\n" +
                 "    post {\n" +
                 "        failure {\n" +
                 "            echo \"failed\"\n" +
                 "        }\n" +
                 "        success {\n" +
                 "            echo \"success\"\n" +
                 "        }\n" +
                 "    }\n" +
                 "}");
    WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false)));
    for (SCMSource source : mp.getSCMSources()) {
        assertEquals(mp, source.getOwner());
    }

    mp.scheduleBuild2(0).getFuture().get();

    j.waitUntilNoActivity();

    WorkflowJob p = scheduleAndFindBranchProject(mp, "master");
    j.waitUntilNoActivity();
    WorkflowRun b1 = p.getLastBuild();
    Assert.assertEquals(Result.SUCCESS, b1.getResult());

    List<FlowNode> stages = getStages(NodeGraphBuilder.NodeGraphBuilderFactory.getInstance(b1));

    Assert.assertEquals(3, stages.size());

    Assert.assertEquals("build", stages.get(0).getDisplayName());
    Assert.assertEquals("SkippedStage", stages.get(1).getDisplayName());
    Assert.assertEquals("deploy", stages.get(2).getDisplayName());

    List<Map> resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/nodes/", List.class);
    Assert.assertEquals(3, resp.size());
    Assert.assertEquals("build", resp.get(0).get("displayName"));
    Assert.assertEquals("SkippedStage", resp.get(1).get("displayName"));
    Assert.assertEquals("deploy", resp.get(2).get("displayName"));
    //check status
    Assert.assertEquals("SUCCESS", resp.get(0).get("result"));
    Assert.assertEquals("FINISHED", resp.get(0).get("state"));
    Assert.assertEquals("NOT_BUILT", resp.get(1).get("result"));
    Assert.assertEquals("SKIPPED", resp.get(1).get("state"));
    Assert.assertEquals("SUCCESS", resp.get(2).get("result"));
    Assert.assertEquals("FINISHED", resp.get(2).get("state"));

    resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/steps/", List.class);
    Assert.assertEquals(7, resp.size());

    resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/nodes/" + stages.get(0).getId() + "/steps/", List.class);
    Assert.assertEquals(3, resp.size());

    resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/nodes/" + stages.get(1).getId() + "/steps/", List.class);
    Assert.assertEquals(0, resp.size());

    resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/nodes/" + stages.get(2).getId() + "/steps/", List.class);
    Assert.assertEquals(4, resp.size());
}
 
Example 19
Source File: PipelineNodeTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void declarativeSyntheticSteps() throws Exception {
    setupScm("pipeline {\n" +
                 "    agent any\n" +
                 "    stages {\n" +
                 "        stage(\"build\") {\n" +
                 "            steps{\n" +
                 "              sh 'echo \"Start Build\"'\n" +
                 "              echo 'End Build'\n" +
                 "            }\n" +
                 "        }\n" +
                 "        stage(\"deploy\") {\n" +
                 "            steps{\n" +
                 "              sh 'echo \"Start Deploy\"'\n" +
                 "              sh 'echo \"Deploying...\"'\n" +
                 "              sh 'echo \"End Deploy\"'\n" +
                 "            }           \n" +
                 "        }\n" +
                 "    }\n" +
                 "    post {\n" +
                 "        failure {\n" +
                 "            echo \"failed\"\n" +
                 "        }\n" +
                 "        success {\n" +
                 "            echo \"success\"\n" +
                 "        }\n" +
                 "    }\n" +
                 "}");
    WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false)));
    for (SCMSource source : mp.getSCMSources()) {
        assertEquals(mp, source.getOwner());
    }

    mp.scheduleBuild2(0).getFuture().get();

    j.waitUntilNoActivity();

    WorkflowJob p = scheduleAndFindBranchProject(mp, "master");
    j.waitUntilNoActivity();
    WorkflowRun b1 = p.getLastBuild();
    Assert.assertEquals(Result.SUCCESS, b1.getResult());

    List<FlowNode> stages = getStages(NodeGraphBuilder.NodeGraphBuilderFactory.getInstance(b1));

    Assert.assertEquals(2, stages.size());

    Assert.assertEquals("build", stages.get(0).getDisplayName());
    Assert.assertEquals("deploy", stages.get(1).getDisplayName());

    List<Map> resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/nodes/", List.class);
    Assert.assertEquals(2, resp.size());
    Assert.assertEquals("build", resp.get(0).get("displayName"));
    Assert.assertEquals("deploy", resp.get(1).get("displayName"));

    resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/steps/", List.class);
    Assert.assertEquals(7, resp.size());

    resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/nodes/" + stages.get(0).getId() + "/steps/", List.class);
    Assert.assertEquals(3, resp.size());

    resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/nodes/" + stages.get(1).getId() + "/steps/", List.class);
    Assert.assertEquals(4, resp.size());

}