Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jenkins pipeline PR build contains wrong branch name

I am using Jenkins multi branch pipeline with bitbucket and I see an issue where the automatic build created for a PR fails as I rely on env.BRANCH_NAME. Problem is that this env now holds not the feature branch name as expected, instead it holds the PR is (e.g. PR-2 instead of feature/test-branch).

I have code in my job that pushes to branch based on the BRANCH_NAME. This code obviously now fails as there is no branch named PR-2.

Anyone saw this before and has a workaround?

like image 629
YaOg Avatar asked Feb 19 '18 15:02

YaOg


1 Answers

I have a stage in my pipeline setting the build name accordingly in case I have to use the CHANGE_BRANCH instead of the normal branch name.

stage('Set Build Name') {
  steps {
    script {
      if (env.BRANCH_NAME.startsWith('PR')) {
        currentBuild.displayName = "#${env.BUILD_NUMBER} - ${env.CHANGE_BRANCH}"
      } else {
        currentBuild.displayName = "#${env.BUILD_NUMBER} - ${env.BRANCH_NAME}"
      }
    }
  }
}
like image 52
Christian.D Avatar answered Nov 07 '22 04:11

Christian.D