I\'ve create a jenkins pipeline and it is pulling the pipeline script from scm.
I set the branch specifier to \'all
\', so it builds on any change to any bra
If you have a jenkinsfile for your pipeline, check if you see at execution time your branch name in your environment variables.
You can print them with:
pipeline {
agent any
environment {
DISABLE_AUTH = 'true'
DB_ENGINE = 'sqlite'
}
stages {
stage('Build') {
steps {
sh 'printenv'
}
}
}
}
However, PR 91 shows that the branch name is only set in certain pipeline configurations:
For me this worked: (using Jenkins 2.150, using simple Pipeline type - not multibranch, my branch specifier: '**')
echo 'Pulling... ' + env.GIT_BRANCH
Output:
Pulling... origin/myBranch
where myBranch is the name of the feature branch
This is for simple Pipeline type - not multibranch. Using Jenkins 2.150.1
environment {
FULL_PATH_BRANCH = "${sh(script:'git name-rev --name-only HEAD', returnStdout: true)}"
GIT_BRANCH = FULL_PATH_BRANCH.substring(FULL_PATH_BRANCH.lastIndexOf('/') + 1, FULL_PATH_BRANCH.length())
}
then use it env.GIT_BRANCH
Switching to a multibranch pipeline allowed me to access the branch name. A regular pipeline was not advised.
A colleague told me to use scm.branches[0].name
and it worked. I wrapped it to a function in my Jenkinsfile:
def getGitBranchName() {
return scm.branches[0].name
}
Use multibranch pipeline.. not pipeline
In my script..
stage('Build') {
node {
echo 'Pulling...' + env.BRANCH_NAME
checkout scm
}
}
Yields...
Pulling...master