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

Allow Script Path/Content to add/override environment variables for the build job

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Major Major
    • envinject-plugin
    • None
    • Environment Injector Plugin v1.55

      Currently the help indication for Script Path/Content states:

      ... adding or overriding environment variables in the script doesn't have any impacts in the build job.

      It would be extremely useful to be able to source a script (i.e., bash) which exports environment variables such that they persist for use in the build job. Currently to work around this, I need to source the script at the top of every Execute Shell build step.

          [JENKINS-14097] Allow Script Path/Content to add/override environment variables for the build job

          David Ehrenberger added a comment - - edited

          I use the bash built-in 'source' command which executes the script in the context of the current shell. For the purposes of this plugin, doing so will load any exported environment variables into the shell environment. Once the shell is closed, the variables exported into it are gone as well. This is why, if I execute 5 different shell build steps, I must source the environment file in each build step.

          As an example, if my script called environment.anything contains:

          #!/bin/bash
          
          export MY_VARIABLE="Test Variable"
          

          This would be possible in a shell:

          <user@machine> source environment.anything
          <user@machine> echo $MY_VARIABLE
          Test Variable
          

          You can read about shells Here but the 'source' command should be supported by most major shell types.

          Ultimately, I'd like to be able to source a script which sets up my job environment such that all my build steps also share that environment.

          David Ehrenberger added a comment - - edited I use the bash built-in 'source' command which executes the script in the context of the current shell. For the purposes of this plugin, doing so will load any exported environment variables into the shell environment. Once the shell is closed, the variables exported into it are gone as well. This is why, if I execute 5 different shell build steps, I must source the environment file in each build step. As an example, if my script called environment.anything contains: #!/bin/bash export MY_VARIABLE= "Test Variable" This would be possible in a shell: <user@machine> source environment.anything <user@machine> echo $MY_VARIABLE Test Variable You can read about shells Here but the 'source' command should be supported by most major shell types. Ultimately, I'd like to be able to source a script which sets up my job environment such that all my build steps also share that environment.

          At the moment, EnvInject plugin can't help you for this issue.
          There is a jenkins issue. Each shell is independent (executed with /bin/sh -xe, maybe a way).
          Because I can't retrieve env vars from Java with the shell env after source command, a track is to fix the shell for the whole job.
          Do you know with '-xe' argument interactions?

          Gregory Boissinot added a comment - At the moment, EnvInject plugin can't help you for this issue. There is a jenkins issue. Each shell is independent (executed with /bin/sh -xe, maybe a way). Because I can't retrieve env vars from Java with the shell env after source command, a track is to fix the shell for the whole job. Do you know with '-xe' argument interactions?

          • -e means that if any command fails (which it indicates by returning a nonzero status), the script will terminate immediately.
          • -x causes the shell to print an execution trace.

          So you are saying there is no "one" job environment currently available in Jenkins? Do you know the existing Jenkins JIRA issue?

          If there were "one" environment, then your plugin change would work since a sub-shell environment can add to/modify existing environment variables from the main shell via the export (in bash) or setenv (in tcsh).

          David Ehrenberger added a comment - -e means that if any command fails (which it indicates by returning a nonzero status), the script will terminate immediately. -x causes the shell to print an execution trace. So you are saying there is no "one" job environment currently available in Jenkins? Do you know the existing Jenkins JIRA issue? If there were "one" environment, then your plugin change would work since a sub-shell environment can add to/modify existing environment variables from the main shell via the export (in bash) or setenv (in tcsh).

          I think we must export environment variables for each sub-shells.
          Is there a simple way to populate environment variables or just a set of export (or setenv) command?
          Could we check dynamically to call export or setenv command?

          Gregory Boissinot added a comment - I think we must export environment variables for each sub-shells. Is there a simple way to populate environment variables or just a set of export (or setenv) command? Could we check dynamically to call export or setenv command?

          I'm not sure I understand your first question, but you can check which shell you're in by querying the SHELL environment variable. For instance, my jenkins log indicates SHELL as: /bin/bash, but on a separate machine my SHELL environment variable indicates: /bin/tcsh. I think shells of type csh have setenv, but ksh uses export. For what its worth, I believe Jenkins run in a BASH environment.

          David Ehrenberger added a comment - I'm not sure I understand your first question, but you can check which shell you're in by querying the SHELL environment variable. For instance, my jenkins log indicates SHELL as: /bin/bash, but on a separate machine my SHELL environment variable indicates: /bin/tcsh. I think shells of type csh have setenv, but ksh uses export. For what its worth, I believe Jenkins run in a BASH environment.

          I've got further interrogations.
          Do you want to source the file before each build step execution or can I source the file at first job build step and inject new environment variables for the rest of the job?
          The second solution is more simpler at the moment due to the current Jenkins design.
          Thanks in advance.

          Gregory Boissinot added a comment - I've got further interrogations. Do you want to source the file before each build step execution or can I source the file at first job build step and inject new environment variables for the rest of the job? The second solution is more simpler at the moment due to the current Jenkins design. Thanks in advance.

          Behind the scenes it doesn't really matter to me how the environment variables are injected.

          David Ehrenberger added a comment - Behind the scenes it doesn't really matter to me how the environment variables are injected.

          I have difficulty implementing this feature in this current EnvInject plugin version.
          EnvInject plugin has to be refactored before I am able to provide this feature.
          Please could you wait for it again.

          Gregory Boissinot added a comment - I have difficulty implementing this feature in this current EnvInject plugin version. EnvInject plugin has to be refactored before I am able to provide this feature. Please could you wait for it again.

          Sure, no problem.

          David Ehrenberger added a comment - Sure, no problem.

          Maciej Matys added a comment -

          adding section 'Evaluated Groovy script' to 'Inject environment variables to the build process' will solved this issue for me it is possible?

          Maciej Matys added a comment - adding section 'Evaluated Groovy script' to 'Inject environment variables to the build process' will solved this issue for me it is possible?

            gbois Gregory Boissinot
            daehren David Ehrenberger
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: