0

I am trying to pull a specific tag from two different repos in my Jenkinsfile. The tags are determined by getting the latest successful builds from the relevant pipeline and using these values to decide which tag to checkout. However I am getting issues as SCM is not replacing the withEnv variables I have added. In the code below you can see I am echoing out what should be the tag (and this echo works fine) but when it comes to SCM checking out the tag LAST_SUCCESSFUL_EXTENSIONS_REPO_BUILD and LAST_SUCCESSFUL_SHARED_REPO_BUILD variables are no substituted with their values

node {

    stage('Checkout') {
        withEnv([
            "LAST_SUCCESSFUL_EXTENSIONS_REPO_BUILD=${ sh ( script: "curl <JENKINS_URL>/job/extensionsrepo/job/${BRANCH_NAME}/lastSuccessfulBuild/buildNumber", returnStdout: true ) }",
            "LAST_SUCCESSFUL_SHARED_REPO_BUILD=${ sh ( script: "curl <JENKINS_URL>/job/sharedrepo/job/${BRANCH_NAME}/lastSuccessfulBuild/buildNumber", returnStdout: true ) }"
        ]) {
            sh 'echo ${BRANCH_NAME}_${LAST_SUCCESSFUL_EXTENSIONS_REPO_BUILD}'
            sh 'echo ${BRANCH_NAME}_${LAST_SUCCESSFUL_SHARED_REPO_BUILD}'
            checkout scm
            checkout([
                $class: 'GitSCM',
                branches: [[name: 'refs/tags/${BRANCH_NAME}_${LAST_SUCCESSFUL_EXTENSIONS_REPO_BUILD}']],
                doGenerateSubmoduleConfigurations: false,
                extensions: [[
                    $class: 'RelativeTargetDirectory', 
                    relativeTargetDir: 'app/extensions'
                ]],
                submoduleCfg: [],
                userRemoteConfigs: [[
                    credentialsId: 'ssh-key',
                    url: '<GIT_URL>'
                ]]
            ])
            checkout([
                $class: 'GitSCM',
                branches: [[name: 'refs/tags/${BRANCH_NAME}_${LAST_SUCCESSFUL_SHARED_REPO_BUILD}']],
                doGenerateSubmoduleConfigurations: false,
                extensions: [[
                    $class: 'RelativeTargetDirectory', 
                    relativeTargetDir: 'app/shared'
                ]],
                submoduleCfg: [],
                userRemoteConfigs: [[
                    credentialsId: 'ssh-key',
                    url: '<GIT_URL>'
                ]]
            ])
        }
    }

    // test, stage, deploy
}

Any pointers on what I am doing wrong here would be much appreciated. No doubt it is something stupid that I am failing to do correctly.

Thanks

Joe Coulson
  • 71
  • 1
  • 6

1 Answers1

0

Try replacing the single quotes in your checkout call where you have the branch var with double quotes. In general, in Groovy you should use double quotes when including embedded variables (see What's the difference of strings within single or double quotes in groovy?).

Like:

checkout([
            $class: 'GitSCM',
            branches: [[name: "refs/tags/${BRANCH_NAME}_${LAST_SUCCESSFUL_EXTENSIONS_REPO_BUILD}"]],
            doGenerateSubmoduleConfigurations: false,
            extensions: [[
                $class: 'RelativeTargetDirectory', 
                relativeTargetDir: 'app/extensions'
            ]],
            submoduleCfg: [],
            userRemoteConfigs: [[
                credentialsId: 'ssh-key',
                url: '<GIT_URL>'
            ]]
        ])
Brad Albright
  • 686
  • 7
  • 12