简体   繁体   English

Jenkins API:EnvVars.overrideAll不会覆盖Pipeline作业中的现有环境变量。

[英]Jenkins API: EnvVars.overrideAll not overriding existing environment variables in Pipeline job.

I am trying to set Jenkins environment variables at runtime via the EnvironmentContributor.buildEnvironmentFor() method. 我试图通过EnvironmentContributor.buildEnvironmentFor()方法在运行时设置Jenkins环境变量。 Suppose I set an environment variable named "existingKey" with value "oldValue" in the Jenkins configure page. 假设我在Jenkins配置页面中将一个名为“ existingKey”的环境变量设置为值“ oldValue”。 If I set existingKey to any other value by using EnvVars.overrideAll it doesn't take. 如果我通过使用EnvVars.overrideAll将existingKey设置为任何其他值,则不需要。 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"));
    }
}

I have tested the above with both buildEnvironmentFor(Job,...) and buildEnvironmentFor(Run,...) with the same result. 我已经用buildEnvironmentFor(Job,...)和buildEnvironmentFor(Run,...)进行了测试,结果相同。 I have also tried EnvVar.override() and envVar.overrideExpandingAll(), none seem to work. 我也尝试了EnvVar.override()和envVar.overrideExpandingAll(),但似乎都没有。

I have also extended RunListener, which allows me to monitor if my env variables get set. 我还扩展了RunListener,它使我可以监视是否设置了我的env变量。

@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}
    }
}

The resulting logs look like this: 生成的日志如下所示:

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

I am also printing out the env in my build container and it shows the same pattern. 我还在我的构建容器中打印出env,它显示了相同的模式。 Any ideas how to actually override existing variables and not just set new ones? 任何想法如何实际覆盖现有变量而不仅仅是设置新变量?

Since, EnvVars extends TreeMap , before inserting existingKey you can remove it ie 由于EnvVars扩展了TreeMap ,因此在插入existingKey之前可以将其删除,即

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

envs.remove("existingKey");
newEnvVars.put("newKey", "newValue");
newEnvVars.put("existingKey","newValue");
envs.overrideAll(newEnvVars);    

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

It looks like this is not possible. 看来这是不可能的。

And yes env caches variables after first use, to avoid performance problems. 是的, env首次使用后会缓存变量,以避免性能问题。 There is not currently any provision to rerun EnvironmentContributor s, nor any API in Jenkins for such contributors to indicate that their result might have changed since the last call. 当前没有重新运行EnvironmentContributor的规定,也没有Jenkins中的任何API来指示此类贡献者自上次调用以来其结果可能已更改。 https://groups.google.com/forum/#!msg/jenkinsci-dev/FM_Nx_K_v9g/4BzWXd3cAgAJ https://groups.google.com/forum/#!msg/jenkinsci-dev/FM_Nx_K_v9g/4BzWXd3cAgAJ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM