Issue
I am struggling with setting up my CI/CD
with my project. My SCM is gitlab
and CI is jenkins
.
I have created a pipeline script which works very well. Also I am able to set up the gitlab
webhook
which triggers the build in jenkins
without any issue.
Here is my sample pipeline script:
def running(gitlabBuildName) {
updateGitlabCommitStatus(name: "${gitlabBuildName}", state: 'running')
}
def success(gitlabBuildName) {
updateGitlabCommitStatus(name: "${gitlabBuildName}", state: 'success')
}
def failure(gitlabBuildName) {
updateGitlabCommitStatus(name: "${gitlabBuildName}", state: 'failed')
}
properties ([
[$class: 'GitLabConnectionProperty', gitLabConnection: 'gitlab'],
pipelineTriggers([[
$class: "GitLabPushTrigger",
triggerOnPush: true,
triggerOnMergeRequest: true,
// triggerOpenMergeRequestOnPush:"both",
//triggerOnNoteRequest: true,
//noteRegex: "CI_STAGE_BUILD_THIS",
// skipWorkInProgressMergeRequest: true,
// ciSkip:false ,
//setBuildDescription: true,
//addNoteOnMergeRequest:true,
addCiMessage: true,
addVoteOnMergeRequest: true,
acceptMergeRequestOnSuccess: true,
branchFilterType: "NameBasedFilter",
//targetBranchRegex: "some-branch",
includeBranchesSpec: "master,stage,prod",
excludeBranchesSpec: "dev"
]])
]);
pipeline {
agent {
label 'amazon-cloud'
}
options {
gitLabConnection('gitlab')
timeout(time:1, unit: 'HOURS')
}
environment {
WORK_DIR = "${WORKSPACE}/${BUILD_NUMBER}"
}
stages {
stage('PREDEPLOYMENT:: Cleanup the VM. '){
steps{
running("${JOB_NAME}")
echo "Running for ${JOB_NAME}"
echo "Deleting previous images. "
dir("$WORKSPACE"){
sh 'rm -rf *'
}
}
post{
success {
echo "Success: VM cleaned up for further tests "
}
failure {
echo "Error: Some error occured while cleaning up the system"
failure("${JOB_NAME}")
}
}
}
stage('CHECKOUT: Checkout the 1st project Repos '){
steps {
checkout([$class: 'GitSCM', branches: [[name: "my-dev"]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: '$BUILD_NUMBER/my-node-1']],
submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${GIT_SSH_KEY_ID}",
url: 'https://git_url/project_name1.git']]])
checkout([$class: 'GitSCM', branches: [[name: "my-dev"]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: '$BUILD_NUMBER/my-node-2']],
submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${GIT_SSH_KEY_ID}",
url: 'https://git_url/project_name2.git']]])
}
post {
success {
echo "Success: Git checkout done for repos. "
echo "=========="
echo "${env.GIT_BRANCH}" // prints null
}
failure {
echo "Error: Some error occured while Git checkout of repos."
failure("${JOB_NAME}")
}
}
}
stage('CHECKOUT: Checkout the project2 Repos '){
steps{
checkout([$class: 'GitSCM', branches: [[name: "new-ui"]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: '$BUILD_NUMBER/project_name2']],
submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${GIT_SSH_KEY_ID}",
url: 'https://git_url/project_name2.git']]])
}
post {
success {
echo "Success: Git checkout done for project_name2 repos. "
}
failure {
echo "Error: Some error occured while Git checkout of project_name2 repos."
failure("${JOB_NAME}")
}
}
}
stage('BUILD: Temp: Prints env variables. ') {
steps {
echo "${BRANCH_NAME}"
}
post {
success {
echo "Success: Successfully created the latest local build and tagged it."
}
failure {
echo "Error: Couldn't create the image. "
failure("${JOB_NAME}")
}
}
}
stage('Cleanup: Clean the VM Now. ') {
steps {
//sh 'docker rmi $(docker images -a -q) | echo "Not able to delete some images"'
cleancleanWs()
}
post {
success {
echo "Success: Cleaned up the vm. "
success("${JOB_NAME}")
}
failure {
echo "Error: Some error occured while cleaning up the vm. "
failure("${JOB_NAME}")
//cleanWs()
}
}
}
}
}
But the problem with above code is I am not able to find the branch name by the help of any ENV variables.
For exa. My Jenkins
job runs when a changes are pushed to a branch. But I want to get the branch name that has been checkout (where changes are pushed) and baed on that, I want to run some extra scripts when branch is "stage".
I tried to use "env.BRANCH_NAME"
etc, but all are showing null.
Can anyone please help me, to just get the branch name.
Solution
env.BRANCH_NAME
will work only on multiBranchPipeline so switch your project and you will get the branch name :)
Answered By - rohit thomas
Answer Checked By - Katrina (JavaFixing Volunteer)