Skip to content
Permalink
Browse files

Merge pull request #131 from jenkinsci/user-defined-global-libs

[JENKINS-26135] Allow CPS global lib scripts to define global variables
  • Loading branch information...
jglick committed Aug 20, 2015
2 parents 4423736 + 4b48f8a commit 28c9600d1c623779b62e2e46e187671671501a21
Showing with 662 additions and 96 deletions.
  1. +1 −0 CHANGES.md
  2. +0 −57 aggregator/src/test/groovy/org/jenkinsci/plugins/workflow/cps/steps/LoadStepTest.groovy
  3. +3 −1 aggregator/src/test/java/org/jenkinsci/plugins/workflow/CpsFlowExecutionTest.java
  4. +57 −0 aggregator/src/test/java/org/jenkinsci/plugins/workflow/cps/global/WorkflowLibRepositoryTest.java
  5. +52 −0 aggregator/src/test/java/org/jenkinsci/plugins/workflow/cps/steps/LoadStepTest.java
  6. +1 −1 aggregator/src/test/java/org/jenkinsci/plugins/workflow/steps/parallel/ParallelStepTest.java
  7. +136 −22 cps-global-lib/README.md
  8. +84 −0 ...global-lib/src/main/java/org/jenkinsci/plugins/workflow/cps/global/UserDefinedGlobalVariable.java
  9. +58 −0 ...al-lib/src/main/java/org/jenkinsci/plugins/workflow/cps/global/UserDefinedGlobalVariableList.java
  10. +25 −0 cps-global-lib/src/main/java/org/jenkinsci/plugins/workflow/cps/global/WorkflowLibRepository.java
  11. +4 −0 ...src/main/resources/org/jenkinsci/plugins/workflow/cps/global/UserDefinedGlobalVariable/help.jelly
  12. +84 −0 ...ib/src/test/java/org/jenkinsci/plugins/workflow/cps/global/UserDefinedGlobalVariableListTest.java
  13. +1 −1 cps/pom.xml
  14. +66 −0 cps/src/main/java/org/jenkinsci/plugins/workflow/cps/CpsClosure2.java
  15. +8 −2 cps/src/main/java/org/jenkinsci/plugins/workflow/cps/CpsGroovyShell.java
  16. +22 −3 cps/src/main/java/org/jenkinsci/plugins/workflow/cps/CpsScript.java
  17. +1 −2 cps/src/main/java/org/jenkinsci/plugins/workflow/cps/CpsWhitelist.java
  18. +24 −2 cps/src/main/java/org/jenkinsci/plugins/workflow/cps/GlobalVariable.java
  19. +31 −0 cps/src/main/java/org/jenkinsci/plugins/workflow/cps/GlobalVariableSet.java
  20. +3 −4 cps/src/main/java/org/jenkinsci/plugins/workflow/cps/Snippetizer.java
  21. +1 −1 pom.xml
@@ -4,6 +4,7 @@ Only noting significant user changes, not internal code cleanups and minor bug f

## 1.10 (upcoming)

