com.offbytwo.jenkins.JenkinsServer Java Examples

The following examples show how to use com.offbytwo.jenkins.JenkinsServer. 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: BentenJenkinsClient.java    From benten with MIT License 7 votes vote down vote up
@PostConstruct
public void init(){
    try{

        jenkins = new JenkinsServer(new URI(jenkinsProperties.getBaseurl()), jenkinsProperties.getUsername(), jenkinsProperties.getPassword());

        jenkinsHttpClient = new JenkinsHttpClient(new URI(jenkinsProperties.getBaseurl()),jenkinsProperties.getUsername(),jenkinsProperties.getPassword());

       // logger.info("Connection to Jenkins Server returned: " + jenkins.isRunning());
       // logger.info("Version returned from Jenkins http Client: " + jenkinsHttpClient.getJenkinsVersion());
    }catch (Exception e){
        logger.error("Failed to Connect to Jenkins Instance");
        e.printStackTrace();
    }

}
 
Example #2
Source File: GogsWebHook_IT.java    From gogs-webhook-plugin with MIT License 7 votes vote down vote up
/**
 * Wait for the build to be queued and complete
 *
 * @param jenkins          Jenkins server instance
 * @param expectedBuildNbr the build number we're are expecting
 * @param timeOut          the maximum time in millisecond we are waiting
 * @throws InterruptedException the build was interrupted
 * @throws TimeoutException     we exeeded the timeout period.
 * @throws IOException          an unexpected error occurred while communicating with Jenkins
 */
private void waitForBuildToComplete(JenkinsServer jenkins, int expectedBuildNbr, long timeOut, String jobname) throws InterruptedException, TimeoutException, IOException {
    boolean buildCompleted = false;
    long timeoutCounter = 0L;
    while (!buildCompleted) {
        Thread.sleep(2000);
        timeoutCounter = timeoutCounter + 2000L;
        if (timeoutCounter > timeOut) {
            throw new TimeoutException("The job did not complete in the expected time");
        }
        //When the build is in the queue, the nextbuild number didn't change.
        //When it changed, It might still be running.
        JobWithDetails wrkJobData = jenkins.getJob(jobname);
        int newNextNbr = wrkJobData.getNextBuildNumber();
        log.info("New Next Nbr:" + newNextNbr);
        if (expectedBuildNbr != newNextNbr) {
            log.info("The expected build is there");
            boolean isBuilding = wrkJobData.getLastBuild().details().isBuilding();
            if (!isBuilding) {
                buildCompleted = true;
            }
        }
    }
}
 
