0

show branch in Jenkins file

Hello @everyone, can someone help me? I'm trying to build a Jenkinsfile pipeline. I have already configured my Jenkins server to trigger with a GitBucket based on a push event in the repository. Now, in case I have multiple branches, I need to know which branch was pushed. I have tried all the alternatives, but it only gives me "Branch null.:(

stage('show branch')
{
// v1
// def payload = readJSON file: 'payload.json'
// def branch = payload.ref.split('/').last()
// sh "echo 'Branch name: ${branch}'"
// v2
// checkout scm
// def branchName = scm.branches[scm.branches.length-1].name
// echo "The branch name is $branchName"
// v3
// echo "The branch v3 name is $BB_BRANCH"
// v4
// echo "Branch :${env.GIT_BRANCH}"
// v5
// checkout scm
// def branchName = sh(script: 'git symbolic-ref -q --short HEAD', returnStdout: true).trim()
// echo "The branch v5 name is $branchName"
// v6
// echo "The branch v6 name is $BITBUCKET_SOURCE_BRANCH"
// v7
echo "Branch v7 :${env.BRANCH_NAME}"
}

gives me "Branch null.:(

  • Welcome to SO! Please check your question, the output does not match with the provided code. And there is a lot of dead code in your question, please remove any unrelated things that prevents understanding your question clearly (cf. [mre]). Btw. how is Gitbucket related to your question? Please add all the clarifications by editing your question. – hakre May 23 '23 at 15:00
  • 1
    Does this answer your question? [Get git branch name in Jenkins Pipeline/Jenkinsfile](https://stackoverflow.com/questions/42383273/get-git-branch-name-in-jenkins-pipeline-jenkinsfile) – hakre May 23 '23 at 15:02

0 Answers0