Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

groovy - Using global variables in Jenkins Pipeline

I have tried all sort of ways but nothing seems to be working. Here is my jenkinsfile.

def ZIP_NODE
def CODE_VERSION
pipeline{
    /*A declarative pipeline*/

    agent {
        /*Agent section*/ 
        // where would you like to run the code 
        label 'ubuntu' 
        }
    options{
        timestamps()
        }
    parameters {
        choice(choices: ['dev'], description: 'Name of the environment', name: 'ENV')
        choice(choices: ['us-east-1', 'us-west-1','us-west-2','us-east-2','ap-south-1'], description: 'What AWS region?', name: 'AWS_DEFAULT_REGION')
        string(defaultValue: "", description: '', name: 'APP_VERSION')

        }
    stages{
        /*stages section*/
        stage('Initialize the variables') {
            // Each stage is made up of steps
            steps{
                script{
                    CODE_VERSION='${BUILD_NUMBER}-${ENV}'
                    ZIP_NODE='abcdefgh-0.0.${CODE_VERSION}.zip'
                }
            }                
        }
        stage ('code - Checkout') {
            steps{
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'xxxxxxxxxxxxxxxxxxxxxxxxxx', url: 'http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.git']]]) 
            }  
        }

        stage ('code - Build'){
            steps{
                sh ''' 
                    echo ${JOB_NAME}
                    pwd
                    echo ${ZIP_NODE}
                    echo 'remove alraedy existing zip files'
                    rm -rf *.zip
                    zip -r ${ZIP_NODE} . 
                    chmod 777 $ZIP_NODE 
                ''' 
            }
        }
        stage('Deploy on Beanstalk'){
            steps{
                build job: 'abcdefgh-PHASER' , parameters: [[$class: 'StringParameterValue', name: 'vpc', value: ENV], [$class: 'StringParameterValue', name: 'ZIP_NODE', value: ZIP_NODE], [$class: 'StringParameterValue', name: 'CODE_VERSION', value: CODE_VERSION], [$class: 'StringParameterValue', name: 'APP_VERSION', value: BUILD_NUMBER], [$class: 'StringParameterValue', name: 'AWS_DEFAULT_REGION', value: AWS_DEFAULT_REGION], [$class: 'StringParameterValue', name: 'ParentJobName', value: JOB_NAME]]
            }
        }
    } 

}

The output of step script in stage ('Initialize the variables') gives me nothing, It is not setting the value of global variable ZIP_NODE:

[Pipeline] stage
[Pipeline] { (Initialize the variables)
[Pipeline] script
[Pipeline] {
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage

And then we go to stage (code - Build) we do not get the value of ZIP_NODE. See echo statement at 22:34:17

[Pipeline] stage
[Pipeline] { (code - Build)
[Pipeline] sh
22:34:16 [abcdefgh-ci-dev-pipeline] Running shell script
22:34:17 + echo abcdefgh-ci-dev-pipeline
22:34:17 abcdefgh-ci-dev-pipeline
22:34:17 + pwd
22:34:17 /home/advisor/Jenkins/workspace/abcdefgh-ci-dev-pipeline
22:34:17 + echo
22:34:17 
22:34:17 + echo remove alraedy existing zip files

Thanks to @awefsome, I had some observation which I would like add in details: When I use below code I get desired output i.e. correct value of ZIP_NODE:

 stage ('code - Build'){
            steps{
                sh "echo ${JOB_NAME} && pwd && echo ${ZIP_NODE} && echo 'remove alraedy existing zip files' && rm -rf *.zip && zip -r ${ZIP_NODE} . && chmod 777 $ZIP_NODE"
            }
        }

But when I use below code I do not get the value of ZIP_NODE:

stage ('code - Build'){
            steps{

                sh ''' 
                        echo ${ZIP_NODE}
                        echo ${JOB_NAME}
                        pwd
                        echo ${ZIP_NODE}
                        echo ${CODE_VERSION}
                        #rm -rf .ebextensions
                        echo 'remove alraedy existing zip files'
                        rm -rf *.zip
                        zip -r ${ZIP_NODE} . 
                        chmod 777 $ZIP_NODE 
                    '''
            }
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
sh '''
'''

should be

sh """
"""

with single quotes the variables don't get processed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...