Anand Deshmukh
Anand Deshmukh

Reputation: 1126

How to catch branch name in Jenkinsfile

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'

            }

        }

    }
}

Upvotes: 2

Views: 8607

Answers (2)

John Jones
John Jones

Reputation: 2034

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.

Upvotes: 2

Sam
Sam

Reputation: 2882

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

if (env.BRANCH_NAME== 'develop') {\\code here}

Upvotes: 2

Related Questions