Answer a question

I have a Groovy script that runs a separate Powershell script as part of a Jenkins pipeline. The Powershell script returns a value (int) and I intend to use that value in the Groovy script by assigning it to a variable but the groovy variable is always evaluated to null. Code below, edited for brevity.

Powershell Script

#Some actions...
$foo = 0
return $foo

Returns (when run manually in Powershell):

0

Groovy Script

stage('StageX'){
    //Some actions...
    def return_val = powershell "Path\\to\\script\\someScript.ps1"
    echo return_val
    if (return_val == 0){
        //Do things...
    }
}

Returns:

null

I know that the script actually runs as it performs other actions that are apparent when the Groovy script runs, I just can't seem to get Groovy to pick up the return value of the Powershell script.

What is the best way to do this?

Answers

You need to pass it the returnStatus parameter:

    def return_val = powershell script: "Path\\to\\script\\someScript.ps1", returnStatus: true

See the documentation for more information https://www.jenkins.io/blog/2017/07/26/powershell-pipeline/

Logo

CI/CD社区为您提供最前沿的新闻资讯和知识内容

更多推荐