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

Problems expanding variables created outside the Jenkins build

      Some plugins stopped to work when macros are putted to expand. That worked well before. You can see almost identical stacktrace

      https://issues.jenkins-ci.org/browse/JENKINS-37222
      https://github.com/jenkinsci/stashnotifier-plugin/issues/117

          [JENKINS-37238] Problems expanding variables created outside the Jenkins build

          it has never worked using token macro I am injecting the variable using the EnvInject plugin to create the environment variable as seen here:

          18:36:56 [EnvInject] - Loading node environment variables.
          18:36:56 [EnvInject] - Preparing an environment for the build.
          18:36:56 [EnvInject] - Keeping Jenkins system variables.
          18:36:56 [EnvInject] - Keeping Jenkins build variables.
          18:36:56 [EnvInject] - Executing and processing the following script content:
          18:36:56 echo ${branch}
          18:36:56 FOO=`git ls-remote ssh://git@stash:7999/techops/puppet.git origin ${branch} | cut -f1`
          18:36:56
          18:36:56
          18:36:56 [/tmp] $ /bin/sh -xe /tmp/hudson4110050367620133632.sh
          18:36:56 + echo feature-jenkins-slave-swarm-qax
          18:36:56 feature-jenkins-slave-swarm-qax
          18:36:56 ++ git ls-remote ssh://git@stash:7999/techops/puppet.git origin feature-jenkins-slave-swarm-qax
          18:36:56 ++ cut -f1
          18:36:56 + FOO=a75e94c868219fdbcd2acab968d8f4a02a3dac7c
          18:36:56 [EnvInject] - Script executed successfully.
          18:36:56 [EnvInject] - Injecting contributions.

          As I can surmise here, my variable is being populated via EnvInject, I can printenv and see FOO with the correct sha. When using the above suggestion I am definitely passing something different to the notifer as i get a 405 response from the stash server.

          Shawn Baumkirchner added a comment - it has never worked using token macro I am injecting the variable using the EnvInject plugin to create the environment variable as seen here: 18:36:56 [EnvInject] - Loading node environment variables. 18:36:56 [EnvInject] - Preparing an environment for the build. 18:36:56 [EnvInject] - Keeping Jenkins system variables. 18:36:56 [EnvInject] - Keeping Jenkins build variables. 18:36:56 [EnvInject] - Executing and processing the following script content: 18:36:56 echo ${branch} 18:36:56 FOO=`git ls-remote ssh://git@stash:7999/techops/puppet.git origin ${branch} | cut -f1` 18:36:56 18:36:56 18:36:56 [/tmp] $ /bin/sh -xe /tmp/hudson4110050367620133632.sh 18:36:56 + echo feature-jenkins-slave-swarm-qax 18:36:56 feature-jenkins-slave-swarm-qax 18:36:56 ++ git ls-remote ssh://git@stash:7999/techops/puppet.git origin feature-jenkins-slave-swarm-qax 18:36:56 ++ cut -f1 18:36:56 + FOO=a75e94c868219fdbcd2acab968d8f4a02a3dac7c 18:36:56 [EnvInject] - Script executed successfully. 18:36:56 [EnvInject] - Injecting contributions. As I can surmise here, my variable is being populated via EnvInject, I can printenv and see FOO with the correct sha. When using the above suggestion I am definitely passing something different to the notifer as i get a 405 response from the stash server.

          Alex Earl added a comment -

          It's possible that the variable is no longer available once the post build actions are being executed. Did you try the ${ENV...} method?

          Alex Earl added a comment - It's possible that the variable is no longer available once the post build actions are being executed. Did you try the ${ENV...} method?

          I did try that method:

          ${ENV, var="$FOO"}

          with FOO above being the sha, this is the correct syntax correct?

          Shawn Baumkirchner added a comment - I did try that method: ${ENV, var="$FOO"} with FOO above being the sha, this is the correct syntax correct?

          Alex Earl added a comment -

          No $ in front of FOO

          Alex Earl added a comment - No $ in front of FOO

          So I took your advice, you are right, the FOO variable which is added via EnvInject is not available during the post build steps. I moved the variable creation to a post buildstep shell step and the issue persists. I also changed the variable to as you requested: ${ENV, var="FOO"}

          Shawn Baumkirchner added a comment - So I took your advice, you are right, the FOO variable which is added via EnvInject is not available during the post build steps. I moved the variable creation to a post buildstep shell step and the issue persists. I also changed the variable to as you requested: ${ENV, var="FOO"}

          Alex Earl added a comment -

          Environment variables are not available outside the step that creates them, e.g. a shell step can't create an environment variable that will stick around after that step.

          Alex Earl added a comment - Environment variables are not available outside the step that creates them, e.g. a shell step can't create an environment variable that will stick around after that step.

          Right, so you're saying an environment var which is created in post build would not be available in post build? or is it plugin specific?

          Shawn Baumkirchner added a comment - Right, so you're saying an environment var which is created in post build would not be available in post build? or is it plugin specific?

          Alex Earl added a comment -

          Not available from one post build step to the next. Normally though, anything you create from a normal shell script will not be available outside that specific step (whether build or post-build). This is why EnvInject was created. I am not sure it allows setting environment variables during post-build.

          Alex Earl added a comment - Not available from one post build step to the next. Normally though, anything you create from a normal shell script will not be available outside that specific step (whether build or post-build). This is why EnvInject was created. I am not sure it allows setting environment variables during post-build.

          I've decided to just leave this alone for now. I don't have any more time to work on it. It seems that trying to export the variable at any step the variables are not found. Thank you so much for your help on this Alex. Please close at your leisure.

          Shawn Baumkirchner added a comment - I've decided to just leave this alone for now. I don't have any more time to work on it. It seems that trying to export the variable at any step the variables are not found. Thank you so much for your help on this Alex. Please close at your leisure.

          Alex Earl added a comment -

          Sorry it's not working for you. I've been hit by the inability to get environment variables exposed as well.

          Alex Earl added a comment - Sorry it's not working for you. I've been hit by the inability to get environment variables exposed as well.

            slide_o_mix Alex Earl
            scaytrase Pavel Batanov
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: