将 python 脚本的输出获取到 Jenkinsfile 中的变量中

Gam*_*ads 3 python groovy jenkins-pipeline

我有一个 Python 脚本,它在标准输出上返回一个字符串。python 脚本返回的值可以收集在 bash 脚本中,如下所示:

#!/bin/bash

outputString=$(./my_python_script.py -some_parameter 2>&1)
echo "The output string is $outputString"
Run Code Online (Sandbox Code Playgroud)

在使用 Groovy 编写的脚本化管道Jenkinsfile上,我需要从 调用我的 python 脚本Jenkinsfile并将输出收集到Jenkinsfile.

问题:
如果我的 Jenkinsfile 在 macOS 节点上运行,我该如何执行此操作。以下是我至少可以在 shell 变量中获取输出的方法。

stage('My build') {
    node('my_build_node') {
        sh 'output_string=$(./my_python_script.py -some_parameter 2>&1) && sh 'echo The output_string is $output_string'
    }
}
Run Code Online (Sandbox Code Playgroud)

dag*_*ett 5

def outputString = sh returnStdout:true, script:'./my_python_script.py -some_parameter 2>&1'
println "the output string is: ${outputString}"
Run Code Online (Sandbox Code Playgroud)

sh步骤参数详细信息: https://www.jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#sh-shell-script