LoginSignup
0
1

More than 3 years have passed since last update.

Jenkins 下流ジョブから上流ジョブのパラメータを取得する方法

Last updated at Posted at 2019-12-29

Docker Imageをビルドしている上流ジョブの終了をトリガーに、
できたてほやほやのイメージ使ってテストするジョブを作ろうと思い、下流ジョブから上流ジョブのビルドパラメータを取る方法を調べたのでカキコ

準備

  1. [Jenkinsの管理] -> [In-process Script Approval] をクリック
  2. 以下のように"method hudson.model.Run getEnvironment"と"method org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper getRawBuild"を承認欄に追加 image.png

スクリプト

以下の行ををジョブに組み込めば、環境変数経由で上流ジョブのパラメータの取得が可能

stage('Get Upstream Parameter') {
    steps {
        script {
            upstreamBuilds = currentBuild.upstreamBuilds
            if (upstreamBuilds.isEmpty()) {
                // Job is run by manually -> Do nothing.
            } else {
                // Job is run by trigger of upstream success.
                def upstream = upstreamBuilds[0].rawBuild
                def upstreamEnv = upstream.environment
                echo upstreamEnv["container_name"]
                echo upstreamEnv["branch_name"]
            }
         }
     }
}

方法2:上流ジョブから、パラメータ付きで下流ジョブを実行

こっちは簡単で、以下のように書くだけでOK。

build job: 'DownstreamJob', parameters: [
    [$class: 'StringParameterValue', name: 'container_name', value: "${params.container_name}"]
]

ただ、上流側に呼び出し処理を実装すると、
今回のDocker image buildみたいに不特定多数のテストジョブの上流になる場合、
テストジョブが増えるたびに上流スクリプトに更新がかかることになるので、
「上流から下流へパラメータつきでジョブを実行する」のか「下流から上流のパラメータを取りにいく」のかは、作ろうとしてるテストから考えた方がよさそう

参考資料

Jenkinsの仕様を扱った資料少ないなぁ思ってたけど、探してみると意外と出てきますね(単なる勉強不足だった..)

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1