YaOg
YaOg

Reputation: 1768

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?

Upvotes: 6

Views: 4718

Answers (1)

Christian.D
Christian.D

Reputation: 949

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}"
      }
    }
  }
}

Upvotes: 7

Related Questions