webentwicklung-frage-antwort-db.com.de

Verwendung globaler Variablen in der Jenkins-Pipeline

Ich habe alle Arten von Möglichkeiten ausprobiert, aber nichts scheint zu funktionieren. Hier ist meine Jenkinsdatei.

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]]
            }
        }
    } 

}

Die Ausgabe von step script in stage ('Initialize the variables') gibt mir nichts, es setzt nicht den Wert der globalen Variablen Zip_NODE:

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

Und dann gehen wir zur Bühne (Code - Build), wir bekommen nicht den Wert von Zip_NODE. Siehe echo statement um 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

Dank @awefsome hatte ich einige Beobachtungen, die ich im Detail hinzufügen möchte: Wenn ich den folgenden Code verwende, erhalte ich die gewünschte Ausgabe, d. H. Den korrekten Wert von 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"
            }
        }

Wenn ich jedoch den folgenden Code verwende, erhalte ich nicht den Wert von 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 
                    '''
            }
        }
10
user271418

Versuchen Sie Folgendes und sehen Sie, wie es geht:

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

    agent {
        /*Agent section*/ 
        // where would you like to run the code 
        label 'master' 
        }
    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{
                println "checkout skipped"
                //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{
                println "build job skipped"
                //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]]
            }
        }
    } 
}

Ich habe folgende Ausgabe bekommen:

Started by user jenkins
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in /Users/Shared/Jenkins/Home/workspace/test
[Pipeline] {
[Pipeline] timestamps
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Initialize the variables)
[Pipeline] script
[Pipeline] {
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (code - Checkout)
[Pipeline] echo
21:19:06 checkout skipped
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (code - Build)
[Pipeline] sh
21:19:06 [test] Running Shell script
21:19:06 + echo test
21:19:06 test
21:19:06 + pwd
21:19:06 /Users/Shared/Jenkins/Home/workspace/test
21:19:06 + echo abcdefgh-0.0.17-dev.Zip
21:19:06 abcdefgh-0.0.17-dev.Zip
21:19:06 + echo 'remove alraedy existing Zip files'
21:19:06 remove alraedy existing Zip files
21:19:06 + rm -rf '*.Zip'
21:19:06 + Zip -r abcdefgh-0.0.17-dev.Zip .
21:19:06 
21:19:06 Zip error: Nothing to do! (try: Zip -r abcdefgh-0.0.17-dev.Zip . -i .)
[Pipeline] }
[Pipeline] // stage
6
awefsome
sh '''
'''

sollte sein

sh """
"""

bei einfachen Anführungszeichen werden die Variablen nicht verarbeitet.

12
Simon