Reading Hudson and job environment variables from ParameterDefinition plugin

285 views
Skip to first unread message

Paulo Eduardo Neves

unread,
Sep 20, 2017, 5:06:35 AM9/20/17
to Jenkins Developers
Earlier today I post this message to Jenkin Users mailing list. I mistakenly though that it were the place for plugin questions. Sorry for the crosspost,

I have a prebuild plugin that extends ParameterDefinition class. It just give me some options to prepare the build.

It is an old installation and uses the deprecated SetEnv plugin to setup a bunch of environment variables.

Is it possible to access the environment variables defined in the SetEnv plugin from my plugin? It just creates the build object when the submit button is pressed, so I don't have access to it from my form.

If I can access the configuration file, I can also parse these variables. Maybe this is easier to do.

I also would like to access the Jenkins global variable values. If I do a System.getenv("JAVA_HOME") I get the value, but if I try with a variable defined in the SetEnv plugin or in the global configuration, it comes null.

If you can point me an example or reference, it would be great.

Thanks in advance for any help.

Robert Sandell

unread,
Sep 20, 2017, 5:51:26 AM9/20/17
to jenkin...@googlegroups.com
Something like this I'd guess

SetEnvBuildWrapper wrapper = job.getBuildWrappers().get(Jenkins.getInstance().getDescriptorByType(SetEnvBuildWrapper.DescriptorImpl.class))
Map<String,String> vars = wrapper.getLocalVarMap()
/B

--
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-dev+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/917dbdc6-d11d-4277-87d8-f85468213903%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Robert Sandell
Software Engineer
CloudBees Inc.

Paulo Eduardo Neves

unread,
Sep 20, 2017, 11:13:35 AM9/20/17
to Jenkins Developers
Thanks for your answer, Robert. Almost there!


How would you create the job instance variable with the current job? I see that there is a model.Job class, but I'm too newbie and can't find how to instantiate it.



Em quarta-feira, 20 de setembro de 2017 06:51:26 UTC-3, Robert Sandell escreveu:
Something like this I'd guess

SetEnvBuildWrapper wrapper = job.getBuildWrappers().get(Jenkins.getInstance().getDescriptorByType(SetEnvBuildWrapper.DescriptorImpl.class))
Map<String,String> vars = wrapper.getLocalVarMap()
/B
2017-09-19 20:53 GMT+02:00 Paulo Eduardo Neves <paulo...@gmail.com>:
Earlier today I post this message to Jenkin Users mailing list. I mistakenly though that it were the place for plugin questions. Sorry for the crosspost,

I have a prebuild plugin that extends ParameterDefinition class. It just give me some options to prepare the build.

It is an old installation and uses the deprecated SetEnv plugin to setup a bunch of environment variables.

Is it possible to access the environment variables defined in the SetEnv plugin from my plugin? It just creates the build object when the submit button is pressed, so I don't have access to it from my form.

If I can access the configuration file, I can also parse these variables. Maybe this is easier to do.

I also would like to access the Jenkins global variable values. If I do a System.getenv("JAVA_HOME") I get the value, but if I try with a variable defined in the SetEnv plugin or in the global configuration, it comes null.

If you can point me an example or reference, it would be great.

Thanks in advance for any help.

--
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.

Robert Sandell

unread,
Sep 20, 2017, 11:41:35 AM9/20/17
to jenkin...@googlegroups.com
At build time your ParameterValue gets the build as a parameter to buildEnvironment(Run build, EnvVars env), so you get it from build.getParent() but maybe the vars are already added to env so you might not need the code I showed above.

If you need the job at job schedule time, e.g. when clicking build now it get's a bit trickier. You can use the StaplerRequest to find the job from the ancestor path.
Job job = request.findAncestor(Job.class)
But I don't think there is any good way to get it when constructing the value from CliCommand, maybe the following could work
if (command instanceof BuildCommand) {
Job job = (BuildCommand)command.
job;
}


/B

To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-dev+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/53457ddc-e051-49a7-846f-7c26b85c8b3a%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Paulo Eduardo Neves

unread,
Sep 20, 2017, 12:02:20 PM9/20/17
to jenkin...@googlegroups.com
It is a parametrized job. I would like to get the Environment variables the job will use. I need it in the screen that appears after clicking the "build now" button, but before clicking the form submission button that will start the construction process.



--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-dev/2hLy525cxsg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-dev+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/CALzHZS2w6UZ0bBOCaJnLs3rNvSCLbst35KxUUuM62Hom3Az3ug%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.



--
Paulo Eduardo Neves

Paulo Eduardo Neves

unread,
Sep 20, 2017, 2:23:10 PM9/20/17
to jenkin...@googlegroups.com
Is it possible to access the job config.xml file from the ParameterDefinition object? It would also solve my problem.
--
Paulo Eduardo Neves

Reply all
Reply to author
Forward
0 new messages