Pipeline calling Build Step Plugin that Writes ENV Vars - Scoping Issue?

297 views
Skip to first unread message

Jimmy Ray

unread,
May 31, 2016, 4:03:54 PM5/31/16
to Jenkins Developers
So, I am re-writing the Consul-KV-Builder Plugin (https://wiki.jenkins-ci.org/display/JENKINS/Consul-KV-Builder+Plugin) to work with Jenkins pipeline.  I have it working fine, except for writing ENV variables that can written and read when pipeline calls the build step.

Here is my step call:
... step([$class: 'ConsulKVBuilder', debugMode: 'ENABLED', envVarKey: 'test_key', hostUrl: '<HOST_URL_GOES_HERE>', key: 'test/test-key', keyValue: '', requestMode: 'READ', token: '', urlOverride: '']) ...

Here are the ways I am trying to set/get the ENV variable:
... String storageKey = this.envVarKey.replace('.', '_').replace('/', '_'); ...



... VariableInjectionAction action = new VariableInjectionAction(storageKey, value);
                build
.addAction(action);...



... static final class VariableInjectionAction implements Action, EnvironmentContributingAction {


       
private String key;
       
private String value;


       
public VariableInjectionAction(String key, String value) {
           
this.key = key;
           
this.value = value;
       
}


       
@Override
       
public void buildEnvVars(AbstractBuild build, EnvVars envVars) {


           
if (envVars != null && key != null && value != null) {
                envVars
.put(key, value);
           
}
       
}


       
@Override
       
public String getDisplayName() {
           
return "VariableInjectionAction";
       
}


       
@Override
       
public String getIconFileName() {
           
return null;
       
}


       
@Override
       
public String getUrlName() {
           
return null;
       
}
   
} ...

I have also tried:

... build.getEnvironment(listener).put(storageKey, value); ...


These work when I use the Consul KV Builder as a freestyle job step.  When I call it from a pipeline script, the step executes with no errors, but the ENV variable value is null, both in the pipeline script and in the plugin log itself.

... logger.println(String.format("Stored ENV variable (k,v):  %s=%s", storageKey, build.getEnvironment
                       
(listener).get(storageKey, null))); ...


Am I running in a Jenkins Pipeline Plugin environment scoping issue?

-Jimmy

Martin Weber

unread,
May 31, 2016, 4:12:20 PM5/31/16
to jenkin...@googlegroups.com
Am Dienstag, 31. Mai 2016, 13:03:54 schrieb Jimmy Ray:
> So, I am re-writing the Consul-KV-Builder Plugin
> (https://wiki.jenkins-ci.org/display/JENKINS/Consul-KV-Builder+Plugin) to
> work with Jenkins pipeline. I have it working fine, except for writing ENV
> variables that can written and read when pipeline calls the build step.

I had a similar issue. See
<https://groups.google.com/d/msgid/jenkinsci-dev/CANfRfr0V_UvG06E82bj7Zc2PkUEpdwJz_eRX5BAnsG2NyVhMXg%40mail.gmail.com>

Martin

--
Cd wrttn wtht vwls s mch trsr.


Jimmy Ray

unread,
May 31, 2016, 4:19:33 PM5/31/16
to Jenkins Developers
So, it looks like it cannot be done.  That's really disappointing as the main purpose of the Consul KV Builder plugin was to read data from Hashicorp Consul servers and add it to Jenkins jobs to be used as ENV vars.  Wow.

-Jimmy

Jimmy Ray

unread,
May 31, 2016, 4:49:47 PM5/31/16
to Jenkins Developers
I am not sure the SimpleBuildWrapper will work for me.  The plugin is written as a build step, to allow for multiple additions to the same project, such as that multiple Consul Key/Value data could be Read/Written/Deleted as part of a single project.  Would the SimpleBuildWrapper allow me to add multiple wrapper definitions (calls to Consul) per project?  If so, is there an example out there that does this?  It would need to function like the build parms, but allow me to pull the data from Consul via REST.

-Jimmy

Jesse Glick

unread,
May 31, 2016, 8:39:51 PM5/31/16
to Jenkins Dev
On Tue, May 31, 2016 at 4:49 PM, Jimmy Ray <jimmy...@gmail.com> wrote:
> Would the
> SimpleBuildWrapper allow me to add multiple wrapper definitions (calls to
> Consul) per project?

Well, you could either have the `SimpleBuildWrapper` take a `List`- or
`Object[]`-valued parameter of some nested `Describable` type; or
simply have the script nest `wrap` steps.

Jimmy Ray

unread,
Jun 2, 2016, 12:36:29 PM6/2/16
to Jenkins Developers
Is setUp called before makeBuildVariables?

Jesse Glick

unread,
Jun 2, 2016, 12:58:08 PM6/2/16
to Jenkins Dev
On Thu, Jun 2, 2016 at 12:36 PM, Jimmy Ray <jimmy...@gmail.com> wrote:
> Is setUp called before makeBuildVariables?

Cannot recall offhand, but anyway as the Javadoc notes,
`makeBuildVariables` is not used for Pipeline builds.

Jimmy Ray

unread,
Jun 2, 2016, 4:14:13 PM6/2/16
to Jenkins Developers
OK, so I have the SimpleBuildWrapper working in regular builds and the Pipeline Snippet Generator  I was using this code to test:

      node {
      wrap([$class: 'ConsulKVReadWrapper', reads: [[debugMode: 'ENABLED', envKey: 'test_key', key: 'test/test-key', token: '', url: '<URL_HERE>', urlOverride: ''], [debugMode: 'ENABLED', envKey: 'test_key_new', key: 'test/test-key', token: '', url: '<URL_HERE>', urlOverride: '']]]) {
    // some block

    print env.test_key
    print env.test_key_new
    }
}



However, I get nulls in the ENV variables still.  The logs are here:

Started by user Jimmy Ray
[Pipeline] withEnv
[Pipeline] {
[Pipeline] node
Running on master in /Users/Shared/Jenkins/Home/jobs/Pipelines/jobs/PipelinePlugin/jobs/Manager/workspace
[Pipeline] {
[Pipeline] wrap

test/test-key
[{"CreateIndex":42815,"ModifyIndex":100829,"LockIndex":0,"Key":"test/test-key","Flags":0,"Value":"dGVzdC12YWx1ZQ=="}]
test-value

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

test/test-key
[{"CreateIndex":42815,"ModifyIndex":100829,"LockIndex":0,"Key":"test/test-key","Flags":0,"Value":"dGVzdC12YWx1ZQ=="}]
test-value

Stored ENV variable (k,v):  test_key_new=null
[Pipeline] {
[Pipeline] echo
null
[Pipeline] echo
null
[Pipeline] }
[Pipeline] // wrap
[Pipeline] }
[Pipeline] // node
[Pipeline] echo
value
[Pipeline] echo
CI
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] End of Pipeline
Finished: SUCCESS



The code to set ENV variables values in the setUp method is here:

VariableInjectionAction action = new VariableInjectionAction(read.getEnvKey(), value);
                run.addAction(action);
                run.getEnvironment(listener);

                if (read.getDebugMode().equals(DebugMode.ENABLED)) {
                    logger.println(String.format("Stored ENV variable (k,v):  %s=%s", read.getEnvKey(), run
                            .getEnvironment
                                    (listener).get(read.getEnvKey(), null)));
                }



I am getting the string, "test-value" back and logging it, but it is not making it into the ENV variables.  Should I be using a different way to write the ENV variables?

-Jimmy

Jesse Glick

unread,
Jun 2, 2016, 4:41:17 PM6/2/16
to Jenkins Dev
On Thu, Jun 2, 2016 at 4:14 PM, Jimmy Ray <jimmy...@gmail.com> wrote:
> VariableInjectionAction action = new
> VariableInjectionAction(read.getEnvKey(), value);
> run.addAction(action);
> run.getEnvironment(listener);

Not quite sure what you are trying to do here, but it will not work.
Just use `SimpleBuildWrapper.Context.env(String, String)` to set
variables within the block.

Jimmy Ray

unread,
Jun 2, 2016, 5:21:56 PM6/2/16
to Jenkins Developers
Thanks
Reply all
Reply to author
Forward
0 new messages