python - 如何将常规输出保存到管道变量
问题描述
我在 jenkisn 管道中有以下代码:
stage ("amd_distribution_input_transformation"){
steps{
script{
amd_distribution_input_transformation url: params.DOMAIN_DESCRIPTOR_URL, secret: params.CERDENITAL_ID
}
}
}
amd_distribution_input_transformation.groovy 内容:
def call(Map parameters)
{
def CREDENITAL_ID = parameters.secret
def DOMAIN_DESCRIPTOR_URL = parameters.url
sh '''
python amd_distribution_input_transformation.py
'''
}
}
在 amd_distribution_input_transformation.py 中,一些代码正在运行,最后,它返回名为“artifacts_list”的对象
我的问题是,如何将 groovy 文件的返回对象分配给管道变量。顺便说一句,如果有帮助,我可以将输出从 python 代码写入 json 文件(在这里我坚持如何最终将该文件分配给管道变量)
解决方案
sh
command 只能捕获脚本的标准输出。
所以,你不能return value from shell script
。你应该打印它。
并使用管道命令的returnStdout:true
参数sh
来获取打印值。
例如你有my.py
python 脚本
import json
# a Python object (dict):
x = {
"name": "John",
"age": 30,
"city": "New York"
}
# convert into JSON:
y = json.dumps(x)
# print the result to transfer to caller:
print(y)
然后在管道中,您可以获得由 python 打印的 json:
def jsonText = sh returnStdout:true, script: "python my.py"
def json=readJSON text: jsonText
//print some values from json:
println json.city
println "name: ${json.name}"
使用的管道步骤:
https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#-sh-shell-script
推荐阅读
- python - Python:导入模块时未定义定义中的默认变量
- angular - 使用 angular-oauth2-oidc 库从 Auth0 获取完全访问令牌
- android - Android 使用 Retrofit 从相机和画廊上传图像
- javascript - 无法在本地画布上绘制远程文件
- javascript - 将 $(document.body).css() 输出与字符串进行比较
- java - 在 HashSet 中添加元素
- c# - 我的船有一个错误,它不想出现在我的游戏中,我不知道为什么
- php - 当前文件夹之外的文件夹的路径
- java - 在扫描仪中使用分隔符;忽略空格和换行
- swift - 异步调用后更新 UITableView 的 numberOfRowsInSection