简体   繁体   English

将数据(变量/参数)从一个下游作业传递到上游作业,以便将数据传递到另一个下游作业

[英]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 个下游作业。

structure / flowchart of jobs工作结构/流程图

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 可能不会损坏,因为它受到工人数量的限制,但仍然......

Solution A: Use artifacts解决方案 A:使用工件

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.这样,作业和构建可以在它们之间共享信息。

Solution B: Use buildVariables解决方案 B:使用 buildVariables

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.

相关问题 如何在詹金斯中将参数作为环境变量从上游作业传递到下游作业? 无需对下游工作进行参数化 - How to pass parameter as environment variable from upstream job to downstream job in jenkins? without downstream jobs being parametrized Jenkins的上游和下游工作 - Upstream and DownStream Job on Jenkins 将运行参数传递给下游作业 - passing a run parameter to a downstream job 如何在不构建上游作业的情况下将参数从上游作业传递给下游作业 - how to pass parmeter to downstream job from upstream job without building upstream job 如何将参数传递给下游作业,但不应该触发下游作业? - How to pass parameter to the downstream job but shouldn't trigger the downstream job? 是否可以将一个詹金斯工作与另一个工作联系起来并建立上下游关系? - Is it possible to link one Jenkins job to another and establish an upstream/downstream relationship? Jenkins-如何将参数从下游作业传递到其父上游? - Jenkins - How to pass parameters from a downstream job to its parent upstream? 如何将文件传递给阻止上游作业的下游作业? - How to pass file to downstream job which blocks upstream job? 如何在 Jenkins MultiJob 中将下游作业的描述传递给上游作业 - How to pass Description of downstream job to upstream job in Jenkins MultiJob 上游作业中添加的两个参数中只有一个适用于下游作业 - Only one of two parameters added in upstream job works in downstream job
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM