Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request #131 from jenkinsci/user-defined-global-libs
Browse files Browse the repository at this point in the history
[JENKINS-26135] Allow CPS global lib scripts to define global variables
  • Loading branch information
jglick committed Aug 20, 2015
2 parents 4423736 + 4b48f8a commit 28c9600
Show file tree
Hide file tree
Showing 21 changed files with 662 additions and 96 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -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_.
Expand Down

This file was deleted.

Expand Up @@ -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);
}
Expand Down
@@ -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;
Expand Down Expand Up @@ -31,6 +34,9 @@ public class WorkflowLibRepositoryTest {
@Inject
WorkflowLibRepository repo;

@Inject
UserDefinedGlobalVariableList uvl;

/**
* Have some global libs
*/
Expand Down Expand Up @@ -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);
}

}
Expand Up @@ -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");
Expand Down

0 comments on commit 28c9600

Please sign in to comment.