2016-11-16 114 views
1

我有一个Jenkins管道作业,我将一些构建变量作为输入,并且如果变量未被用户传递,我执行一个脚本并获取这些变量的值。之后我必须使用这些变量的值来触发其他工作。在Jenkins管道中更改shell执行程序中的groovy变量

所以我的代码看起来是这样的:

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

...... 

的问题是,当我触发空T_RELEASE_VERSION的主要工作,孩子搭建工作T-INTEG流水线被触发用的空值RELEASE_VERSION参数。

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

回答

0

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

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

Eval.me例如:Serializing groovy map to string with quotes

写入/读取到文件例如: https://wilsonmar.github.io/jenkins2-pipeline/

编辑 的可读性和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() 
+0

我利用你的建议,并与类似想出了这个: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>状态' Q_RELEASE_VERSION_NEW = readFile('status')。trim() –

+1

好极了,为了提高可读性,添加了解决问题的方法 – MaTePe

相关问题