Setting an env var from a build step

275 views
Skip to first unread message

PhilipLB

unread,
Feb 27, 2013, 9:00:47 AM2/27/13
to jenkin...@googlegroups.com
Hi,

I'd like to set an environment variable from within the execution of my Builder (in my plugin). How do I do this? SCM plugins have their buildEnvVars-method (or similar), but the Builder doesn't...
Any help would be greatly appreciated!

Best regards,
Philip

PhilipLB

unread,
Feb 27, 2013, 9:30:00 AM2/27/13
to jenkin...@googlegroups.com
This was too optimistic:
build.getEnvironment(listener).put("FOO", "BAR");

In a later buildstep, the "FOO" env var is still null.

Slide

unread,
Feb 27, 2013, 9:34:24 AM2/27/13
to Jenkins Dev


--
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.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Website: http://earl-of-code.com

cjo

unread,
Feb 27, 2013, 9:45:25 AM2/27/13
to jenkin...@googlegroups.com
Or have your builder add an action that implements EnviromentContributingAction [1] to the build, normally extending from InvisibleAction [2]
via build.getActions().add(<yourAction>);

PhilipLB

unread,
Feb 27, 2013, 10:25:01 AM2/27/13
to jenkin...@googlegroups.com
This worked like a charm! The environment variable is now available in other build-steps, even in a shell one. :)
Class:
/**
 * An action to publish a single environment variable.
 */
public class PublishEnvVarAction extends InvisibleAction implements EnvironmentContributingAction {

    /** The environment variable key. */
    private String key;

    /** The environment variable value. */
    private String value;
   
    /**
     * Constructor.
     * @param key the environment variable key
     * @param value the environment variable value
     */
    public PublishEnvVarAction(String key, String value) {
        this.key = key;
        this.value = value;
    }
   
    /* (non-Javadoc)
     * @see hudson.model.EnvironmentContributingAction#buildEnvVars(hudson.model.AbstractBuild, hudson.EnvVars)
     */
    public void buildEnvVars(AbstractBuild<?, ?> build, EnvVars env) {
        env.put(key, value);
    }

}

In my Builder:
build.addAction(new PublishEnvVarAction("FOO", "BAR"));
Reply all
Reply to author
Forward
0 new messages