Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-32692

Access to environment variables set in workflow script in other plugins

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Minor Minor
    • workflow-cps-plugin
    • None
    • Jenkins 1.625.3, workflow-plugin 1.13, gerrit-trigger-plugin 2.18.2, envinject-plugin 1.92.1

      There should be a way to access environment variables set within the workflow script from other plugins, from code that executes outside the Groovy script.

      My particular case is that I want to compute a value for an environment variable within the workflow Groovy script, and then use that value in Gerrit Trigger configuration to customize how the plugin reports the results back to Gerrit (in this case, the URL to post back). Gerrit Trigger uses Run.getEnvironment() to do replacements in the configuration values I want, and in a freestyle project, I can use EnvInject plugin to inject environment variables that then get expanded there. But in a workflow build, values assigned to env.FOO are not visible to Gerrit Trigger, and I could not find any other way to achieve this, either.

      I'm not an expert in Jenkins internals, but by looking at the code of the various plugins, it looks like it could be straightforward to provide an EnvironmentContributor from the workflow plugin that would expose the variables set through env also through Run.getEnvironment(). There are probably other ways of doing this as well, but this way should not require other plugins to depend on workflow-plugin constructs. And probably all other EnvInject plugin functionality is already covered by the workflow plugin.

          [JENKINS-32692] Access to environment variables set in workflow script in other plugins

          Jesse Glick added a comment -

          Your case would perhaps be better handled by JENKINS-26103, though I am not familiar with the details.

          An EnvironmentContributor might be a general solution though it would require some careful refactoring of the already brittle environment variable handling.

          Jesse Glick added a comment - Your case would perhaps be better handled by JENKINS-26103 , though I am not familiar with the details. An EnvironmentContributor might be a general solution though it would require some careful refactoring of the already brittle environment variable handling.

          Teemu Murtola added a comment -

          Yes, a way to directly interact with Gerrit Trigger from the workflow would provide another solution, but JENKINS-26103 by itself would not provide a perfect solution for us: we have multiple Jenkins jobs that trigger on the same Gerrit event, and Gerrit Trigger automatically combines the posts back to Gerrit such that there is only one post, once all the builds finish. For now, we would probably just want to move one of these jobs to a workflow, and still keep this reporting otherwise functional. So intermediate voting as mentioned in that issue would not be ideal.

          What could work is that if instead of JENKINS-26103, Gerrit Trigger provided a mechanism to influence the final post from within the workflow. That would probably be a good long-term solution for our case. EnvironmentContributor could be more general, but I'm not sure how many other plugins would actually benefit from it.

          Teemu Murtola added a comment - Yes, a way to directly interact with Gerrit Trigger from the workflow would provide another solution, but JENKINS-26103 by itself would not provide a perfect solution for us: we have multiple Jenkins jobs that trigger on the same Gerrit event, and Gerrit Trigger automatically combines the posts back to Gerrit such that there is only one post, once all the builds finish. For now, we would probably just want to move one of these jobs to a workflow, and still keep this reporting otherwise functional. So intermediate voting as mentioned in that issue would not be ideal. What could work is that if instead of JENKINS-26103 , Gerrit Trigger provided a mechanism to influence the final post from within the workflow. That would probably be a good long-term solution for our case. EnvironmentContributor could be more general, but I'm not sure how many other plugins would actually benefit from it.

          An Nguyen added a comment - - edited

          I'd love to see this implemented although my use case is different.

          An Nguyen added a comment - - edited I'd love to see this implemented although my use case is different.

            jglick Jesse Glick
            tmurtola Teemu Murtola
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: