Pipeline Step vs Wrap Environment Variable Writing

69 views
Skip to first unread message

Jimmy Ray

unread,
Nov 2, 2016, 4:48:01 PM11/2/16
to Jenkins Developers
In a wrapper plugin that I wrote, I set and environment string like so:

context.env(key, value);

Where "context" is that which is passed in like so:

@Override
    public void setUp(Context context, Run<?, ?> run, FilePath filePath, Launcher launcher, TaskListener
            listener, EnvVars envVars) throws IOException, InterruptedException {...


I am trying to do the same with a pipeline step plugin:

final EnvVars environment = build.getEnvironment(listener); ...


environment.addLine(String.format("%s=%s", key, value));

The wrapper works fine, and env.<key> works fine.  However, the step is not working.  In the step I log:  

logger.println(String.format("Stored ENV variable (k,v):  %s=%s", key, environment.get(key)));

So, in the logs I see:  

Stored ENV variable (k,v):  test_key=42

However, when I use the echo call I see null for the ENV var at that key:

echo env.test_key

So, I assume I have to do something else to get the ENV variable into the right environment to be used after the step call?  Am I missing some kind of scoping issue with steps?

-Jimmy

Jimmy Ray

unread,
Nov 2, 2016, 8:52:16 PM11/2/16
to Jenkins Developers
In a Freestyle job where I use the Build Step portion of this plugin, I can set the ENV variable and get it via a shell call of:

echo ${key}

So, it just does not work in the Pipeline Build Step.

-Jimmy

Jimmy Ray

unread,
Nov 3, 2016, 1:24:30 AM11/3/16
to Jenkins Developers
So, after seeing this thread (https://groups.google.com/forum/#!topic/jenkinsci-dev/OG03cDc7Gew), I guess this may not work at all in a SimpleBuildStep.  That is really too bad, as I was hoping to keep the plugin working the same between Freestyle and Pipeline.  I feel that being able to set run-level environment variables, outside of a blocked-scope  step.  I have this working in the wrapper, but it now I have to abandon my step approach for Pipeline builds.

-Jimmy

Jesse Glick

unread,
Nov 7, 2016, 3:41:27 PM11/7/16
to Jenkins Dev
On Wed, Nov 2, 2016 at 4:48 PM, Jimmy Ray <jimmy...@gmail.com> wrote:
> final EnvVars environment = build.getEnvironment(listener); ...
> environment.addLine(String.format("%s=%s", key, value));

Makes no sense. The result of `getEnvironment` is immutable—a new map
each time you call it.

If you have a block-scoped Pipeline `Step`, the equivalent of
`SimpleBuildWrapper.Context.env` would be to use
`EnvironmentExpander`. See `EnvStep` for a simple example.

There is intentionally no way to set build-wide variables, only within
a block scope.
Reply all
Reply to author
Forward
0 new messages