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.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM