管道作业 - 传递参数上游? [英] Pipeline jobs - pass parameters upstream?

查看:121
本文介绍了管道作业 - 传递参数上游?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL:DR:显然在Jenkins管道作业中,您可以轻松地向下游传递参数。我想知道的是如果你可以通过上游

用例:



我们有三个工作; job_one job_two job_three 。由于只需要一个阶段,所以这些通常是分开运行的,但是在越来越频繁的情况下,我们希望能够将所有三个阶段连续运行。

第一个和第二个依赖可以提前定义的参数,但第三个需要从第二个作业生成的参数(文件名的结构未知,直到job_two运行)。



我已经构建了,它为每个作业调用如下内容。在这种情况下,会填充 PARAM1 ,因为伞将作为使用参数构建运行。


$ b $ pre $ 编译作业:'job_one',参数:[[$ class:'StringParameterValue',name:'PARAM1',value: $ PARAM1]]

所有罚款和花花公子,我可以使用 PARAM1 in job_one 就好。



问题:



对于 job_three 我需要参数 filename 。这是在 job_two 中生成的,因此我从中可以看出无法访问,因为job_three不知道job_two在做什么。



在一个理想的世界中,我只需要将文件名传递给作业,这会将它反馈回来进入job_three。因此,如何将生成的文件名传递给伞工作?



我正在描绘一个类似于此的最终脚本;

  node('on-demand-t2small'){

stage('Build 1'){

build job:'job_one',parameters:[[$ class:'StringParameterValue',name:'PARAM1',value:'$ PARMA1 ]]
}
stage('Build 2'){

build job:'job_two',parameters:[[$ class:'StringParameterValue',name:'PARAM2 ',value:$ PARMA2]]

//以某种方式将文件名参数从job_two中取出,以便我可以将它移至作业三...
}
stage('Build 3'){

build job:'job_three',参数:[[$ class:'StringParameterValue',name:'filename',value:'$ filename']]



$ b $ h $>附加说明:

我承认第一个问题将是为什么没有job_two触发job_three?我无法以这种方式设置系统,原因有两个:


  1. job_two需要能够在不触发job_three的情况下运行,并且三个不一定需要两个输入才能运行。 b $ b
  2. 我辩论让伞开始两个,然后在两个条款中有一个条款,如果它是由伞开始的,它只会触发三个条款,但据我所知,这将限制伞工作中的反馈;你不会知道两个失败是因为两个失败,还是因为三个(作为两个部分)失败。如果我对此假设有误,请告诉我们。

我曾考虑将参数设置为环境变量,但我相信这是特定于节点的,我不能保证这两个作业将在同一个节点上运行,因此似乎不是解决方案。



伞是用groovy编写的管道作业,其他三个可能是流水线或自由式的工作,如果这很重要的话。



如果可能的话,我会很感激详细的答案,我还是Groovy,Jenkins和编码的新手一般来说。

解决方案

它应该很简单:

  stage('Build 3'){
res = build job:'job_three',parameters:[[$ class:'StringParameterValue',name:'filename',value:'$ filename ]]
echo$ res.buildVariables.filename
}

假设你在job_three中做
$ b $ pre $ lt; code> env.filename =col新文件名


TL;DR: Obviously in a Jenkins pipeline job you can easily pass parameters downstream. What I want to know is if you can pass them upstream.

Use case:

We have three jobs; job_one, job_two, and job_three. These are frequently run separately as only one stage is needed, but in increasingly more-frequent cases we'd like to be able to run all three back to back.

The first and second rely on parameters you can define ahead of time, but the third needs a parameter that is generated from the second job (a file name whose structure is unknown until job_two runs).

I have built umbrella, which calls something like the following for each job. In this case, PARAM1 is populated because umbrella runs as "Build with parameters".

build job: 'job_one', parameters: [[$class: 'StringParameterValue', name: 'PARAM1', value: "$PARAM1"]]

All fine and dandy, I can then use PARAM1 in job_one just fine.

The Problem:

For job_three I need the parameter filename. This is generated within job_two, and therefore from what I can tell is inaccessible because job_three has no idea what job_two is doing.

In an ideal world I would just have job_two pass the filename to the umbrella job, which would feed it back into job_three. Therefore, how can I pass the generated filename back up to the umbrella job?

I'm picturing a final script something like this;

node('on-demand-t2small'){

    stage ('Build 1') {

        build job: 'job_one', parameters: [[$class: 'StringParameterValue', name: 'PARAM1', value: "$PARMA1"]]
}
    stage ('Build 2') {

        build job: 'job_two', parameters: [[$class: 'StringParameterValue', name: 'PARAM2', value: "$PARMA2"]]

    //somehow get the filename parameter out of job_two here so that I can move it to job three...
} 
    stage ('Build 3') {

        build job: 'job_three', parameters: [[$class: 'StringParameterValue', name: 'filename', value: "$filename"]]
} }

Additional Notes:

I recognize that the first question will be "why not have job_two trigger job_three? I can't set the system up this way for two reasons;

  1. job_two needs to be able to run without triggering job_three, and three can't always require two's input to run.
  2. I debated having the umbrella kick off two and then have a clause in two that would trigger three ONLY IF it had been started by the umbrella, but as far as I can tell this will limit feedback in the umbrella job; you won't know if two failed because two failed, or because three (as a part of two) failed. If I'm wrong about this assumption please let me know.

I had thought about setting the parameter as an environment variable but I believe that's node-specific and I can't guarantee both jobs will run on the same node so that seemed to not be the solution.

Umbrella is a pipeline job written in groovy, the other three may be pipeline or freestyle jobs, if that matters.

I would appreciate detailed answers where possible, I'm still new to Groovy, Jenkins, and coding in general.

解决方案

It should be as simple as that:

stage ('Build 3') {
        res = build job: 'job_three', parameters: [[$class: 'StringParameterValue', name: 'filename', value: "$filename"]]
        echo "$res.buildVariables.filename"
}

Assuming that in job_three you do

env.filename = "col new file name"

这篇关于管道作业 - 传递参数上游?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