* [JENKINS-26135](https://issues.jenkins-ci.org/browse/JENKINS-26135): expand global library functionality to allow predefined variables and even custom DSLs.
* [JENKINS-29890](https://issues.jenkins-ci.org/browse/JENKINS-29890): `input` step submitter was not being consistently logged.
* [JENKINS-25879](https://issues.jenkins-ci.org/browse/JENKINS-25879), [JENKINS-29875](https://issues.jenkins-ci.org/browse/JENKINS-29875): New API to run long lived tasks that could block on I/O in a separate thread avoiding to block main CPS VM thread.
* [JENKINS-29653](https://issues.jenkins-ci.org/browse/JENKINS-29653): visual tweak to _Snippet Generator_.

This file was deleted.

@@ -64,12 +64,14 @@ public static void register(Object o) {
story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
assertNotNull(LOADER);
System.err.println(LOADER.get());
{
try {
// TODO in Groovy 1.8.9 this keeps static state, but only for the last script (as also noted in JENKINS-23762).
// The fix of GROOVY-5025 (62bfb68) in 1.9 addresses this, which we would get if JENKINS-21249 is implemented.
Field f = ASTTransformationVisitor.class.getDeclaredField("compUnit");
f.setAccessible(true);
f.set(null, null);
} catch (NoSuchFieldException e) {
// assuming that Groovy version is newer
}
MemoryAssert.assertGC(LOADER);
}
@@ -1,7 +1,10 @@
package org.jenkinsci.plugins.workflow.cps.global;

import java.io.File;
import java.util.Arrays;

import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
@@ -31,6 +34,9 @@
@Inject
WorkflowLibRepository repo;

@Inject
UserDefinedGlobalVariableList uvl;

/**
* Have some global libs
*/
@@ -86,4 +92,55 @@ public void globalLib() throws Exception {
}
});
}

/**
* User can define global variables.
*/
@Test
public void userDefinedGlobalVariable() throws Exception {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
File vars = new File(repo.workspace, UserDefinedGlobalVariable.PREFIX);
vars.mkdirs();
FileUtils.writeStringToFile(new File(vars, "acmeVar.groovy"), StringUtils.join(Arrays.asList(
"def hello(name) {echo \"Hello ${name}\"}",
"def foo(x) { this.x = x+'-set'; }",
"def bar() { return x+'-get' }")
, "\n"));
FileUtils.writeStringToFile(new File(vars, "acmeFunc.groovy"), StringUtils.join(Arrays.asList(
"def call(a,b) { echo \"call($a,$b)\" }")
, "\n"));
FileUtils.writeStringToFile(new File(vars, "acmeBody.groovy"), StringUtils.join(Arrays.asList(
"def call(body) { ",
" def config = [:]",
" body.resolveStrategy = Closure.DELEGATE_FIRST",
" body.delegate = config",
" body()",
" echo 'title was '+config.title",
"}")
, "\n"));

// simulate the effect of push
uvl.rebuild();

WorkflowJob p = jenkins.createProject(WorkflowJob.class, "p");

p.setDefinition(new CpsFlowDefinition(
"acmeVar.hello('Workflow');" +
"acmeVar.foo('seed');" +
"echo '['+acmeVar.bar()+']';"+
"acmeFunc(1,2);"+
"acmeBody { title = 'yolo' }",
true));

// build this workflow
WorkflowRun b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));

story.j.assertLogContains("Hello Workflow", b);
story.j.assertLogContains("[seed-set-get]", b);
story.j.assertLogContains("call(1,2)", b);
story.j.assertLogContains("title was yolo", b);
}
});
}
}
@@ -0,0 +1,52 @@
package org.jenkinsci.plugins.workflow.cps.steps;

import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.JenkinsRule;

/**
*
*
* @author Kohsuke Kawaguchi
*/
public class LoadStepTest {

@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher();
@Rule public JenkinsRule r = new JenkinsRule();

@Test
public void basics() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node {\n" +
" writeFile text: 'println(21*2)', file: 'test.groovy'\n" +
" println 'something printed'\n" +// make sure that 'println' in groovy script works
" load 'test.groovy'\n" +
"}", true));
WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
r.assertLogContains("something printed", b);
r.assertLogContains("42", b);
}

/**
* "evaluate" call is supposed to yield a value
*/
@Test
public void evaluationResult() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node {\n" +
" writeFile text: '21*2', file: 'test.groovy'\n" +
" def o = load('test.groovy')\n" +
" println 'output=' + o\n" +
"}"));
WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
r.assertLogContains("output=42", b);
}

}
@@ -143,7 +143,7 @@ public void failure_in_subflow_will_fail_fast() throws Exception {
* FailFast should not kill branches if there is no failure.
*/
@Test @Issue("JENKINS-26034")
public void failFast_has_no_effect_on_suceess() throws Exception {
public void failFast_has_no_effect_on_success() throws Exception {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
p = jenkins().createProject(WorkflowJob.class, "demo");

0 comments on commit 28c9600

Please sign in to comment.
You can’t perform that action at this time.