Rens Groenveld
Rens Groenveld

Reputation: 982

Jenkins; Inject Environment Variable in Jelly field?

This has been bugging me for a little while, there is a list with environment variables and I want to use them in the configuration of one of my builds on my own custom plugin, as such:

enter image description here

So in this case I would like the ${WORKSPACE} to resolve to a path that has been configured by the environment.

Anyone know how to do this? I can't seem to find it as a Jelly tag.

Upvotes: 2

Views: 1871

Answers (2)

JZimmerman
JZimmerman

Reputation: 464

The type of variable expansion you are asking for can only be performed by the build step itself. If this is your own plug-in then you can apply the change I suggest here, otherwise you can always ask the plug-in author to do so. Short of either you'll have to rely on the work-around Slav provided.

If you do have access to the source for the plug-in here's how to expand variables during execution of the build step. I assume the build step class is SanityTestResultsToJUnitXMLBuilder. Inside this class's perform method you need to expand the source and destination directory fields. I added place holders for other pieces of code you'd normally find in a build step for brevity.

public class SanityTestResultsToJUnitXMLBuilder extends Builder {
    private final String sourceDirectory;
    private final String destinationDirectory;

    /* Constructor and getters typically appear here. */

    @Override
    public void perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
        EnvVars environment = build.getEnvironment(listener);
        String expandedSourceDirectory = environment.expand(sourceDirectory);
        String expandedDestinationDirectory = environment.expand(destinationDirectory);

        /* The rest of the perform() logic goes here */
    }

    /* Other methods typically appear here. */

    /* The Descriptor typically appears here. */
}

Upvotes: 4

Slav
Slav

Reputation: 27485

Not sure what exactly you are asking for, but you can get the $WORKSPACE environment variable by:

def workspace = manager.build.getEnvVars()["WORKSPACE"]

Upvotes: 0

Related Questions