我有一个 Jenkins 管道,它的步骤之一是执行一个 python 脚本(这个 python 脚本只是返回一个“hello world”字符串),我想将其返回值而不是打印值存储在变量中,我的管道阶段看起来像这样:
stage("Execute"){
steps {
script {
sh 'python3 -V'
def command = "python3 test.py "
def process = Runtime.getRuntime().exec(command)
def output = process.in.text
process.waitFor()
println output
}
}
}
上面的代码能够成功打印出python3版本,但是运行时会返回python not found错误Runtime.getRuntime().exec(command)
Caused: java.io.IOException: Cannot run program "python3": error=2, No such file or directory
我的管道中是否缺少任何 python 路径配置?
您已经知道如何使用
sh
步骤,为什么还要费心所有 Runtime
的东西呢?只要这样做:
stage("Execute"){
steps {
script {
def output = sh script: 'python3 test.py', returnStdout: true
echo output
}
}
}