在 Jenkins Pipeline 中使用全局变量 [英] Using global variables in Jenkins Pipeline

查看:237
本文介绍了在 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]]
            }
        }
    } 

}

阶段脚本的输出('初始化变量')什么也没给我,它没有设置全局变量 ZIP_NODE 的值:

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

然后我们进入阶段(代码 - 构建)我们没有得到 ZIP_NODE 的值.见 22:34:17 的 echo 声明

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

感谢@awefsome,我有一些观察,我想补充一些细节:当我使用下面的代码时,我得到了所需的输出,即 ZIP_NODE 的正确值:

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

但是当我使用下面的代码时,我没有得到 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 
                    '''
            }
        }

推荐答案

尝试以下操作,看看效果如何:

Try following and see how it goes:

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

我得到以下输出:

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

这篇关于在 Jenkins Pipeline 中使用全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