Example #3
Source File: JenkinsHandler.java    From gogs-webhook-plugin with MIT License 6 votes vote down vote up
public static void waitUntilJenkinsHasBeenStartedUp(JenkinsServer jenkinsServer) throws TimeoutException {
    final long start = System.currentTimeMillis();
    System.out.print("Wait until Jenkins is started...");
    while (!jenkinsServer.isRunning() && !timeOut(start)) {
        try {
            System.out.print(".");
            Thread.sleep(TimeUnit.MILLISECONDS.convert(1L, TimeUnit.SECONDS));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    if (!jenkinsServer.isRunning() && timeOut(start)) {
        System.out.println("Failure.");
        throw new TimeoutException("Jenkins startup check has failed. Took more than one minute.");
    }

    System.out.println("done.");
}
 
Example #4
Source File: TestJenkinsVerifier.java    From verigreen with Apache License 2.0 6 votes vote down vote up
@Test
public void testStopBuildById() throws IOException, InterruptedException {
    
    String jobName = "testing-jenkins-api";
    String parameterNameForJob = "ParamForTesting";
    final ImmutableMap<String, String> params = ImmutableMap.of(parameterNameForJob, "master");
    
    BuildVerifier buildVerifier = CollectorApi.getJenkinsVerifier();
    JenkinsServer jenkninsServer = CollectorApi.getJenkinsServer();
    JobWithDetails job = jenkninsServer.getJob(jobName);
    int nextBuildNumber = job.getNextBuildNumber();
    job.build(params);
    Thread.sleep(5000);
    boolean stopBuildResult =
            ((JenkinsVerifier) buildVerifier).stop(jobName, Integer.toString(nextBuildNumber));
    Assert.assertEquals(true, stopBuildResult);
}
 
Example #5
Source File: ClassicJobApi.java    From blueocean-plugin with MIT License 5 votes vote down vote up
public <T> T until(Function<JenkinsServer, T> function, long timeoutInMS) {
    return new FluentWait<JenkinsServer>(jenkins)
        .pollingEvery(500, TimeUnit.MILLISECONDS)
        .withTimeout(timeoutInMS, TimeUnit.MILLISECONDS)
        .ignoring(NotFoundException.class)
        .until((JenkinsServer server) -> function.apply(server));
}
 
Example #6
Source File: GogsWebHook_IT.java    From gogs-webhook-plugin with MIT License 5 votes vote down vote up
/**
 * Loads the marker file of the last build (archived during the build)
 *
 * @param jenkins the jenkins instance we want to load from
 * @return the marker file loaded as a property file (so that it can be easily queried)
 * @throws IOException        Something unexpected went wrong when querying the Jenkins server
 * @throws URISyntaxException Something unexpected went wrong loading the marker as a property
 */
private Properties loadMarkerArtifactAsProperty(JenkinsServer jenkins, String jobName) throws IOException, URISyntaxException {
    JobWithDetails detailedJob = jenkins.getJob(jobName);
    BuildWithDetails lastBuild = detailedJob.getLastBuild().details();
    int buildNbr = lastBuild.getNumber();
    boolean isBuilding = lastBuild.isBuilding();
    log.info("BuildNbr we are examining: " + buildNbr);

    List<Artifact> artifactList = lastBuild.getArtifacts();
    assertEquals("Not the expected number of artifacts", 1, artifactList.size());

    Artifact markerArtifact = artifactList.get(0);
    String markerArtifactFileName = markerArtifact.getFileName();
    assertEquals("The artifact is not the expected one", "marker.txt", markerArtifactFileName);
    InputStream markerArtifactInputStream = lastBuild.details().downloadArtifact(markerArtifact);
    String markerAsText = IOUtils.toString(markerArtifactInputStream, Charset.defaultCharset());
    log.info("\n" + markerAsText);
    StringReader reader = new StringReader(markerAsText);
    Properties markerAsProperty = new Properties();
    markerAsProperty.load(reader);

    //check if the marker matches the build number we expect.
    String buildNbrFromMarker = markerAsProperty.getProperty("BUILD_NUMBER");
    String buildNbrFromQery = String.valueOf(buildNbr);
    assertEquals("The build number from the marker does not match the last build number", buildNbrFromMarker, buildNbrFromQery);
    return markerAsProperty;
}
 
Example #7
Source File: TestJenkinsVerifier.java    From verigreen with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuild() throws IOException {
    
	String jobName = "testing-jenkins-api";
    String parameterNameForJob = "ParamForTesting";
    final ImmutableMap<String, String> params = ImmutableMap.of(parameterNameForJob, "master");
    JenkinsServer jenkninsServer = CollectorApi.getJenkinsServer();
    JobWithDetails job = jenkninsServer.getJob(jobName);
    job.build(params);
}
 
Example #8
Source File: JenkinsClient.java    From seppb with MIT License 4 votes vote down vote up
public JenkinsClient(JenkinsServer jenkinsServer, JenkinsHttpClient client) {
    this.jenkinsServer = jenkinsServer;
    this.client = client;
}
 
Example #9
Source File: JenkinsClient.java    From seppb with MIT License 4 votes vote down vote up
public JenkinsClient(String url, String username, String password, HttpClientBuilder builder) {
    this.client = new JenkinsHttpClient(URI.create(url), builder, username, password);
    this.jenkinsServer = new JenkinsServer(client);
}
 
Example #10
Source File: BentenJenkinsClient.java    From benten with MIT License 4 votes vote down vote up
public JenkinsServer getJenkins() {
    return jenkins;
}
 
Example #11
Source File: ForgeClientHelpers.java    From fabric8-forge with Apache License 2.0 4 votes vote down vote up
public static JenkinsServer createJenkinsServer() throws URISyntaxException {
    String url = getJenkinsURL();
    return new JenkinsServer(new URI(url));
}
 
Example #12
Source File: ForgeClientAsserts.java    From fabric8-forge with Apache License 2.0 4 votes vote down vote up
public static JobWithDetails assertJob(String projectName) throws URISyntaxException, IOException {
    JenkinsServer jenkins = createJenkinsServer();
    JobWithDetails job = jenkins.getJob(projectName);
    assertThat(job).describedAs("No Jenkins Job found for name: " + projectName).isNotNull();
    return job;
}
 
Example #13
Source File: CollectorApi.java    From verigreen with Apache License 2.0 2 votes vote down vote up
public static JenkinsServer getJenkinsServer() {
    
    return getBean(JenkinsServer.class);
}