How can I have unique build numbers across branches with Jenkins & the Pipeline Multibranch Plugin

后端 未结 3 547
悲&欢浪女
悲&欢浪女 2021-02-13 23:34

We are using Jenkins Pipeline Multibranch Plugin with Blue Ocean.

Through my reading, I believe it is quite common to tie your project\'s build number to the Jenkins run

3条回答
  •  长发绾君心
    2021-02-13 23:43

    You can get the Git branch name from $GIT_BRANCH and add this to $BUILD_NUMBER to make an ID that's unique across branches (as long as your company doesn't do something like get themselves taken over by a large corporation that migrates you to another Jenkins server and resets all the build numbers: to protect against that, you might want to use $BUILD_URL).

    Only snag is $GIT_BRANCH contains the / character, plus any characters you used when naming the branch, and these may or may not be permitted in all the places where you want an ID. ($BUILD_URL is also going to contain characters like : and /) If this is an issue, one workaround would be to delete unwanted characters with tr:

    export MY_ID=$(echo $GIT_BRANCH-$BUILD_NUMBER | tr -dc [A-Za-z0-9-])
    

    (-dc means delete the complement of these characters, so A-Z, a-z, 0-9 and - are the characters you want to keep.)

提交回复
热议问题