在Jenkins管道中更改shell执行程序中的groovy变量

编程入门 行业动态 更新时间:2024-10-27 10:27:33
本文介绍了在Jenkins管道中更改shell执行程序中的groovy变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个Jenkins管道作业,我将一些构建变量作为输入,如果变量未被用户传递,我执行脚本并获取这些变量的值。后来我不得不用这些变量的值来触发其他工作。

节点{ withCredentials([[$ class:'StringBinding',credentialsId:'DOCKER_HOST',变量:'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 .... 阶段并发:1,名称:'consul-get-version' sh''' if [-z $ {T_RELEASE_VERSION}] 然后 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':{建立工作: 'T-INTEG流水线',参数:[[$类: 'StringParameterValue',名称: 'RELEASE_VERSION',值:T_RELEASE_VERSION], [$类: 'BooleanParameterValue',名称: 'FASTFORWARD_TO_DEPLOY' ,value:true]] }, ......

问题是当我用空T_RELEASE_VERSION触发主作业时,子构建作业t-integ-pipeline被RELEASE_VERSION参数的空值触发。

如何更改shell执行程序中的groovy参数,然后使用修改后的值在groovy执行程序中再次访问它?

使用env-inject时,可以将值存储在属性文件中,并将它们作为环境变量注入。找不到任何简单的方法来做到这一点。

无论如何,这是一个解决方案,将值存储到文件中,然后从管道中读取文件。然后使用eval或类似的方法将其转换为可解析的对象(哈希)。

Eval.me示例:将groovy映射到带引号的字符串

写/读文件示例: wilsonmar.github.io/jenkins2-pipeline/

编辑 Manish解决方案的可读性:

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

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. 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.

How can I change a groovy parameter inside a shell executor and then access it again in the groovy executor with the modified value?

解决方案

When using env-inject it was possible to store the values in the properties files and the inject them as environment variables. 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.me example: Serializing groovy map to string with quotes

Write/Read to file example: wilsonmar.github.io/jenkins2-pipeline/

EDIT Manish solution for readability:

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()

更多推荐

在Jenkins管道中更改shell执行程序中的groovy变量

本文发布于:2023-10-07 14:47:54,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   道中   程序   Jenkins   groovy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!