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

Env variables not available in pipeline options sections, outside of stages

    Details

    • Similar Issues:

      Description

      Environments variables such as ${env.NODE_NAME} are not available in Options {} before stages.  I want to dynamically lock a node using options {} before the first stage starts and keep that node locked for the entire pipeline. 

      I have spent hours on this everything I tried returns NULL. I know I can lock each stage individually but that won't work for me since it releases that lock at the end of each stage.

      options {
          lock (resource: "${env.NODE_NAME}")
          }
      stages {
          stage("stage name") {
               steps {.....

        Attachments

          Activity

          Hide
          lglussen Luke Lussenden added a comment - - edited

          I use the options style lock to wrap a few stages in a large pipeline and I have the same problem.  Even though the lock is deep within the pipeline, it seems the options are per-computed before the run and the ENV variables are not yet injected.   For my use case - I would really like to use the `GIT_URL` provided by the multibranch pipeline project as the source of my lock.  I don't mind multiple branches building at once, but there is a section they can't be in at the same time if they are on the same repo.

          stages {
                stage('do stuff 1'){}
                stage('do stuff 2'){}
                stage('locked set'){
                   stages {
                       options { lock(resource: "${GIT_URL}") }
                       stage('first action set'){ }
                       stage('second action set'){ }
                   }
               }
               stage('do final stuff'){}
           }

          If you have a parameterized build - I believe parameters are available to the options blocks - but not environment variables.

          Show
          lglussen Luke Lussenden added a comment - - edited I use the options style lock to wrap a few stages in a large pipeline and I have the same problem.  Even though the lock is deep within the pipeline, it seems the options are per-computed before the run and the ENV variables are not yet injected.   For my use case - I would really like to use the `GIT_URL` provided by the multibranch pipeline project as the source of my lock.  I don't mind multiple branches building at once, but there is a section they can't be in at the same time if they are on the same repo. stages {      stage( ' do stuff 1' ){}      stage( ' do stuff 2' ){}      stage( 'locked set' ){         stages {             options { lock(resource: "${GIT_URL}" ) }             stage( 'first action set' ){ }             stage( 'second action set' ){ }         }     }     stage( ' do final stuff' ){} } If you have a parameterized build - I believe parameters are available to the options blocks - but not environment variables.

            People

            • Assignee:
              Unassigned
              Reporter:
              chadg Chad Geisler
            • Votes:
              3 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

              • Created:
                Updated: