简体   繁体   English

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

[英]Get output of a python script into a variable in Jenkinsfile

I have a Python script which returns a string on stdout.我有一个 Python 脚本,它在标准输出上返回一个字符串。 The value rerturned by the python script can be collected in a bash script like below: python 脚本返回的值可以在 bash 脚本中收集,如下所示:

#!/bin/bash

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

On a scripted pipeline with the Jenkinsfile written in Groovy, I need to call my python script from the Jenkinsfile and collect the output into a variable in the Jenkinsfile .在使用Jenkinsfile编写的 Jenkinsfile 的脚本化管道上,我需要从 Jenkinsfile 调用我的Jenkinsfile脚本并将Jenkinsfile收集到 Jenkins 文件中的变量中。

Question:问题:
How do I do this provided that my Jenkinsfile run on a macOS node.如果我的 Jenkinsfile 在 macOS 节点上运行,我该怎么做。 Following is the way how I can at least get the output in a shell variable.以下是我至少可以在 shell 变量中获得 output 的方法。

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'
    }
}
def outputString = sh returnStdout:true, script:'./my_python_script.py -some_parameter 2>&1'
println "the output string is: ${outputString}"

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取 Jenkinsfile 管道 groovy 中 python 脚本的输出结果 - Get the ouput result for a python script in a Jenkinsfile pipeline groovy 有没有一种方法可以使bash脚本的输出“馈入”到最初执行该操作的python脚本中的变量中? - Is there a way to get the output of a bash script to “feed” into a variable in a python script that executes the action initially? 如何将 Groovy 变量传递给 Jenkinsfile 中的 python 程序 - How to pass the Groovy variable to python program in Jenkinsfile 在变量中获取 Python 中函数的输出 - Get Output of A Function in Python In a variable 使用来自 python 的附加参数运行 shell 脚本并将输出作为 python 变量(API 身份验证) - Run shell script with additional parameters from python and get output as python variable (API authentication) 如何将python脚本的输出存储在变量中 - How to store the output of a python script in a variable 如何将 python 脚本的 output 重定向到 bash 变量? - How to redirect output of python script to bash variable? 获取命令行脚本的输出作为模板变量 - get the output of a command line script as a template variable 如何在不同的python脚本中将python脚本的输出设置为变量? - How to set output of python script as variable in a different python script? Python:将python脚本作为子进程运行并获取输出 - Python: run python script as subprocess and get the output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM