Search code examples
jenkinsjenkins-workflow

Can a workflow step access environment variables provided by an EnvironmentContributingAction?


A custom plugin we wrote for an older version of Jenkins uses an EnvironmentContributingAction to provide environment variables to the execution so they could be used in future build steps and passed as parameters to downstream jobs.

While attempting to convert our build to workflow, I'm having trouble accessing these variables:

node {
    // this step queries an API and puts the results in
    // environment variables called FE1|BE1_INTERNAL_ADDRESS
    step([$class: 'SomeClass', parameter: foo])

    // this ends up echoing 'null and null'
    echo "${env.FE1_INTERNAL_ADDRESS} and ${env.BE1_INTERNAL_ADDRESS}"
}

Is there a way to access the environment variable that was injected? Do I have to convert this functionality to a build wrapper instead?


Solution

  • EnvironmentContributingAction is currently limited to AbstractBuilds, which WorkflowRuns are not, so pending JENKINS-29537 which I just filed, your plugin would need to be modified somehow. Options include:

    • Have the builder add a plain Action instead, then register an EnvironmentContributor whose buildEnvironmentFor(Run, …) checks for its presence using Run.getAction(Class).
    • Switch to a SimpleBuildWrapper which defines the environment variables within a scope, then invoke it from Workflow using the wrap step.
    • Depend on workflow-step-api and define a custom Workflow Step with comparable functionality but directly returning a List<String> or whatever makes sense in your context. (code sample)