简体   繁体   English

在 Jenkins 管道中更改 shell 执行程序内的常规变量

[英]Change groovy variables inside shell executor in Jenkins pipeline

I have a Jenkins pipeline job where I am taking some build variables as input, and if the variables are not passed by the user, I execute a script and get the value of those variables.我有一个 Jenkins 管道作业,我将一些构建变量作为输入,如果用户没有传递这些变量,我会执行一个脚本并获取这些变量的值。 Later I have to use the value of these variables to trigger other jobs.后来我不得不使用这些变量的值来触发其他作业。

So my code looks something like this:所以我的代码看起来像这样:

node {
withCredentials([[$class: 'StringBinding', credentialsId: 'DOCKER_HOST', variable: 'DOCKER_HOST']]) {

env.T_RELEASE_VERSION = T_RELEASE_VERSION
env.C_RELEASE_VERSION = C_RELEASE_VERSION
env.N_RELEASE_VERSION = N_RELEASE_VERSION
env.F_RELEASE_VERSION = F_RELEASE_VERSION

....

stage concurrency: 1, name: 'consul-get-version'
sh '''
        if [ -z ${T_RELEASE_VERSION} ]
        then
            export T_RELEASE_VERSION=$(ruby common/consul/services_prod_version.rb prod_t_release_version)
            aws ecr get-login --region us-east-1
            aws ecr list-images --repository-name t-server | grep ${T_RELEASE_VERSION}
        else
            aws ecr get-login --region us-east-1
            aws ecr list-images --repository-name t-server | grep ${T_RELEASE_VERSION}
        fi

.......


    't-integ-pipeline' : {
build job: 't-integ-pipeline', parameters: [[$class: 'StringParameterValue', name: 'RELEASE_VERSION', value: T_RELEASE_VERSION],
                                           [$class: 'BooleanParameterValue', name: 'FASTFORWARD_TO_DEPLOY', value: true]]
},

......

The issue is when I am triggering the main job with empty T_RELEASE_VERSION, the child build job t-integ-pipeline is triggered with an empty value of the RELEASE_VERSION parameter.问题是当我用空的 T_RELEASE_VERSION 触发主作业时,子构建作业 t-integ-pipeline 是用 RELEASE_VERSION 参数的空值触发的。

How can I change a groovy parameter inside a shell executor and then access it again in the groovy executor with the modified value?如何更改 shell 执行程序中的 groovy 参数,然后在 groovy 执行程序中使用修改后的值再次访问它?

When using env-inject it was possible to store the values in the properties files and the inject them as environment variables.使用 env-inject 时,可以将值存储在属性文件中并将它们作为环境变量注入。 Couldn't find any easy way to do it in pipeline.在管道中找不到任何简单的方法来做到这一点。

Here is a solution anyway, store the values to a file, and read the file from the pipeline.无论如何,这是一个解决方案,将值存储到文件中,然后从管道中读取文件。 Then use eval or similar to transform it to an parsable object (hash).然后使用 eval 或类似方法将其转换为可解析的对象(哈希)。

Eval.me example: Serializing groovy map to string with quotes Eval.me 示例:将groovy 映射序列化为带引号的字符串

Write/Read to file example: https://wilsonmar.github.io/jenkins2-pipeline/写入/读取文件示例: https : //wilsonmar.github.io/jenkins2-pipeline/

EDIT Manish solution for readability:编辑 Manish 解决方案以提高可读性:

sh 'ruby common/consul/services_prod_version.rb prod_n_release_version > status' 
N_RELEASE_VERSION_NEW = readFile('status').trim() 
sh 'ruby common/consul/services_prod_version.rb prod_q_release_version > status' 
Q_RELEASE_VERSION_NEW = readFile('status').trim()

I found a way change the groovy variable in the shell, No need to store it in the file, There is a example here git-tag-message-plugin , I use this method like below:我找到了一种在shell中更改groovy变量的方法,无需将其存储在文件中,这里有一个示例git-tag-message-plugin ,我使用这种方法如下:

script{
N_RELEASE_VERSION_NEW = getN_RELEASE_VERSION_NEW()
}


String getN_RELEASE_VERSION_NEW() {
    return sh(script: "ruby common/consul/services_prod_version.rb prod_n_release_version ", returnStdout: true)?.trim()
}

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

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