父JOB調用子JOB
父job出發子job,總要帶一些參數過去。
https://www.jenkins.io/doc/pipeline/steps/pipeline-build-step/#build-build-a-job
build
: Build a jobTriggers 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)Array / List of Nested Choice of ObjectsA 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.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 variablesSets 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 formVARIABLE=value
orVARIABLE=
to unset variables otherwise defined. You may also use the syntaxPATH+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
1You 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.