Plugin dev: how to replace environment variable's value before a build starts?

24 views
Skip to first unread message

Orekaria N

unread,
Mar 4, 2020, 11:08:01 AM3/4/20
to Jenkins Developers
Hello


The plugin allows to inject variables at the start of any build using a groovy script. It is working very fine.

What I want to improve:
- if the variable was already defined in "manage jenkins -> configure system -> environment variable", injecting a variable with the same name, does not update the value

e.g:
1) environment variable defined in configure system:
  Name: MY_GLOBAL_VAR
  Value: alfa
2) now, in a job:
- when the build starts, the global-pre-inject plugin injects value 'beta' in MY_GLOBAL_VAR
  Value: beta
- when a build step of type execute shell is added:
  echo "MY_GLOBAL_VAR: $MY_GLOBAL_VAR"
- the build console log result:
  MY_GLOBAL_VAR: alfa
issue: the value was overwritten again after the plugin did it

What I am looking for:
Could you tell me where I should inject the value of a variable so it can replace a variable with the same name set in "manage jenkins -> configure system -> environment variables", when a build starts?
Please be specific, I am new to jenkins internals

More info:
- I am currently injecting properties overriding setUpEnvironment of RunListener and returning
return new Environment() {
   
@Override
   
public void buildEnvVars(Map<String, String> env) {
        env
.putAll(groovyMapEnvVars);
   
}
};
- I can also inject new values with, e.g. :
EnvironmentContributor.all().add(1, new EnvironmentContributor() {
   
@Override
   
public void buildEnvironmentFor(@Nonnull Run r, @Nonnull EnvVars envs, @Nonnull TaskListener listener) throws IOException, InterruptedException {
        envs
.put("NEW_VAR", "somevalue");
   
}
});


Thanks in advance

Markus Winter

unread,
Mar 4, 2020, 4:08:22 PM3/4/20
to jenkin...@googlegroups.com
The envinject plugin allows to configure groovy scripts that set environment variables.
So why write another plugin?

--
Diese Nachricht wurde von meinem Android Mobiltelefon mit GMX Mail gesendet.
Am 04.03.20, 17:08 schrieb Orekaria N <orek...@gmail.com>:

        envs
. put ( "NEW_VAR" , "somevalue" );
   
}
});


Thanks in advance

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/9487c061-7c17-4f83-b804-6e6ce9d502e2%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages