2017-02-28 211 views
-1

我有一個名爲MyTests的布爾參數作爲構建作業的一部分。該作業調用Groovy腳本。我相信下面的特定代碼在腳本中引起了一個問題。任何想法在Groovy中引用if語句中的布爾值的正確方法。Jenkins Groovy腳本

stage("post_build") { 
    if (${params.MyTests}) { 
     my_code_block... 
     } 

java.lang.NoSuchMethodError: No such DSL method '$' found among steps

回答

1

取出${...}並直接寫入param.MyTests${...}只能在引用變量(或常規groovy/java表達式)內部的字符串時使用。所以:

def foo = "bar" 
echo foo 
echo "Withing a string: ${foo}" 

所以你的情況:

stage("post_build") { 
    if (params.MyTests) { 
     my_code_block... 
    } 
    ... 
+0

將設定了上述自動評估爲真或是否需要添加=真.. – user2040074

+0

沒有,'params.MyTests'會?是一個布爾型對象,請參閱下面的Gerold Broser的回答中的引用,基本上'params'中的對象將具有對應於參數類型的對象。 –

0

Pipeline Syntax, Flow Control

stage('Example') { 
    if (env.BRANCH_NAME == 'master') { 
     echo 'I only execute on the master branch' 
    } 
} 

JENKINS-27295

I would say that it is a best practice to always use the params object if you want the ensure that the type is consistent. Referencing the parameter as either foo or env.foo returns the value as it was injected into an environment variable and will always be of type String .

properties([parameters([booleanParam(defaultValue: false, description: '', name: 'foo')])]) 

echo "foo: " + foo.getClass().toString() 
echo "env.foo: " + env.foo.getClass().toString() 
echo "params.foo: " + params.foo.getClass().toString() 

returns:

[Pipeline] echo 
foo: class java.lang.String 
[Pipeline] echo 
env.foo: class java.lang.String 
[Pipeline] echo 
params.foo: class java.lang.Boolean 
[Pipeline]