Search code examples
jenkinsjenkins-pipelinejenkins-groovyxl-deploy

How to use environment variable inside Jenkinsfile


I am having similar issue as mentioned here

I am trying to deploy an application via Jenkinsfile. For which I have to run this command on the deploy stage in Jenkins (if I hardcode the value then it works fine):

xldDeploy serverCredentials: 'usernam', environmentId: 'Environments/SysTest1/SysTest1_1', packageId: 'Applications/Testapp/testapp_1.0.4.5.Build39_TAG-test'

"testapp_1.0.4.5.Build39_TAG-test" is getting generated at running time. Which can be created by concating "${TagVersion}.Build${env.BUILD_NUMBER}_${ComponentTagName}"

I tried below code in my Jenkins pipeline:

stage('Deploy') {  
   node('noibuild01') {
    if ("${env.Build_WildflyCPECommon}" == 'true') {
    echo "${TagVersion}"
    echo "${ComponentTagName}"
    echo "${env.BUILD_NUMBER}"
    script {
                    env.buildNumber = "${TagVersion}.Build${env.BUILD_NUMBER}_${ComponentTagName}"
                    env.packageid = "'Applications/Testapp/${env.buildNumber}'"
                }
    echo "${env.buildNumber}"
    echo "${env.packageid}"
    
    xldDeploy serverCredentials: 'nex8voo', environmentId: 'Environments/SysTest1/SysTest1_1', packageId: "${env.packageid}"
    }
    }
  }

I checked the output, it is showing correctly:

echo "${env.buildNumber}" giving
testapp_1.0.4.5.Build39_TAG-test
echo "${env.packageid}" giving 
'Applications/Testapp/testapp_1.0.4.5.Build39_TAG-test'

But xldDeploy serverCredentials: 'username', environmentId: 'Environments/SysTest1/SysTest1_1', packageId: "${env.packageid}" is taking as:

[/repository/ci/'Applications/Testapp/testapp_1.0.4.5.Build39_TAG-test']

Repository entity: ['Applications/Testapp/testapp_1.0.4.5.Build39_TAG-test'] not found

I think I can't use packageId: "${env.packageid}".

Is there anything I could try? Maybe Groovy or Python code?


Solution

  • Your packageid environment variable is not being assigned a concatenated string correctly. You have literal quotes inside the string interpolation quotes. You should change it to:

    env.packageid = "Applications/Testapp/${env.buildNumber}"
    

    to only interpolate the string, which is the functionality you want here.

    Additionally, you do not need to interpolate the environment variable inside an empty string for your method parameter, so your method invocation can be cleaned up as:

    xldDeploy serverCredentials: 'nex8voo', environmentId: 'Environments/SysTest1/SysTest1_1', packageId: env.packageid