Java Code Examples for hudson.model.FreeStyleProject#getLastBuild()

The following examples show how to use hudson.model.FreeStyleProject#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: DockerContainerITest.java    From warnings-ng-plugin with MIT License 6 votes vote down vote up
/**
 * Build a maven project on a docker container agent.
 *
 * @throws IOException
 *         When the node assignment of the agent fails.
 * @throws InterruptedException
 *         If the creation of the docker container fails.
 */
@Test
public void shouldBuildMavenOnAgent() throws IOException, InterruptedException {
    assumeThat(isWindows()).as("Running on Windows").isFalse();

    DumbSlave agent = createDockerContainerAgent(javaDockerRule.get());

    FreeStyleProject project = createFreeStyleProject();
    project.setAssignedNode(agent);

    createFileInAgentWorkspace(agent, project, "src/main/java/Test.java", getSampleJavaFile());
    createFileInAgentWorkspace(agent, project, "pom.xml", getSampleMavenFile());
    project.getBuildersList().add(new Maven("compile", null));
    enableWarnings(project, createTool(new Java(), ""));

    scheduleSuccessfulBuild(project);

    FreeStyleBuild lastBuild = project.getLastBuild();
    AnalysisResult result = getAnalysisResult(lastBuild);

    assertThat(result).hasTotalSize(2);
    assertThat(lastBuild.getBuiltOn().getLabelString()).isEqualTo(((Node) agent).getLabelString());
}
 
Example 2
Source File: DockerContainerITest.java    From warnings-ng-plugin with MIT License 6 votes vote down vote up
/**
 * Runs a make file to compile a cpp file on a docker container agent.
 *
 * @throws IOException
 *         When the node assignment of the agent fails.
 * @throws InterruptedException
 *         If the creation of the docker container fails.
 */
@Test
public void shouldBuildMakefileOnAgent() throws IOException, InterruptedException {
    assumeThat(isWindows()).as("Running on Windows").isFalse();

    DumbSlave agent = createDockerContainerAgent(gccDockerRule.get());

    FreeStyleProject project = createFreeStyleProject();
    project.setAssignedNode(agent);

    createFileInAgentWorkspace(agent, project, "test.cpp", getSampleCppFile());
    createFileInAgentWorkspace(agent, project, "makefile", getSampleMakefileFile());
    project.getBuildersList().add(new Shell("make"));
    enableWarnings(project, createTool(new Gcc4(), ""));

    scheduleSuccessfulBuild(project);

    FreeStyleBuild lastBuild = project.getLastBuild();
    AnalysisResult result = getAnalysisResult(lastBuild);

    assertThat(result).hasTotalSize(1);
    assertThat(lastBuild.getBuiltOn().getLabelString()).isEqualTo(((Node) agent).getLabelString());
}
 
Example 3
Source File: FreestyleTest.java    From yet-another-docker-plugin with MIT License 6 votes vote down vote up
@Override
public Boolean call() throws Throwable {
    final Jenkins jenkins = Jenkins.getInstance();

    // prepare job
    final FreeStyleProject project = jenkins.createProject(FreeStyleProject.class, "freestyle-project");
    final Shell env = new Shell("env");
    project.getBuildersList().add(env);
    project.setAssignedLabel(new LabelAtom(DOCKER_CLOUD_LABEL));
    project.save();

    LOG.trace("trace test.");
    project.scheduleBuild(new TestCause());

    // image pull may take time
    waitUntilNoActivityUpTo(jenkins, 10 * 60 * 1000);

    final FreeStyleBuild lastBuild = project.getLastBuild();
    assertThat(lastBuild, not(nullValue()));
    assertThat(lastBuild.getResult(), is(Result.SUCCESS));

    assertThat(getLog(lastBuild), Matchers.containsString(TEST_VALUE));
    assertThat(getLog(lastBuild), Matchers.containsString(CLOUD_ID + "=" + DOCKER_CLOUD_NAME));

    return true;
}
 
Example 4
Source File: FreestyleTest.java    From yet-another-docker-plugin with MIT License 5 votes vote down vote up
@Override
        public Boolean call() throws Throwable {
            final Jenkins jenkins = Jenkins.getInstance();
            assertThat(image, notNullValue());

            // prepare job
            final FreeStyleProject project = jenkins.createProject(FreeStyleProject.class, "freestyle-dockerShell");
            final Shell env = new Shell("env");
            project.getBuildersList().add(env);

            DockerShellStep dockerShellStep = new DockerShellStep();
            dockerShellStep.setShellScript("env && pwd");
            dockerShellStep.getContainerLifecycle().setImage(image);
            dockerShellStep.setConnector(new CloudNameDockerConnector(DOCKER_CLOUD_NAME));
            project.getBuildersList().add(dockerShellStep);

//            project.setAssignedLabel(new LabelAtom(DOCKER_CLOUD_LABEL));
            project.save();

            LOG.trace("trace test.");
            project.scheduleBuild(new TestCause());

            // image pull may take time
            waitUntilNoActivityUpTo(jenkins, 10 * 60 * 1000);

            final FreeStyleBuild lastBuild = project.getLastBuild();
            assertThat(lastBuild, not(nullValue()));
            assertThat(lastBuild.getResult(), is(Result.SUCCESS));

            assertThat(getLog(lastBuild), Matchers.containsString("exit code: 0"));

            return true;
        }
 
