将 Powershell 脚本的返回值分配给 Groovy 变量

Raw*_*amb 2 powershell groovy jenkins

我有一个 Groovy 脚本,它运行一个单独的 Powershell 脚本作为 Jenkins 管道的一部分。Powershell 脚本返回一个值 ( int),我打算通过将其分配给变量来在 Groovy 脚本中使用该值,但 groovy 变量始终计算为 null。下面的代码,为简洁起见进行了编辑。

Powershell脚本

#Some actions...
$foo = 0
return $foo
Run Code Online (Sandbox Code Playgroud)

返回(在 Powershell 中手动运行时):

0
Run Code Online (Sandbox Code Playgroud)

Groovy 脚本

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

返回:

null
Run Code Online (Sandbox Code Playgroud)

我知道该脚本实际运行时会执行 Groovy 脚本运行时明显的其他操作,但我似乎无法让 Groovy 获取 Powershell 脚本的返回值。

做这个的最好方式是什么?

sme*_*elm 7

您需要向其传递参数returnStatus

    def return_val = powershell script: "Path\\to\\script\\someScript.ps1", returnStatus: true
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅文档https://www.jenkins.io/blog/2017/07/26/powershell-pipeline/