2

I am using Jenkins for CI-CD on docker environment. I have 3 environments and branches according to the environment. Say, develop, qa, prod.

I am using Jenkins pipeline for building and pushing the image to DTR.

I just want to check how can I get the branch name in jenkinsfile so that I can have a condition on the basis of branch name to push the Docker image dependent upon branch name like:

if(env.BRANCH_NAME==develop){ 

Below is my Jenkinsfile:

pipeline { agent { label "master" } stages { stage('Build') { steps { echo '..........................Building Jar..........................' echo 'Pulling................................................' + env.BRANCH_NAME sh 'npm install' } } stage('Build-Image') { steps { echo '..........................Building Image..........................' sh 'sudo docker build -t some_org_dev/some_repo:v0.1 --build-arg PORT=9007 --build-arg ENVIRONMENT=develop .' } } stage('Tag-Image') { steps { if(env.BRANCH_NAME==develop){ echo '..........................Taging Image..........................' sh 'sudo docker login some_org_dev/some_repo -u username -p password' sh 'sudo docker tag some_org_dev/some_repo:v0.1 some_org/some_repo/service-portal:v0.1' }else if(env.BRANCH_NAME==qa){ echo '..........................Taging Image..........................' sh 'sudo docker login some_org_qa/some_repo -u username -p password' sh 'sudo docker tag some_org_qa/some_repo:v0.1 some_org/some_repo/service-portal:v0.1' } } stage('Push-Image') { steps { if(env.BRANCH_NAME==develop){ echo '..........................Pushing Image..........................' sh 'sudo docker push some_org_dev/some_repo:v0.1 some_org/some_repo/service-portal:v0.1' } }else if(env.BRANCH_NAME==qa){ echo '..........................Pushing Image..........................' sh 'sudo docker push some_org_qa/some_repo:v0.1 some_org/some_repo/service-portal:v0.1' } } } } 

2 Answers 2

2

I believe you are missing quotes, Is the error your getting an undefined var? Try:

if (env.BRANCH_NAME== 'develop') {\\code here} 
Sign up to request clarification or add additional context in comments.

8 Comments

I am getting null at echo 'Pulling................................................' + env.BRANCH_NAME in stage('Build') {
could you try echo('Pulling................................................' + env.BRANCH_NAME) for me
Same problem is here. . getting null
Have you done checkout scm? if not can you try above this command?
I have added checkout scm but still i am getting null. On the other hand if I do echo 'Pulling Job name......' + env.JOB_NAME I am getting the name of the job but branch is still null
|
2

On a multibranch-pipeline, I'm using something like the following:

pipeline { agent { label "jenkins" } stages { stage("A Stage") { when { expression { GIT_BRANCH ==~ /(develop)/ } } steps { sh 'deploy' } } } } 

You might need to poke around with sh 'env' if you aren't using multibranch.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.