Jenkins:在全局函数的主体中获取环境变量 [英] Jenkins: get environment variables in the body of a global function

查看:31
本文介绍了Jenkins:在全局函数的主体中获取环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 PublishGitHub.groovy 上有一个共享的全局函数,如下所示:

I have a shared global function on PublishGitHub.groovy looks like this:

#!/usr/bin/env groovy
def call(body)
{
    def config = [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = config

    echo "u001B[32mINFO: Publishing...u001B[m"
    body()
    echo "u001B[32mINFO: End Publish...u001B[m"      
}

以及我的 JenkinsFile 上的代码:

And a code on my JenkinsFile:

environment {
    VERSION = "v1.3.${env.BUILD_NUMBER}"
}
stages {
    stage ('Publish WebAPI'){
        steps{
            echo "u001B[32mINFO: Start Publish...u001B[m"

            PublishGitHub{
                echo "This is a body with version: ${env.VERSION}"
            }               
        }
    }
}

这是我的输出:

[Pipeline] echo
INFO: Start Publish...
[Pipeline] echo
INFO: Publishing...
[Pipeline] }

并遵循下一个错误:

java.lang.NullPointerException:无法在 null 上获取属性VERSION"对象

java.lang.NullPointerException: Cannot get property 'VERSION' on null object

因为在body里面我没有权限访问环境变量?

Because inside the body I do not have access to the environment variables?

推荐答案

您的共享库代码在工作流 CPS 上下文之外运行,这就是您传递给 vars 脚本的闭包无法识别 env 的原因财产.您可以通过传递对工作流脚本的引用来解决此问题.如果你这样调用你的函数

Your shared library code runs outside of the workflow CPS context, that is why closure you pass to the vars script does not recognize env property. You can fix this problem by passing a reference to the workflow script. If you call your function like this

PublishGitHub(this) {
    echo "This is a body with version: ${env.VERSION}"
}

并且您对 vars/PublishGitHub.groovy 脚本进行了一些小的修改,例如:

and you apply a small modification to vars/PublishGitHub.groovy script like:

#!/usr/bin/env groovy

def call(config, body) {
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = config

    echo "u001B[32mINFO: Publishing...u001B[m"
    body()
    echo "u001B[32mINFO: End Publish...u001B[m"
}

然后您将成功运行您的管道:

then you will run your pipeline successfully:

[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Publish WebAPI)
[Pipeline] echo
[32mINFO: Start Publish...[m
[Pipeline] echo
[32mINFO: Publishing...[m
[Pipeline] echo
This is a body with version: v1.3.537
[Pipeline] echo
[32mINFO: End Publish...[m
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

如果你想限制共享库的范围,你总是可以简单地传递 env 而不是 this 并更改 vars/PublishGitHub.groovy像这样:

If you want to limit the scope for the shared library, you can always simply pass env instead of this and change vars/PublishGitHub.groovy to something like this:

#!/usr/bin/env groovy

def call(env, body) {
    def config = [
            env: env
    ]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = config

    echo "u001B[32mINFO: Publishing...u001B[m"
    body()
    echo "u001B[32mINFO: End Publish...u001B[m"
}

在这种情况下,您只允许共享库访问环境变量.

In this scenario you give your shared library an access to environment variables only.

这篇关于Jenkins:在全局函数的主体中获取环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