Example 5
Source File: HudsonTestCaseShutdownSlaveTest.java    From jenkins-test-harness with MIT License 4 votes vote down vote up
public void testShutdownSlave() throws Exception {
    DumbSlave slave1 = createOnlineSlave(); // online, and a build finished.
    DumbSlave slave2 = createOnlineSlave(); // online, and a build finished, and disconnected.
    DumbSlave slave3 = createOnlineSlave(); // online, and a build still running.
    DumbSlave slave4 = createOnlineSlave(); // online, and not used.
    DumbSlave slave5 = createSlave();   // offline.
    
    assertNotNull(slave1);
    assertNotNull(slave2);
    assertNotNull(slave3);
    assertNotNull(slave4);
    assertNotNull(slave5);
    
    // A build runs on slave1 and finishes.
    {
        FreeStyleProject project1 = createFreeStyleProject();
        project1.setAssignedLabel(LabelExpression.parseExpression(slave1.getNodeName()));
        project1.getBuildersList().add(new SleepBuilder(1 * 1000));
        assertBuildStatusSuccess(project1.scheduleBuild2(0));
    }
    
    // A build runs on slave2 and finishes, then disconnect slave2 
    {
        FreeStyleProject project2 = createFreeStyleProject();
        project2.setAssignedLabel(LabelExpression.parseExpression(slave2.getNodeName()));
        project2.getBuildersList().add(new SleepBuilder(1 * 1000));
        assertBuildStatusSuccess(project2.scheduleBuild2(0));
        
        SlaveComputer computer2 = slave2.getComputer();
        computer2.disconnect(null);
        computer2.waitUntilOffline();
    }
    
    // A build runs on slave3 and does not finish.
    // This build will be interrupted in tearDown().
    {
        FreeStyleProject project3 = createFreeStyleProject();
        project3.setAssignedLabel(LabelExpression.parseExpression(slave3.getNodeName()));
        project3.getBuildersList().add(new SleepBuilder(10 * 60 * 1000));
        project3.scheduleBuild2(0);
        FreeStyleBuild build;
        while((build = project3.getLastBuild()) == null) {
            Thread.sleep(500);
        }
        assertTrue(build.isBuilding());
    }
}
 
Example 6
Source File: GitHubBranchTriggerTest.java    From github-integration-plugin with MIT License 4 votes vote down vote up
@LocalData
@Test
public void someTest() throws Exception {
    FreeStyleProject prj = jRule.createFreeStyleProject("project");
    prj.addProperty(new GithubProjectProperty("http://localhost/org/repo"));

    final List<GitHubBranchEvent> events = new ArrayList<>();
    events.add(new GitHubBranchCreatedEvent());
    events.add(new GitHubBranchHashChangedEvent());

    final GitHubBranchTrigger trigger = new GitHubBranchTrigger("", CRON, events);
    prj.addTrigger(trigger);
    prj.save();
    // activate trigger
    jRule.configRoundtrip(prj);

    final GitHubBranchTrigger branchTrigger = prj.getTrigger(GitHubBranchTrigger.class);

    assertThat(branchTrigger.getRemoteRepository(), notNullValue());

    GitHubBranchRepository localRepo = prj.getAction(GitHubBranchRepository.class);
    assertThat(localRepo, notNullValue());
    assertThat(localRepo.getBranches().size(), is(2));
    assertThat(localRepo.getBranches(), hasKey("for-removal"));
    assertThat(localRepo.getBranches(), hasKey("should-change"));
    GitHubBranch shouldChange = localRepo.getBranches().get("should-change");
    assertThat(shouldChange.getCommitSha(), is("6dcb09b5b57875f334f61aebed695e2e4193ffbb"));

    // only single branch should change in local repo
    branchTrigger.doRun("should-change");
    jRule.waitUntilNoActivity();

    assertThat(prj.getBuilds(), hasSize(1));
    FreeStyleBuild lastBuild = prj.getLastBuild();

    GitHubBranchCause cause = lastBuild.getCause(GitHubBranchCause.class);
    assertThat(cause, notNullValue());
    assertThat(cause.getCommitSha(), is("6dcb09b5b57875f334f61aebed695e2e4193ffgg"));
    assertThat(cause.getBranchName(), is("should-change"));

    localRepo = prj.getAction(GitHubBranchRepository.class);
    assertThat(localRepo, notNullValue());
    assertThat(localRepo.getBranches().size(), is(2));
    assertThat(localRepo.getBranches(), hasKey("for-removal"));
    assertThat(localRepo.getBranches(), hasKey("should-change"));
    shouldChange = localRepo.getBranches().get("should-change");
    assertThat(shouldChange.getCommitSha(), is("6dcb09b5b57875f334f61aebed695e2e4193ffgg"));


    // and now full trigger run()
    branchTrigger.doRun();

    jRule.waitUntilNoActivity();

    assertThat(prj.getBuilds(), hasSize(2));
    lastBuild = prj.getLastBuild();
    assertThat(lastBuild, notNullValue());

    cause = lastBuild.getCause(GitHubBranchCause.class);
    assertThat(cause, notNullValue());
    assertThat(cause.getCommitSha(), is("6dcb09b5b57875f334f61aebed695e2e4193db5e"));
    assertThat(cause.getBranchName(), is("new-branch"));

    localRepo = prj.getAction(GitHubBranchRepository.class);
    assertThat(localRepo.getBranches().size(), is(2));
    assertThat(localRepo.getBranches(), not(hasKey("for-removal")));
    assertThat(localRepo.getBranches(), hasKey("should-change"));

    shouldChange = localRepo.getBranches().get("should-change");
    assertThat(shouldChange.getCommitSha(), is("6dcb09b5b57875f334f61aebed695e2e4193ffgg"));

    assertThat(localRepo.getBranches(), hasKey("new-branch"));
    GitHubBranch branch = localRepo.getBranches().get("new-branch");
    assertThat(branch.getCommitSha(), is("6dcb09b5b57875f334f61aebed695e2e4193db5e"));

}