简体   繁体   中英

Setting envvars in Jenkins plugin

I'm trying to develop new Jenkins plugin. I've started from hello-world archetype provided by Jenkins. My plugin works fine!

Bun now i want to put some environment variables from my plugin. I've used whis code to do it

public void perform(Run<?, ?> run, FilePath workspace, Launcher launcher, TaskListener listener) {

    ...
    EnvVars envVars = run.getEnvironment(listener);
    envVars.put("SOME_VARIABLE", "SOME_VALUE");
    ...

}

But it don't work. I'm trying to use this variable on next build step and got nothing. I've googled it and there isn't quite clear discriptions. Source codes of existing plugins (EnvInject, etc) also doesn't help.

What am i doing wrong? Can anybody provide me some samples?

From my plugin...

 private void putEnvVar(String key, String value) throws IOException {
     Jenkins jenkins = Jenkins.getInstance();
     DescribableList<NodeProperty<?>, NodePropertyDescriptor> globalNodeProperties = jenkins.getGlobalNodeProperties();
     List<EnvironmentVariablesNodeProperty> envVarsNodePropertyList = globalNodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class);

     EnvironmentVariablesNodeProperty newEnvVarsNodeProperty = null;
     EnvVars envVars = null;

     if (envVarsNodePropertyList == null || envVarsNodePropertyList.isEmpty()) {
        newEnvVarsNodeProperty = new hudson.slaves.EnvironmentVariablesNodeProperty();
        globalNodeProperties.add(newEnvVarsNodeProperty);
        envVars = newEnvVarsNodeProperty.getEnvVars();
     } else {
        envVars = envVarsNodePropertyList.get(0).getEnvVars();
     }
     envVars.put(key, value);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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