/** * Return true if the run is paused on input * @param run * @return */ public static boolean isPendingInput(WorkflowRun run) { // Logic borrowed from Pipeline Stage View plugin, RuneEx InputAction inputAction = run.getAction(InputAction.class); if (inputAction != null) { List<InputStepExecution> executions = inputAction.getExecutions(); if (executions != null && !executions.isEmpty()) { return true; } } return false; }
public static void assertRevisionAction(WorkflowRun build) { SCMRevisionAction revisionAction = build.getAction(SCMRevisionAction.class); assertNotNull(revisionAction); SCMRevision revision = revisionAction.getRevision(); assertEquals(GitBranchSCMRevision.class, revision.getClass()); Set<String> expected = new HashSet<>(); List<BuildData> buildDataActions = build.getActions(BuildData.class); if (!buildDataActions.isEmpty()) { // i.e., we have run at least one checkout step, or done a heavyweight checkout to get a single file for (BuildData data : buildDataActions) { expected.add(data.lastBuild.marked.getSha1().getName()); } assertThat(expected, hasItem(((GitBranchSCMRevision) revision).getHash())); } }
@Override public void triggerManual(String projectName, String upstreamName, String buildId) throws AuthenticationException { LOG.fine("Manual/Input step called for project: " + projectName + " and build id: " + buildId); WorkflowJob workflowJob; try { workflowJob = ProjectUtil.getWorkflowJob(projectName, getOwnerItemGroup()); RunList<WorkflowRun> builds = workflowJob.getBuilds(); for (WorkflowRun run : builds) { if (Integer.toString(run.getNumber()).equals(buildId)) { InputAction inputAction = run.getAction(InputAction.class); if (inputAction != null && !inputAction.getExecutions().isEmpty()) { inputAction.getExecutions().get(0).doProceedEmpty(); } } } } catch (IOException | PipelineException e) { LOG.warning("Failed to resolve project to trigger manual/input: " + e); } }
private static InputStepExecution getInputStepExecution(WorkflowRun run, String inputMessage) throws Exception { InputAction ia = run.getAction(InputAction.class); List<InputStepExecution> execList = ia.getExecutions(); return execList.stream().filter(e -> inputMessage.equals(e.getInput().getMessage())).findFirst().orElse(null); }
private static boolean canRestart(WorkflowRun b, String user) { final RestartDeclarativePipelineAction a = b.getAction(RestartDeclarativePipelineAction.class); return ACL.impersonate(User.get(user).impersonate(), new NotReallyRoleSensitiveCallable<Boolean,RuntimeException>() { @Override public Boolean call() throws RuntimeException { return a.isRestartEnabled(); } }); }
private static boolean canReplay(WorkflowRun b, String user) { ReplayAction a = b.getAction(ReplayAction.class); try (ACLContext context = ACL.as(User.getById(user, true))) { return a.isEnabled(); } }
private static boolean canReplayDeepTest(WorkflowRun b, String user) { ReplayAction a = b.getAction(ReplayAction.class); try (ACLContext context = ACL.as(User.getById(user, true))) { return a.isReplayableSandboxTest(); } }
private static boolean canRebuild(WorkflowRun b, String user) { ReplayAction a = b.getAction(ReplayAction.class); try (ACLContext context = ACL.as(User.getById(user, true))) { return a.isRebuildEnabled(); } }
private static boolean canReplay(WorkflowRun b, String user) { final ReplayAction a = b.getAction(ReplayAction.class); return ACL.impersonate(User.get(user).impersonate(), new NotReallyRoleSensitiveCallable<Boolean,RuntimeException>() { @Override public Boolean call() throws RuntimeException { return a.isEnabled(); } }); }
@Override public EnvVars getEnvironment(TaskListener listener) throws IOException, InterruptedException { EnvVars env = super.getEnvironment(listener); Jenkins instance = Jenkins.getInstance(); if (instance != null) { for (NodeProperty nodeProperty : instance.getGlobalNodeProperties()) { nodeProperty.buildEnvVars(env, listener); } } // TODO EnvironmentContributingAction does not support Job yet: ParametersAction a = getAction(ParametersAction.class); if (a != null) { for (ParameterValue v : a) { v.buildEnvironment(this, env); } } EnvVars.resolve(env); return env; }
@Issue("JENKINS-45455") @Test public void actionPresentOnDeclarative() throws Exception { WorkflowRun r = expect("simplePipeline").go(); assertNotNull(r.getAction(RestartDeclarativePipelineAction.class)); }
private HtmlPage restartFromStageInUI(@Nonnull WorkflowRun original, @Nonnull String stageName) throws Exception { RestartDeclarativePipelineAction action = original.getAction(RestartDeclarativePipelineAction.class); assertNotNull(action); assertTrue(action.isRestartEnabled()); HtmlPage page = j.createWebClient().getPage(original, action.getUrlName()); HtmlForm form = page.getFormByName("restart"); HtmlSelect select = form.getSelectByName("stageName"); select.getOptionByValue(stageName).setSelected(true); return j.submit(form); }
@Issue("JENKINS-41456") @Test public void htmlPublisher() throws Exception { WorkflowRun b = expect("htmlPublisher") .logContains("[Pipeline] { (foo)") .go(); HtmlPublisherTarget.HTMLBuildAction buildReport = b.getAction(HtmlPublisherTarget.HTMLBuildAction.class); assertNotNull(buildReport); assertEquals("Test Report", buildReport.getHTMLTarget().getReportName()); }
@Test public void load() throws Exception { j.jenkins.getWorkspaceFor(p).child("lib.groovy").write("def m() {semaphore 'here'}; this", null); p.setDefinition(new CpsFlowDefinition("def lib; node {lib = load 'lib.groovy'}; lib.m()", true)); WorkflowRun b = p.scheduleBuild2(0).waitForStart(); SemaphoreStep.waitForStart("here/1", b); CpsThreadDump td = b.getAction(CpsThreadDumpAction.class).threadDumpSynchronous(); td.print(System.out); assertStackTrace(td.getThreads().get(0), "DSL.semaphore(waiting on here/1)", "Script1.m(Script1.groovy:1)", "WorkflowScript.run(WorkflowScript:1)"); }
@Test public void allowEmpty() throws Exception { WorkflowJob j = rule.jenkins.createProject(WorkflowJob.class, "allowEmpty"); j.setDefinition(new CpsFlowDefinition("stage('first') {\n" + " node {\n" + (Functions.isWindows() ? " bat 'echo hi'\n" : " sh 'echo hi'\n") + " def results = junit(testResults: '*.xml', allowEmptyResults: true)\n" + " assert results.totalCount == 0\n" + " }\n" + "}\n", true)); WorkflowRun r = rule.buildAndAssertSuccess(j); assertNull(r.getAction(TestResultAction.class)); rule.assertLogContains("None of the test reports contained any result", r); }
@Test public void stop() throws Exception { WorkflowJob p = r.createProject(WorkflowJob.class, "p"); p.setDefinition(new CpsFlowDefinition("unkillable()", true)); final WorkflowRun b = p.scheduleBuild2(0).waitForStart(); r.waitForMessage("unkillable", b); try (ACLContext context = ACL.as(Jenkins.ANONYMOUS)) { b.getExecutor().interrupt(); }; r.waitForCompletion(b); r.assertBuildStatus(Result.ABORTED, b); InterruptedBuildAction iba = b.getAction(InterruptedBuildAction.class); assertNotNull(iba); List<CauseOfInterruption> causes = iba.getCauses(); assertEquals(1, causes.size()); assertEquals(CauseOfInterruption.UserInterruption.class, causes.get(0).getClass()); r.waitForMessage("Finished: ABORTED", b); // TODO JENKINS-46076 WorkflowRun.isBuilding() can go to false before .finish has completed r.assertLogContains("never going to stop", b); r.assertLogNotContains("\tat ", b); }
@Override public void evaluate() throws Throwable { WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p"); p.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("param", ""))); p.setDefinition(new CpsFlowDefinition("echo \"run with ${param}\"", true)); WorkflowRun b1 = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0, new ParametersAction(new StringParameterValue("param", "some value")))); story.j.assertLogContains("run with some value", b1); // When we replay a parameterized build, we expect the original parameter values to be set. WorkflowRun b2 = (WorkflowRun) b1.getAction(ReplayAction.class).run("echo \"run again with ${param}\"", Collections.<String,String>emptyMap()).get(); story.j.assertLogContains("run again with some value", story.j.assertBuildStatusSuccess(b2)); } });
@Issue("JENKINS-45455") @Test public void nullStageName() throws Exception { WorkflowRun original = expect(Result.FAILURE, "restart", "simpleRestart") .logContains("Odd numbered build, failing", "This shouldn't show up on second run") .go(); RestartDeclarativePipelineAction action = original.getAction(RestartDeclarativePipelineAction.class); assertNotNull(action); assertTrue(action.isRestartEnabled()); Exception runFailure = null; try { Queue.Item queueItem = action.run(null); // Because we should definitely have thrown an exception in run. assertNull(queueItem); } catch (Exception e) { runFailure = e; } assertNotNull(runFailure); assertTrue(runFailure instanceof IllegalStateException); assertEquals(Messages.RestartDeclarativePipelineAction_NullStageName(), runFailure.getMessage()); }
@Issue("JENKINS-45455") @Test public void emptyStageName() throws Exception { WorkflowRun original = expect(Result.FAILURE, "restart", "simpleRestart") .logContains("Odd numbered build, failing", "This shouldn't show up on second run") .go(); RestartDeclarativePipelineAction action = original.getAction(RestartDeclarativePipelineAction.class); assertNotNull(action); assertTrue(action.isRestartEnabled()); Exception runFailure = null; try { Queue.Item queueItem = action.run(""); // Because we should definitely have thrown an exception in run. assertNull(queueItem); } catch (Exception e) { runFailure = e; } assertNotNull(runFailure); assertTrue(runFailure instanceof IllegalStateException); assertEquals(Messages.RestartDeclarativePipelineAction_NullStageName(), runFailure.getMessage()); }
@Issue("JENKINS-45455") @Test public void notPresentStageName() throws Exception { WorkflowRun original = expect(Result.FAILURE, "restart", "simpleRestart") .logContains("Odd numbered build, failing", "This shouldn't show up on second run") .go(); RestartDeclarativePipelineAction action = original.getAction(RestartDeclarativePipelineAction.class); assertNotNull(action); assertTrue(action.isRestartEnabled()); Exception runFailure = null; try { Queue.Item queueItem = action.run("not-present"); // Because we should definitely have thrown an exception in run. assertNull(queueItem); } catch (Exception e) { runFailure = e; } assertNotNull(runFailure); assertTrue(runFailure instanceof IllegalStateException); assertEquals(Messages.RestartDeclarativePipelineAction_StageNameNotPresent("not-present", original.getFullDisplayName()), runFailure.getMessage()); }