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

EnvInject doesn't see GIT_COMMIT even after SCM phase

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • envinject-plugin
    • None
    • Jenkins 1.596.2
      Environment Injector Plugin 1.92
      GIT plugin 2.4.0

      Variables provided by Git plugin (tested with GIT_COMMIT) are not accessible in EnvInject plugin even if applied after SCM phase (Build Environment -> Inject environment variables to the build process).

      Sample Groovy code used in "Evaluated Groovy script":

      [GIT_COMMIT_SHORT: GIT_COMMIT]
      

      ends with: "[EnvInject] - [ERROR] - Problems occurs on injecting env vars as a build wrap: null". It works wine with JOB_NAME or similar. GIT_COMMIT variable is later accessible in a build step.

      Sample Use case: Get a short version of Git commit SHA fetch by a job.

          [JENKINS-30042] EnvInject doesn't see GIT_COMMIT even after SCM phase

          May be related to JENKINS-29349.

          Marcin Zajączkowski added a comment - May be related to JENKINS-29349 .

          emszpak, Did you get this bug after to update EnvInject?

          Manuel Recena Soto added a comment - emszpak , Did you get this bug after to update EnvInject?

          Workaround cobbled together from various sources on the interwebs:

          def env = System.getenv()
          env.each{
              println it
          } 
          
          def thr = Thread.currentThread()
          def build = thr?.executable
          def envVarsMap = build.parent.builds[0].properties.get("envVars")
          envVarsMap.each{
              println it
          }
          
          def map = [HASH: envVarsMap['GIT_COMMIT']]
          return map
          

          Props to folks uncited...

          brendon wilder added a comment - Workaround cobbled together from various sources on the interwebs: def env = System .getenv() env.each{ println it } def thr = Thread .currentThread() def build = thr?.executable def envVarsMap = build.parent.builds[0].properties.get( "envVars" ) envVarsMap.each{ println it } def map = [HASH: envVarsMap[ 'GIT_COMMIT' ]] return map Props to folks uncited...

          Theodore Pak added a comment -

          Thanks to rantingpenguin my build's EnvInject Groovy script works now. The critical detail is that:

          System.getenv()
          != 
          Thread.currentThread()?.executable.parent.builds[0].properties.get('envVars')
          

          I'mont sure why. But you should use the latter to get all environment vars, as Brendon does in the script he provided.

          Theodore Pak added a comment - Thanks to rantingpenguin my build's EnvInject Groovy script works now. The critical detail is that: System .getenv() != Thread .currentThread()?.executable.parent.builds[0].properties.get( 'envVars' ) I'mont sure why. But you should use the latter to get all environment vars, as Brendon does in the script he provided.

          recena I don't know. It was the new feature and I haven't changed it with the previous version. If it is important to find a reason I could give it a try.

          Marcin Zajączkowski added a comment - recena I don't know. It was the new feature and I haven't changed it with the previous version. If it is important to find a reason I could give it a try.

          I ended up with dumping Git commit to file in a shell build step and inject it in the next step using EnvInject (but it could work out-of-box).

          Marcin Zajączkowski added a comment - I ended up with dumping Git commit to file in a shell build step and inject it in the next step using EnvInject (but it could work out-of-box).

          emszpak as mentioned before, you could use this Groovy script to get, for example, the first 10 chars of the GIT_COMMIT variable in another variable called GIT_COMMIT_SHORT:

          def thr = Thread.currentThread()
          def build = thr?.executable
          def envVarsMap = build.parent.builds[0].properties.get("envVars")
          
          def map = [
            GIT_COMMIT_SHORT: envVarsMap['GIT_COMMIT'][0..9],
          ]
          return map
          

          Thanks rantingpenguin for sharing this solution!

          Antoine Cotten added a comment - emszpak as mentioned before, you could use this Groovy script to get, for example, the first 10 chars of the GIT_COMMIT variable in another variable called GIT_COMMIT_SHORT : def thr = Thread.currentThread() def build = thr?.executable def envVarsMap = build.parent.builds[0].properties.get("envVars") def map = [ GIT_COMMIT_SHORT: envVarsMap['GIT_COMMIT'][0..9], ] return map Thanks rantingpenguin for sharing this solution!

          Christopher Zee added a comment - - edited

          In addition to the comment written by rantingpenguin, you will need some additional code to ensure this works safely across concurrent builds if you are trying to access build environment variables (not to be confused with environment variables). As seen in this presentation on slide 8:

          def config = new HashMap()
          def thr = Thread.currentThread()
          def build = thr?.executable
          def buildMap = build.getBuildVariables()
          config.putAll(buildMap)
          def envVarsMap = build.parent.builds[0].properties.get("envVars")
          config.putAll(envVarsMap)

          Now all build environment variables and environment variables are accessible in the config HashMap.

          Christopher Zee added a comment - - edited In addition to the comment written by rantingpenguin , you will need some additional code to ensure this works safely across concurrent builds if you are trying to access build environment variables (not to be confused with environment variables). As seen in this presentation on slide 8: def config = new HashMap() def thr = Thread.currentThread() def build = thr?.executable def buildMap = build.getBuildVariables() config.putAll(buildMap) def envVarsMap = build.parent.builds [0] .properties.get("envVars") config.putAll(envVarsMap) Now all build environment variables and environment variables are accessible in the config HashMap.

          Oleg Nenashev added a comment -

          Unfortunately I will not have time to work on EnvInject issues for a while. I decided to unassign all issues so that somebody can take them and finalize.

          Context: The plugin has been waiting for adoption for ~2 years. During all this time I was trying to keep this plugin afloat by reviewing the incoming pull requests, fixing defects and keeping the codebase up to date to simplify the handover. But I have not been using this plugin on my own so that such maintenance was a bit lame. I invite all active users to contribute to the plugin by taking ownership of this plugin and of EnvInject API. I am happy to provide any required knowledge transfers and do some assistance during the first months of maintenance

          Oleg Nenashev added a comment - Unfortunately I will not have time to work on EnvInject issues for a while. I decided to unassign all issues so that somebody can take them and finalize. Context: The plugin has been waiting for adoption for ~2 years. During all this time I was trying to keep this plugin afloat by reviewing the incoming pull requests, fixing defects and keeping the codebase up to date to simplify the handover. But I have not been using this plugin on my own so that such maintenance was a bit lame. I invite all active users to contribute to the plugin by taking ownership of this plugin and of EnvInject API. I am happy to provide any required knowledge transfers and do some assistance during the first months of maintenance

            Unassigned Unassigned
            emszpak Marcin Zajączkowski
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated: