Declarative Pipeline で 自動Checkoutしたブランチ名を取得する

取得した値は環境変数 BUILD_BRANCH に登録する.

Jenkinsの設定画面

pipeline の場合

SCMの追加処理で “specific local branch” を追加する.

multi branch pipeline で Branch Sources に GitHubを使用している場合

Additional で “Check out to matching local branch” を追加する.

Jenkinsfile

最初のstageとして以下を追加

stage('pre setup'){
  steps{
    script{
      env.BUILD_BRANCH = sh(returnStdout: true, script: 'git rev-parse --abbrev-ref @').trim()
    }
  }
}

続きを読む Declarative Pipeline で 自動Checkoutしたブランチ名を取得する