[英]Passing data (variable/parameter) from one downstream job to upstream job in order to pass the data to another downstream job
I have a scenario where I am triggering two downstream jobs one after another sequentially from an upstream job.我有一个场景,我从上游作业依次触发两个下游作业。
I need to return data xyz = 3.1416
(parameter/variable) generated in the first downstream job (Job A) back to the upstream job or read data xyz
(parameter/variable) generated in the first downstream job (Job A) from the upstream job.我需要将第一个下游作业(作业 A)中生成的数据xyz = 3.1416
(参数/变量)返回到上游作业或从上游读取第一个下游作业(作业 A)中生成的数据xyz
(参数/变量)工作。
I want to do that as the upstream job needs to pass this data to the other downstream job (Job B).我想这样做,因为上游作业需要将此数据传递给另一个下游作业(作业 B)。
All these jobs are pipeline jobs.所有这些工作都是管道工作。 I am writing the upstream job as an abstraction layer and to automate the trigger of the 2 downstream jobs sequentially one after another.我将上游作业编写为抽象层,并依次自动触发 2 个下游作业。
There are a couple of approaches trying to solve that problem.有几种方法试图解决这个问题。 Calling jobs up and downstream isn't a good idea, because you can create a circular reference between them (ie: A calls B, that calls A again, that calls B...).向上和下游调用作业不是一个好主意,因为您可以在它们之间创建循环引用(即:A 调用 B,再次调用 A,调用 B...)。 Your Jenkins probably won't break because it is limited by the number of workers, but still...您的 Jenkins 可能不会损坏,因为它受到工人数量的限制,但仍然......
You can store your values in JSON or YAML files and then create Jenkins artifacts using the archiveArtifacts() step and the Copy Artifact plugin.您可以将值存储在 JSON 或 YAML 文件中,然后使用 archiveArtifacts() 步骤和 Copy Artifact 插件创建 Jenkins 工件。 That way, jobs and builds can share information amongst them.这样,作业和构建可以在它们之间共享信息。
There's a way to downstream jobs return values back to upstream jobs, using a resource known as buildVariables.有一种方法可以让下游作业将值返回给上游作业,使用称为 buildVariables 的资源。 Here's the code from the upstream job:这是上游作业的代码:
def ret = build job: 'downstream_job'
print "The returned value from the triggered job was ${ret.buildVariables.RETURNED_VALUE}"
And in the downstream job:在下游工作中:
environment {
RETURNED_VALUE = ""
}
stages {
stage('Doing something') {
steps {
script {
print("Hi, I was triggered!")
env.RETURNED_VALUE = "Blah blah blah"
}
}
}
}
buildVariables can access any environment variable from the downstream job, except build parameters. buildVariables 可以从下游作业访问任何环境变量,构建参数除外。
Best regards.此致。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.