Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-38426] Allow non-literal expressions in env vars.
Allowing fall-through delegation and whitelisting getProperty(s) on
ClosureModelTranslator and PropertiesToMapTranslator lets us do env
vars with values like:
- "${currentBuild.getNumber()}"
- currentBuild.getNumber()

Can't do "${PREVIOUSLY_DEFINED_ENV}_something" yet, though. Still
trying to figure that one out.
  • Loading branch information
abayer committed Sep 23, 2016
1 parent b385ba5 commit 7f05b70
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
Expand Up @@ -41,7 +41,9 @@
public class MethodMissingWrapperWhitelist extends Whitelist {
@Override
public boolean permitsMethod(@Nonnull Method method, @Nonnull Object receiver, @Nonnull Object[] args) {
return (method.getName().equals("invokeMethod") || method.getName().equals("setProperty"))
return (method.getName().equals("invokeMethod") ||
method.getName().equals("setProperty") ||
method.getName().equals("getProperty"))
&& MethodMissingWrapper.class.isAssignableFrom(receiver.getClass());
}

Expand Down
Expand Up @@ -195,7 +195,7 @@ public class ClosureModelTranslator implements MethodMissingWrapper, Serializabl
private void resolveClosure(Object closureObj, Object translator) {
Closure argClosure = closureObj
argClosure.delegate = translator
argClosure.resolveStrategy = Closure.DELEGATE_ONLY
argClosure.resolveStrategy = Closure.DELEGATE_FIRST
argClosure.call()
}
}
Expand Down
Expand Up @@ -53,7 +53,7 @@ public class ModelInterpreter implements Serializable {
ClosureModelTranslator m = new ClosureModelTranslator(Root.class)

closure.delegate = m
closure.resolveStrategy = Closure.DELEGATE_ONLY
closure.resolveStrategy = Closure.DELEGATE_FIRST
closure.call()

Root root = m.toNestedModel()
Expand Down
Expand Up @@ -45,4 +45,19 @@ public void simpleEnvironment() throws Exception {
j.assertLogContains("FOO is BAR", b);
}

@Test
public void nonLiteralEnvironment() throws Exception {
prepRepoWithJenkinsfile("nonLiteralEnvironment");

DumbSlave s = j.createOnlineSlave();
s.setLabelString("some-label");

WorkflowRun b = getAndStartBuild();
j.assertBuildStatusSuccess(j.waitForCompletion(b));
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("FOO is BAR", b);
j.assertLogContains("BUILD_NUM_ENV is 1", b);
j.assertLogContains("ANOTHER_ENV is 1", b);
}

}
43 changes: 43 additions & 0 deletions src/test/resources/nonLiteralEnvironment.groovy
@@ -0,0 +1,43 @@
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

pipeline {
environment {
FOO = "BAR"
BUILD_NUM_ENV = currentBuild.getNumber()
ANOTHER_ENV = "${currentBuild.getNumber()}"
}

agent label:"some-label"

stages {
stage("foo") {
sh 'echo "FOO is $FOO"'
sh 'echo "BUILD_NUM_ENV is $BUILD_NUM_ENV"'
sh 'echo "ANOTHER_ENV is $ANOTHER_ENV"'
}
}
}


0 comments on commit 7f05b70

Please sign in to comment.