简体   繁体   English

将 jenkins 管道中设置的变量传递给 shell 脚本

[英]To pass variables set in jenkins pipeline to shell script

I want to send few parameters to one of my shell scripts written in linux server from a jenkins job.我想从 jenkins 作业向我在 linux 服务器中编写的 shell 脚本之一发送一些参数。 Below is my jenkins pipeline job:下面是我的 jenkins 管道作业:

def MY_VAR
def BUILD_NUMBER
pipeline {
    agent any
    stages {
        stage('Stage One') {
            steps {
                script {
                    BUILD_NUMBER={currentBuild.number}
                    MY_VAR ='abc'
                }                         
            }
        }
        stage('Stage Two') {
            steps {                    
                sh '''
                    cd /scripts/
                    ./my_scripts.sh $BUILD_NUMBER $MY_VAR'''
            }
        }
    }
}

Here I am able to send the value of BUILD_NUMBER but not of MY_VAR .在这里,我可以发送BUILD_NUMBER的值,但不能发送MY_VAR的值。 It seems to me that since MY_VAR is set into pipeline that is why it is happening.在我看来,由于MY_VAR被设置为管道,这就是它发生的原因。 Can anybody please help with the solution有人可以帮忙解决吗

If you want to interpolate $MY_VAR when executing sh step, you need to replace single quotes with the double-quotes.如果要在执行sh步骤时插入$MY_VAR ,则需要将单引号替换为双引号。

def MY_VAR
def BUILD_NUMBER
pipeline {
    agent any
    stages {
        stage('Stage One') {
            steps {
                script {
                    BUILD_NUMBER={currentBuild.number}
                    MY_VAR ='abc'
                }                         
            }
        }
        stage('Stage Two') {
            steps {                    
                sh """
                    cd /scripts/
                    ./my_scripts.sh $BUILD_NUMBER $MY_VAR"""
            }
        }
    }
}

The $BUILD_NUMBER worked, because pipeline exposes env.BUILD_NUMBER and this variable can be accessed inside the shell step as bash's $BUILD_NUMBER env variable. $BUILD_NUMBER起作用了,因为管道公开env.BUILD_NUMBER ,并且可以在 shell 步骤中作为 bash 的$BUILD_NUMBER env 变量访问此变量。 Alternatively, you could set MY_VAR as an environment variable and keep single quotes in the sh step.或者,您可以将MY_VAR设置为环境变量并在sh步骤中保留单引号。 Something like this should do the trick:这样的事情应该可以解决问题:

pipeline {
    agent any
    stages {
        stage('Stage One') {
            steps {
                script {
                    //you can remove BUILD_NUMBER assignment - env.BUILD_NUMBER is already created.
                    //BUILD_NUMBER={currentBuild.number}
                    env.MY_VAR ='abc'
                }                         
            }
        }
        stage('Stage Two') {
            steps {                    
                sh '''
                    cd /scripts/
                    ./my_scripts.sh $BUILD_NUMBER $MY_VAR'''
            }
        }
    }
}

You can learn more about Jenkins Pipeline environment variables from one of my blog posts .您可以从我的一篇博文中了解有关 Jenkins 管道环境变量的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM