Stay Hungry,Stay Foolish!

Jenkins pipeline jobs隐式传参

父JOB调用子JOB

父job出发子job,总要带一些参数过去。

https://www.jenkins.io/doc/pipeline/steps/pipeline-build-step/#build-build-a-job

build: Build a job

Triggers a new build for a given job.
    • job
      Name of a downstream job to build. May be another Pipeline job, but more commonly a freestyle or other project. Use a simple name if the job is in the same folder as this upstream Pipeline job; otherwise can use relative paths like ../sister-folder/downstream or absolute paths like /top-level-folder/nested-folder/downstream.
      • Type: String
    • parameters (optional)
      A list of parameters to pass to the downstream job. When passing secrets to downstream jobs, prefer credentials parameters over password parameters. See the documentation for details.
      Array / List of Nested Choice of Objects
  • propagate (optional)

    If enabled (default state), then the result of this step is that of the downstream build (e.g., success, unstable, failure, not built, or aborted). If disabled, then this step succeeds even if the downstream build is unstable, failed, etc.; use the result property of the return value as needed.

    • Type: boolean
  • quietPeriod (optional)
    Optional alternate quiet period (in seconds) before building. If unset, defaults to the quiet period defined by the downstream project (or finally to the system-wide default quiet period).
    • Type: int
  • wait (optional)
    • Type: boolean

 

例子

https://stackoverflow.com/questions/36306883/how-can-i-trigger-another-job-from-a-jenkins-pipeline-jenkinsfile-with-github

build job: 'your-job-name', 
    parameters: [
        string(name: 'passed_build_number_param', value: String.valueOf(BUILD_NUMBER)),
        string(name: 'complex_param', value: 'prefix-' + String.valueOf(BUILD_NUMBER))
    ]

 

JOB输入参数定义

一般job都有自己定义的参数:

https://devopscube.com/declarative-pipeline-parameters/

pipeline {
    agent any
    stages {
        stage('Setup parameters') {
            steps {
                script { 
                    properties([
                        parameters([
                            choice(
                                choices: ['ONE', 'TWO'], 
                                name: 'PARAMETER_01'
                            ),
                            booleanParam(
                                defaultValue: true, 
                                description: '', 
                                name: 'BOOLEAN'
                            ),
                            text(
                                defaultValue: '''
                                this is a multi-line 
                                string parameter example
                                ''', 
                                 name: 'MULTI-LINE-STRING'
                            ),
                            string(
                                defaultValue: 'scriptcrunch', 
                                name: 'STRING-PARAMETER', 
                                trim: true
                            )
                        ])
                    ])
                }
            }
        }
    }   
}

 

这些参数定义,用于规定用户在web界面上点击 “Build with parameters“时候填写。

如下:

 

 

 

问题来了

因为“Build with parameters“对应的界面为用户操作页面,此参数为用户定义。

但是做CI集成,需要从父亲JOB传递更多的参数给子JOB, 不能在用户界面上新增参数。

 

如何实现?

 

pipeline的build指令, 只有传递参数的parameters参数,其它无传递数据的参数。

https://www.jenkins.io/doc/pipeline/steps/pipeline-build-step/#build-build-a-job

 

环境变量是否可以实现?

首先全局环境变量,为系统配置区域配置,不能再父JOB的脚本中运行时动态添加。

 

考虑 withenv 指定?

经过测试,很遗憾地发现,将下面例句中的 sh 行替换为 build 语句, 在子job中不能获得withenv中定义的环境变量。

这种方法只能针对 父JOB启动的直接的进程生效。

https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/#withenv-set-environment-variables

withEnv: Set environment variables

Sets one or more environment variables within a block. These are available to any external processes spawned within that scope. For example:
node {
  withEnv(['MYTOOL_HOME=/usr/local/mytool']) {
    sh '$MYTOOL_HOME/bin/start'
  }
}

(Note that here we are using single quotes in Groovy, so the variable expansion is being done by the Bourne shell, not Jenkins.)

See the documentation for the env singleton for more information on environment variables.

  • overrides
    A list of environment variables to set, each in the form VARIABLE=value or VARIABLE= to unset variables otherwise defined. You may also use the syntax PATH+WHATEVER=/something to prepend /something to $PATH.

 

build Job指令本身就支持

如下, 他说虽然子JOB的parameters没有定义参数, 你仍然可以在父JOB中,使用build job指令传递 没有在子JOB中定义的参数。

这样就不影响“build with parameters”对应的用户操作界面。

https://stackoverflow.com/questions/39207924/jenkins-how-to-get-and-use-upstream-info-in-downstream

1
 

You can simply use params.variableName in your downstream job to retrieve the parameters passed from your upstream parameter job. Your downstream job need not necessarily be a parameterized job.

 

posted @ 2021-04-07 16:46  lightsong  阅读(2154)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel