Jenkins API:EnvVars.overrideAll不会覆盖Pipeline作业中的现有环境变量. [英] Jenkins API: EnvVars.overrideAll not overriding existing environment variables in Pipeline job.

查看:301
本文介绍了Jenkins API:EnvVars.overrideAll不会覆盖Pipeline作业中的现有环境变量.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在运行时通过EnvironmentContributor.buildEnvironmentFor()方法设置Jenkins环境变量.假设我在Jenkins配置页面中设置了一个名为"existingKey"的环境变量,其值为"oldValue".如果我通过使用EnvVars.overrideAll将existingKey设置为任何其他值,则不需要这样做.新的键/值对仍然存在.这是我的代码:

I am trying to set Jenkins environment variables at runtime via the EnvironmentContributor.buildEnvironmentFor() method. Suppose I set an environment variable named "existingKey" with value "oldValue" in the Jenkins configure page. If I set existingKey to any other value by using EnvVars.overrideAll it doesn't take. New key/value pairs persist. Here's my code:

@Extension
public class MyEnvironmentContributor extends EnvironmentContributor {
    Logger log...

    @Override
    public void buildEnvironmentFor(@Nonnull Job j, @Nonnull EnvVars envs, @Nonnull TaskListener listener) throws IOException, InterruptedException {
        log.fine("pre: existingKey="+envs.get("existingKey"));

        HashMap<String, String> newEnvVars = new HashMap<String, String>();
        newEnvVars.put("newKey", "newValue");
        newEnvVars.put("existingKey","newValue");
        envs.overrideAll(newEnvVars);

        log.fine("post: existingKey="+envs.get("existingKey"));
    }
}

我已经用buildEnvironmentFor(Job,...)和buildEnvironmentFor(Run,...)进行了测试,结果相同.我还尝试了EnvVar.override()和envVar.overrideExpandingAll(),但似乎都无法正常工作.

I have tested the above with both buildEnvironmentFor(Job,...) and buildEnvironmentFor(Run,...) with the same result. I have also tried EnvVar.override() and envVar.overrideExpandingAll(), none seem to work.

我还扩展了RunListener,它使我可以监视是否设置了我的env变量.

I have also extended RunListener, which allows me to monitor if my env variables get set.

@Extension
public class MyRunListener extends RunListener<Run> {
    Logger log.....

    public MyRunListener(){
        //lazy loaded class
    }

    @Override
    public void onStarted(final Run run, final TaskListener listener) {
        log.fine("Build Started: " + run.getUrl() + ", " + run.getDisplayName());
        try{
            EnvVars env = run.getEnvironment(listener);
            for (Entry<String, String> entry : env.entrySet()) {
                log.fine(entry.getKey() + "=" + entry.getValue());
            }
        }catch(){//trivial}
    }
}

生成的日志如下:

Build Started: job/Test/job/EnvTest/20/, #20
pre: existingKey=oldValue
post: existingKey=newValue
newKey=newValue
existingKey=oldValue

我也在我的构建容器中打印出env,它显示了相同的模式.有什么想法如何实际覆盖现有变量,而不仅仅是设置新变量?

I am also printing out the env in my build container and it shows the same pattern. Any ideas how to actually override existing variables and not just set new ones?

推荐答案

看来这是不可能的.

是的,env在第一次使用后会缓存变量,以避免性能下降 问题.目前没有任何要重新运行的规定 EnvironmentContributor,也没有Jenkins中的任何API 贡献者表示他们的结果可能自从 最后一个电话. https://groups.google.com/forum/#! msg/jenkinsci-dev/FM_Nx_K_v9g/4BzWXd3cAgAJ

And yes env caches variables after first use, to avoid performance problems. There is not currently any provision to rerun EnvironmentContributors, nor any API in Jenkins for such contributors to indicate that their result might have changed since the last call. https://groups.google.com/forum/#!msg/jenkinsci-dev/FM_Nx_K_v9g/4BzWXd3cAgAJ

这篇关于Jenkins API:EnvVars.overrideAll不会覆盖Pipeline作业中的现有环境变量.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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