如何将groovy输出保存到管道variab

2024-04-26 08:14:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Jenkins管道中有以下代码:

   stage ("amd_distribution_input_transformation"){
        steps{
            script{
                    amd_distribution_input_transformation url: params.DOMAIN_DESCRIPTOR_URL, secret: params.CERDENITAL_ID
                }
            }
        }

amd分配输入_转换.groovy内容:

def call(Map parameters)
{
    def CREDENITAL_ID = parameters.secret
    def DOMAIN_DESCRIPTOR_URL = parameters.url
    sh '''
        python amd_distribution_input_transformation.py
      '''
    }             
}

在amd\U分配\U输入中_转换.py一些代码正在运行,最后,它返回名为“artifacts\u list”的对象

我的问题是,如何将groovy文件返回的obect赋值给管道变量。 顺便说一句,如果有帮助的话,我可以从python代码将输出写入json文件(这里我一直在讨论如何最终将该文件分配给管道变量)


Tags: 文件代码urlinputsecret管道domaindef
1条回答
网友
1楼 · 发布于 2024-04-26 08:14:23

sh命令只能捕获脚本的标准输出。你知道吗

所以,你不能return value from shell script。你应该把它打印出来。你知道吗

并为sh管道命令使用returnStdout:true参数来获取打印值。你知道吗

例如,您有my.pypython脚本

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

https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#readjson-read-json-from-files-in-the-workspace

相关问题 更多 >