0

Hey I'm trying to make changes to the environment variable GIT_BRANCH and parse the right side of the /, i know this can be achieved with cut like this: $(echo ${env.GIT_BRANCH} | cut -d \"/\" -f 2 )

Thing is, cannot make it work in Jenkins pipelines, error: bad substitution

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                sh "docker build -t jpq/jpq:test ."
            }
        }
        stage('Test') {
            steps {
                sh  "docker run jpq/jpq:test python3 tests.py"
            }
        }
         stage('Push') {
             steps {
                sh '''#!/bin/bash
                  BRANCH=\$(echo \${env.GIT_BRANCH} | cut -d \"/\" -f 2 )
                  echo ${BRANCH}
                  docker tag jpq/jpq:test jpq/jpq:${BRANCH}
                  docker push jpq/jpq:test
                    '''
             }
         }
        // stage('Deploy') {
        //     steps {
        //     }
        // }
    }

}

How can I correctly generate the BRANCH variable and pass it to the docker tag?

1 Answer 1

1

This should work:

stage('Push') {
     steps {
        sh '''#!/bin/bash
            #printenv
            BRANCH=$(echo ${GIT_BRANCH} | cut -d "/" -f2)
            echo "Branch: ${BRANCH}"
        '''
    }
}

Note: To see what all environment variables are available to the shell block, you may use printenv.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.