public boolean buildJob(Job job, Map<String, String> jobParameters) { boolean success = false; try { JobWithDetails ciJob = getJobWithDetails(job); QueueReference reference = ciJob.build(jobParameters, true); success = checkReference(reference); } catch (Exception e) { LOGGER.error("Unable to run Jenkins job: " + e.getMessage()); } return success; }
logger.info(jobName + " is buildable: " + job.details().isBuildable()); int lastBuildNumber = job.details().getLastBuild().getNumber(); int nextBuildNumber = job.details().getNextBuildNumber(); while(job.details().isInQueue()){ waitFor++; logger.info("Job in queue"); if(job.details().getBuildByNumber(nextBuildNumber).details().isBuilding()) { logger.info("Jenkins job "+ jobName +" is building with Build Number: " + nextBuildNumber); return "Jenkins job "+ jobName +" is building with Build Number: " + nextBuildNumber;
public boolean rerunJob(Job ciJob, Integer buildNumber, boolean rerunFailures) { boolean success = false; try { JobWithDetails job = getJobWithDetails(ciJob); Map<String, String> params = job.getBuildByNumber(buildNumber).details().getParameters(); params.put("rerun_failures", Boolean.toString(rerunFailures)); params.replace("debug", "false"); QueueReference reference = job.build(params, true); success = checkReference(reference); } catch (Exception e) { LOGGER.error("Unable to rerun Jenkins job: " + e.getMessage()); } return success; }
slackFormatter.link(jobWithDetails.getUrl(),jobWithDetails.getDisplayName()).text(jobWithDetails.getDescription()).newline().newline(); BuildResult buildResult = jobWithDetails.getLastBuild().details().getResult(); if(buildResult!=null) { bentenSlackFields.add(new BentenSlackField("Last Build Status", buildResult.toString(), true)); bentenSlackFields.add(new BentenSlackField("Last Build Status", "INPROGRESS", true)); bentenSlackFields.add(new BentenSlackField("Last Build",Integer.toString(jobWithDetails.getLastBuild().getNumber()),true)); bentenSlackFields.add(new BentenSlackField("Last Successful Build",Integer.toString(jobWithDetails.getLastSuccessfulBuild().getNumber()),true)); bentenSlackFields.add(new BentenSlackField("Last Failed Build",Integer.toString(jobWithDetails.getLastFailedBuild().getNumber()),true)); bentenSlackFields.add(new BentenSlackField("Is in Queue",Boolean.toString(jobWithDetails.isInQueue()),true)); bentenSlackFields.add(new BentenSlackField("Is Buildable",Boolean.toString(jobWithDetails.isBuildable()),true));
@Test public void testCreateJob() throws Exception { final String sourceJob = "pr"; final String jobName = "test-job-" + UUID.randomUUID().toString(); String sourceXml = server.getJobXml(sourceJob); server.createJob(jobName, sourceXml); Map<String, Job> jobs = server.getJobs(); assertTrue(jobs.containsKey(jobName)); JobWithDetails thisJob = jobs.get(jobName).details(); assertNotNull(thisJob); assertTrue(thisJob.getBuilds().size() == 0); thisJob.build(ImmutableMap.of("foo_param", "MUST_PROVIDE_VALUES_DEFAULTS_DONT_WORK")); // wait to see if the job finishes, but with a timeout Future<Void> future = executor.submit(new PerformPollingTest(server, jobName)); // If this times out, either jenkins is slow or our test failed! // IME, usually takes about 10-15 seconds future.get(30, TimeUnit.SECONDS); Build b = server.getJobs().get(jobName).details().getLastSuccessfulBuild(); assertTrue(b != null); }
@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); } }
public Map<String, String> getBuildParametersMap(Job ciJob, Integer buildNumber) { Map<String, String> jobParameters = null; try { JobWithDetails job = getJobWithDetails(ciJob); jobParameters = job.getBuildByNumber(buildNumber).details().getParameters(); jobParameters.put("ci_run_id", UUID.randomUUID().toString()); } catch (Exception e) { LOGGER.error("Unable to get job: " + e.getMessage()); } return jobParameters; }
/** * Update the <code>description</code> of a Job. * * @param description The description which should be set. If you like to * set an empty description you should use * {@link #EMPTY_DESCRIPTION}. * @param crumbFlag <code>true</code> or <code>false</code>. * @throws IOException in case of errors. */ public void updateDescription(String description, boolean crumbFlag) throws IOException { Objects.requireNonNull(description, "description is not allowed to be null."); ImmutableMap<String, String> params = ImmutableMap.of("description", description); client.post_form(this.getUrl() + "/submitDescription?", params, crumbFlag); }
public int getLatestBuildNumber(String jobName){ try{ return jenkins.getJob(jobName).getLastBuild().getNumber(); }catch (Exception e){ e.printStackTrace(); throw new BentenJenkinsException(e.getMessage()); } }
@Test public void testGetJobByName() throws Exception { final String jobName = "trunk"; JobWithDetails job = server.getJob(jobName); assertEquals("trunk",job.getName()); assertEquals("trunk",job.getDisplayName()); }
QueueItem queueItem = this.server.getQueueItem(queueRef); while (!queueItem.isCancelled() && job.isInQueue()) { Thread.sleep(retryInterval); job = this.server.getJob(jobName);
List<Build> builds = client.get(path + "job/" + EncodingUtils.encode(this.getName()) + "?tree=allBuilds[number[*],url[*],queueId[*]]", AllBuilds.class).getAllBuilds();
public Void call() throws InterruptedException, IOException { while(true) { Thread.sleep(500); JobWithDetails jwd = server.getJobs().get(jobName).details(); try { // Throws NPE until the first build succeeds jwd.getLastSuccessfulBuild(); } catch (NullPointerException e) { continue; } // build succeeded return null; } } }
public boolean debug(Job ciJob, Integer buildNumber) { boolean success = false; try { JobWithDetails job = getJobWithDetails(ciJob); Map<String, String> params = job.getBuildByNumber(buildNumber).details().getParameters(); params.replace("debug", "true"); params.replace("rerun_failures", "true"); params.replace("thread_count", "1"); QueueReference reference = job.build(params, true); success = checkReference(reference); } catch (Exception e) { LOGGER.error("Unable to rerun Jenkins job: " + e.getMessage()); } return success; }
public String showConsoleLogForJobWithBuildNumber(String jobName, int buildNumber){ logger.info("Getting Console Log for job: " + jobName + " with BuildNumber: " +buildNumber); try { logger.info(""); String fullLog = jenkins.getJob(jobName).getBuildByNumber(buildNumber).details().getConsoleOutputText(); return fullLog.substring(fullLog.length()-1000, fullLog.length()-1); }catch (Exception e) { logger.error("Failed to get Console Log for job: " + jobName + " with BuildNumber: " +buildNumber); e.printStackTrace(); throw new BentenJenkinsException(e.getMessage()); } }
public Job getJob(String jobName) { Job job = null; try { JobWithDetails jobWithDetails = getServer().getJob(jobName); if(jobWithDetails != null && jobWithDetails.getUrl() != null) { job = new Job(jobName, jobWithDetails.getUrl()); } } catch (IOException e) { LOGGER.error("Unable to get job by name '" + jobName + "'. " + e.getMessage(), e); } return job; }
@Test public void testQueryJobTask() { Map<String, Job> map = jenkinsService.getJobs(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); String key = (String)entry.getKey(); Job job = (Job)entry.getValue(); System.err.println(job.getName()); try{ List<Artifact> as= job.details().getLastBuild().details().getArtifacts(); for(Artifact artifact :as){ System.err.println( artifact.getDisplayPath()); System.err.println( artifact.getFileName()); System.err.println( artifact.getRelativePath()); } }catch (IOException ie){ } } }
QueueItem queueItem = this.server.getQueueItem(queueRef); while (!queueItem.isCancelled() && job.isInQueue()) { Thread.sleep(retryInterval); job = this.server.getJob(jobName);
String path = "/" + "job/" + EncodingUtils.encode(this.getName()) + "?tree=allBuilds[number[*],url[*],queueId[*]]";