groovy - Jenkins script reference parameters as params.parameterName or just parameterName? - Stack Overflow

In Jenkins, how to decide whether we need to reference parameters as params.parameterName or just param

In Jenkins, how to decide whether we need to reference parameters as params.parameterName or just parameterName on the scripting context within the pipeline?

For example in the following script:

def sonarAnalysisFn(sonarAnalysis) {
    if (sonarAnalysis == true) {  
        echo "Sonar analysis is running"
    } else {
        echo "Skipping Sonar Analysis"
    }
}

node {
    properties([
        parameters([
            string(name: 'name', defaultValue: 'World', description: 'Enter your name'),
            booleanParam(defaultValue: true, description: 'Untick if you want to skip Sonar analysis', name: 'sonarAnalysis')
        ])
    ])
    
    stage('Greet') {
        echo "Hello, ${name}"  // Explicitly using params.name
    }
    
    stage('Sonar Analysis') {
        sonarAnalysisFn(sonarAnalysis)
    }
}

I have passed both name and sonarAnalysis parameters directly, but only the name is passed from the input. sonarAnalysis always pass the True (default value).

But when I use params.sonarAnalysis, it works just fine.

So I jut need to know, when does jenkins consider whether it is a passed parameter or not?

In Jenkins, how to decide whether we need to reference parameters as params.parameterName or just parameterName on the scripting context within the pipeline?

For example in the following script:

def sonarAnalysisFn(sonarAnalysis) {
    if (sonarAnalysis == true) {  
        echo "Sonar analysis is running"
    } else {
        echo "Skipping Sonar Analysis"
    }
}

node {
    properties([
        parameters([
            string(name: 'name', defaultValue: 'World', description: 'Enter your name'),
            booleanParam(defaultValue: true, description: 'Untick if you want to skip Sonar analysis', name: 'sonarAnalysis')
        ])
    ])
    
    stage('Greet') {
        echo "Hello, ${name}"  // Explicitly using params.name
    }
    
    stage('Sonar Analysis') {
        sonarAnalysisFn(sonarAnalysis)
    }
}

I have passed both name and sonarAnalysis parameters directly, but only the name is passed from the input. sonarAnalysis always pass the True (default value).

But when I use params.sonarAnalysis, it works just fine.

So I jut need to know, when does jenkins consider whether it is a passed parameter or not?

Share Improve this question asked Mar 11 at 8:35 Kavindu KalingaKavindu Kalinga 34 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

In scripted pipeline the parameters should always be used as params.parameterName. Refer here.

If variables are not referred as this, there are chances one can get incorrect/unexpected output.

In the case in example above, if one prints the class type of the variable boolean.

println sonarAnalysis.getClass()

The output will be

class java.lang.String

Meaning the condition for the if will need to be changed. Something as follows:

    if (sonarAnalysis.toBoolean() == true) {  
        echo "Sonar analysis is running"
    } else {
        echo "Skipping Sonar Analysis"
    }

This change would need to be done, to consistently get the correct results from the parameters.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744806645a4594816.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信