Jenkinsfile で次のように Pipeline を記述し、ビルドトリガで「SCMをポーリング」を設定して、Git リポジトリのフックで Jenkins の notifyCommit の URL を呼び出してプッシュの都度 Pipeline が実行されるようにしています。
groovy
1stage('test') { 2 parallel one: { 3 node('ore') { 4 checkout(scm) // (1) 5 sh 'uname -n' 6 sh 'git log -1' 7 sleep 10 8 } 9 }, 10 two: { 11 node('are') { 12 checkout(scm) // (2) 13 sh 'uname -n' 14 sh 'git log -1' 15 sleep 10 16 } 17 } 18} 19stage('deploy') { 20 node('sore') { 21 checkout(scm) // (3) 22 sh 'uname -n' 23 sh 'git log -1' 24 } 25}
Pipeline を複数のノードで実行するため、それぞれのノードで checkout(scm)
しているのですが、短時間に連続してリポジトリへプッシュすると、上記の (1)
(2)
(3)
で異なるコミットがチェックアウトされることがありました(Pipeline の実行中にリポジトリの master などのブランチが動いてしまうため)。
Jenkins Pipeline を使わずにフリースタイルプロジェクトで Parameterized Trigger で Pass through Git Commit that was built を指定して後段のプロジェクトへコミットを渡すようにしていたときは、このような問題は無かったのですが・・・
いろいろ試行錯誤したところ、フリースタイルプロジェクトで Parameterized Trigger を使って、Pipeline プロジェクトへ Pass through Git Commit that was built でコミットを渡すと↑の Jenkinsfile でもすべての checkout(scm)
で同じコミットをチェックアウトさせられることはわかりました。が、できれば Pipeline プロジェクト単体でやりたいと思っています。
Pipeline プロジェクト単体で、複数のノードにチェックアウトするときに同じコミットが使われるようにすることはできないものでしょうか?
追記
↑の Jenkinsfile で Pipeline プロジェクト単体のときは下記のように、
> git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url git@git.example.com:ore/are.git # timeout=10 Pruning obsolete local branches Fetching upstream changes from git@git.example.com:ore/are.git > git --version # timeout=10 using GIT_SSH to set credentials > git fetch --tags --progress git@git.example.com:ore/are.git +refs/heads/*:refs/remotes/origin/* --prune > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/refs/heads/master^{commit} # timeout=10 Checking out Revision 4043d3de1b7e92c87c1a5ca0b610f4b0305907ea (refs/remotes/origin/master) Commit message: "hoge" > git config core.sparsecheckout # timeout=10 > git checkout -f 4043d3de1b7e92c87c1a5ca0b610f4b0305907ea
フリースタイルの Parameterized Trigger からコミットを渡したときは下記のように、
> git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url git@git.example.com:ore/are.git # timeout=10 Pruning obsolete local branches Fetching upstream changes from git@git.example.com:ore/are.git > git --version # timeout=10 using GIT_SSH to set credentials > git fetch --tags --progress git@git.example.com:ore/are.git +refs/heads/*:refs/remotes/origin/* --prune Checking out Revision d8a913b5b5741a06488bb39ebad0620f9911431c (refs/remotes/origin/master) Commit message: "hoge" > git config core.sparsecheckout # timeout=10 > git checkout -f d8a913b5b5741a06488bb39ebad0620f9911431c
前者は下記のようにブランチ名からコミットIDを特定しているのに対して、後者はいきなりコミットID指定でチェックアウトされています。
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/refs/heads/master^{commit} # timeout=10
前者と後者では checkout(scm)
の scm
にチェックアウトすべきコミットIDが設定されているかどうかの違いのように思うので、なんらかの方法でビルドのトリガとなったコミットのIDを Jenkinsfile の中で取得して scm
に設定してらればいいような気はしているのですが。。。
あなたの回答
tips
プレビュー