/** * Main entry point invoked by the setup module */ public int run(Bootstrap bootstrap) throws Exception { Jenkins j = Jenkins.getInstance(); WorkflowJob w = j.createProject(WorkflowJob.class, "job"); w.addProperty(new DurabilityHintJobProperty(FlowDurabilityHint.PERFORMANCE_OPTIMIZED)); w.setDefinition(new CpsScmFlowDefinition( new FileSystemSCM(bootstrap.jenkinsfile.getParent()), bootstrap.jenkinsfile.getName())); QueueTaskFuture<WorkflowRun> f = w.scheduleBuild2(0, new SetJenkinsfileLocation(bootstrap.jenkinsfile)); b = f.getStartCondition().get(); writeLogTo(System.out); f.get(); // wait for the completion return b.getResult().ordinal; }
@Override public void evaluate() throws Throwable { SemaphoreStep.success("wait/1", null); WorkflowJob p = story.j.jenkins.getItemByFullName("p", WorkflowJob.class); WorkflowRun b = p.getLastBuild(); story.j.assertLogContains("subsequently two", story.j.waitForCompletion(b)); } });
@Override public void evaluate() throws Throwable { WorkflowJob p = story.j.jenkins.getItemByFullName("p", WorkflowJob.class); WorkflowRun b = p.getBuildByNumber(1); SemaphoreStep.success("wait/1", null); story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b)); } });
public void upsertItem(Item item) { if (item instanceof WorkflowJob) { WorkflowJob job = (WorkflowJob) item; if (job.getProperty(BuildConfigProjectProperty.class) != null && StringUtils.isNotBlank(job.getProperty(BuildConfigProjectProperty.class).getNamespace()) && StringUtils.isNotBlank(job.getProperty(BuildConfigProjectProperty.class).getName())) { logger.info("Updated WorkflowJob " + job.getDisplayName() + " replicating changes to OpenShift"); upsertBuildConfigForJob(job); } } }
@Override public WorkflowJob setBranch(WorkflowJob project, Branch branch) { project.setDefinition(createDefinition()); BranchJobProperty property = project.getProperty(BranchJobProperty.class); try { if (property == null) { project.addProperty(new BranchJobProperty(branch)); } else { // TODO may add equality check if https://github.com/jenkinsci/branch-api-plugin/pull/36 or equivalent is implemented property.setBranch(branch); project.save(); } } catch (IOException x) { LOGGER.log(Level.WARNING, null, x); } return project; }
public void onRenamed(WorkflowJob pipeline, String oldName, String newName) { LOGGER.log(Level.FINE, "onRenamed({0}, {1}, {2})", new Object[]{pipeline, oldName, newName}); String oldFullName; ItemGroup parent = pipeline.getParent(); if (parent.equals(Jenkins.getInstance())) { oldFullName = oldName; } else { oldFullName = parent.getFullName() + "/" + oldName; } String newFullName = pipeline.getFullName(); globalPipelineMavenConfig.getDao().renameJob(oldFullName, newFullName); }
@Issue("SECURITY-567") @Test public void methodPointers() throws Exception { logging.record(CpsTransformer.class, Level.FINEST); WorkflowJob job = jenkins.jenkins.createProject(WorkflowJob.class, "p"); job.setDefinition(new CpsFlowDefinition("println((Jenkins.&getInstance)())", true)); WorkflowRun b = job.scheduleBuild2(0).get(); jenkins.assertBuildStatus(Result.FAILURE, b); jenkins.assertLogContains("org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod jenkins.model.Jenkins getInstance", b); }
WorkflowJob job = new WorkflowJob(Jenkins.getInstance(), OpenShiftUtils.jenkinsJobName(bc, defaultNamespace)); null ); job.setDefinition(new CpsScmFlowDefinition(scm, jenkinsfilePath)); } else { LOGGER.warning("BuildConfig does not contain source repository information - cannot map BuildConfig to Jenkins job"); job.setDefinition(new CpsFlowDefinition(jenkinsfile, true)); job.addTrigger(new BuildTrigger());
@Issue("JENKINS-44149") @Test public void propsRemoved() throws Exception { WorkflowRun b = getAndStartNonRepoBuild("simpleJobProperties"); j.assertBuildStatusSuccess(j.waitForCompletion(b)); WorkflowJob job = b.getParent(); assertNotNull(job.getProperty(BuildDiscarderProperty.class)); job.setDefinition(new CpsFlowDefinition(pipelineSourceFromResources("propsTriggersParamsRemoved"), true)); j.buildAndAssertSuccess(job); assertNull(job.getProperty(BuildDiscarderProperty.class)); }
if (property == null) { if (job instanceof WorkflowJob) { FlowDefinition defn = ((WorkflowJob) job).getDefinition(); if (defn instanceof CpsScmFlowDefinition) { Jenkins.getActiveInstance().getDescriptorByType(WorkflowMultiBranchProject.DescriptorImpl.class).getDisplayName() + "” or “" + Jenkins.getActiveInstance().getDescriptorByType(CpsScmFlowDefinition.DescriptorImpl.class).getDisplayName() + "”"); } else { SCMHead head = branch.getHead(); FlowExecutionOwner owner = ((WorkflowRun) build).asFlowExecutionOwner(); TaskListener listener = owner != null ? owner.getListener() : TaskListener.NULL; tip = scmSource.fetch(head, listener);
@Issue("JENKINS-44848") @LocalData @Test public void trackerPropertyUpgrade() throws Exception { WorkflowJob p = r.jenkins.getItemByFullName("trackerPropertyUpgrade", WorkflowJob.class); assertNotNull(p); WorkflowRun b1 = p.getLastBuild(); assertNotNull(b1); assertNotNull(p.getProperty(BuildDiscarderProperty.class)); assertNull(p.getAction(JobPropertyTrackerAction.class)); p.setDefinition(new CpsFlowDefinition("properties([disableConcurrentBuilds()])", true)); r.buildAndAssertSuccess(p); assertNull(p.getProperty(BuildDiscarderProperty.class)); assertNotNull(p.getProperty(DisableConcurrentBuildsJobProperty.class)); JobPropertyTrackerAction action2 = p.getAction(JobPropertyTrackerAction.class); assertNotNull(action2); assertEquals(Collections.singleton(r.jenkins.getDescriptor(DisableConcurrentBuildsJobProperty.class).getId()), action2.getJobPropertyDescriptors()); }
boolean ok = false; if (job instanceof WorkflowJob) { FlowDefinition defn = ((WorkflowJob) job).getDefinition(); if (defn instanceof CpsScmFlowDefinition) { Jenkins.getActiveInstance().getDescriptorByType(WorkflowMultiBranchProject.DescriptorImpl.class).getDisplayName() + "” or “" + Jenkins.getActiveInstance().getDescriptorByType(CpsScmFlowDefinition.DescriptorImpl.class).getDisplayName() + "”");
@Issue("JENKINS-43934") @Test public void flattenGString() throws Exception { WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition(new CpsFlowDefinition("def message = myJoin(['the', /${'message'.toLowerCase(Locale.ENGLISH)}/]); echo(/What is $message?/)", true)); r.assertLogContains("What is the message?", r.assertBuildStatusSuccess(p.scheduleBuild2(0))); } public static class MyJoinStep extends Step {
@Issue("JENKINS-50888") // Tried to modify build without lazy load being triggered @Test public void modifyBeforeLazyLoad() { story.then(r -> { // Normal build WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition(new CpsFlowDefinition("echo 'dosomething'", true)); r.buildAndAssertSuccess(p); }); story.then(r -> { // But wait, we try to modify the build without loading the execution WorkflowJob p = r.jenkins.getItemByFullName("p", WorkflowJob.class); WorkflowRun b = p.getBuildByNumber(1); b.setDescription("Bob"); b.save(); // Before the JENKINS-50888 fix this would trigger an IOException }); story.then( r-> { // Verify that the FlowExecutionOwner can trigger lazy-load correctly WorkflowJob p = r.jenkins.getItemByFullName("p", WorkflowJob.class); WorkflowRun b = p.getBuildByNumber(1); Assert.assertEquals("Bob", b.getDescription()); Assert.assertEquals("4", b.getExecution().getCurrentHeads().get(0).getId()); }); } }
@Override public void evaluate() throws Throwable { WorkflowJob p = jenkins.getItemByFullName("p", WorkflowJob.class); WorkflowRun b = p.getBuildByNumber(2); SemaphoreStep.success("wait/1", null); story.j.assertLogContains("a ran on value from b", story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b))); // Better case: jenkins.getWorkspaceFor(p).child("b.groovy").write("def m(a, arg) {a(\"${arg} from b\")}; this", null); p.setDefinition(new CpsFlowDefinition("def a; def b; node {a = load 'a.groovy'; b = load 'b.groovy'}; b.m(a, 'value')", true)); story.j.assertLogContains("a ran on value from b", story.j.assertBuildStatusSuccess(p.scheduleBuild2(0))); } });
@Issue("JENKINS-46894") @Test public void BuildStatusWhenWithUserIdCause() throws Exception { WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "buildStatusWhenUserIdCause"); p.setDefinition(new CpsFlowDefinition(pipelineSourceFromResources("buildStatusWhenUserIdCause"), true)); // get the build going, and wait until workflow pauses WorkflowRun b = p.scheduleBuild2(0, new CauseAction(new UserIdCause("vlinde")) ).getStartCondition().get(); j.waitForCompletion(b); j.assertLogContains("World", b); j.assertLogContains("Heal it", b); }
@Issue("JENKINS-50880") @Test public void whenBeforeInputFalse() throws Exception { String whenFile = "whenBeforeInputFalse"; WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, whenFile); p.setDefinition(new CpsFlowDefinition(pipelineSourceFromResources(whenFile), true)); // get the build going, and wait until workflow pauses QueueTaskFuture<WorkflowRun> q = p.scheduleBuild2(0); WorkflowRun b = q.getStartCondition().get(); CpsFlowExecution e = (CpsFlowExecution) b.getExecutionPromise().get(); j.assertBuildStatusSuccess(j.waitForCompletion(b)); j.assertLogNotContains("X-NO-SHOW-X", 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)); } });
/** * Tests the ability to execute a step with an unnamed monomorphic describable argument. */ @Issue("JENKINS-29711") @Test public void monomorphic() throws Exception { WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "mon"); p.setDefinition(new CpsFlowDefinition("monomorphStep([firstArg:'one', secondArg:'two'])", true)); r.assertLogContains("First arg: one, second arg: two", r.assertBuildStatusSuccess(p.scheduleBuild2(0))); WorkflowRun run = p.getLastBuild(); LinearScanner scanner = new LinearScanner(); FlowNode node = scanner.findFirstMatch(run.getExecution().getCurrentHeads(), new NodeStepTypePredicate("monomorphStep")); ArgumentsAction argumentsAction = node.getPersistentAction(ArgumentsAction.class); Assert.assertNotNull(argumentsAction); Assert.assertEquals("one,two", ArgumentsAction.getStepArgumentsAsString(node)); }
m.hg(otherRepo, "init"); m.touchAndCommit(otherRepo, "otherfile"); WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "demo"); p.addTrigger(new SCMTrigger("")); p.setQuietPeriod(3); // so it only does one build p.setDefinition(new CpsFlowDefinition( "node('" + slave.getNodeName() + "') {\n" + " dir('main') {\n" + " archive '**'\n" + "}")); WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0)); VirtualFile artifacts = b.getArtifactManager().root(); assertTrue(artifacts.child("main/file").isFile()); assertTrue(artifacts.child("other/otherfile").isFile()); r.assertLogContains("unversioned did not exist", b); m.touchAndCommit(sampleRepo, "file2"); m.touchAndCommit(otherRepo, "otherfile2"); m.notifyCommit(sampleRepo); m.notifyCommit(otherRepo); FileUtils.copyFile(p.getSCMTrigger().getLogFile(), System.out); b = r.assertBuildStatusSuccess(p.getLastBuild()); assertEquals(2, b.number); artifacts = b.getArtifactManager().root(); assertTrue(artifacts.child("main/file2").isFile()); Iterator<? extends SCM> scms = p.getSCMs().iterator(); assertTrue(scms.hasNext()); assertEquals(sampleRepo.toURI().toString(), ((MercurialSCM) scms.next()).getSource());