2

我有一个 Jenkins 管道作业,我将一些构建变量作为输入,如果用户没有传递这些变量,我会执行一个脚本并获取这些变量的值。稍后我必须使用这些变量的值来触发其他作业。

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

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-pipeline 会使用 RELEASE_VERSION 参数的空值触发。

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

4

2 回答 2

1

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

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

Eval.me 示例:将 groovy 映射序列化为带引号的字符串

写入/读取文件示例: 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()
于 2016-11-16T16:42:39.320 回答
0

我找到了一种改变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()
}
于 2020-08-27T09:49:46.620 回答