Answers: <p>If you want to use environment variables with the <code>${VAR}</code> syntax in the declarative pipeline, then you need to define those variables in the <code>environment { }</code> block.</p>
  
  <p>for example:  </p>
  
  <pre><code>environment {
          DISABLE_AUTH = 'true'
          DB_ENGINE    = 'sqlite'
      }
  
      stages {
          stage('Build') {
              steps {
                  echo "Database engine is ${DB_ENGINE}"
                  echo "DISABLE_AUTH is ${DISABLE_AUTH}"
                  sh 'printenv'
              }
          }
      }
  
  </code></pre>
  
  <p>If you want to use environment variables that come from other plugins etc, then you need to access them with the <code>env.VAR</code> syntax.  For example:</p>
  
  <p><code>echo "Running ${env.BUILD_ID} on ${env.JENKINS_URL}"</code></p>
  
Answers: CC BY-SA 4.0
Answers: 2019-09-09T07:50:26.867Z
Answers: 2019-09-09T07:50:26.867Z
Answers: avi
Answers: <p>Not sure if you did, but put the vars into double quotes:</p>
  
  <pre><code>... "${BUILD_CAUSE}"
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-04-10T19:00:53.640Z
Answers: Thanks, Chris, for your try to help, but it's not about that. I tried different work-arounds, including setting vars in Groovy style (e.g. env.BUILD_CAUSE), but nothing helped.
Answers: acd
Answers: CC BY-SA 4.0
Answers: 2019-04-10T14:00:09.490Z
Answers: 2019-04-10T14:00:09.490Z
Answers: Chris
Body: <p>In FreeStyle Project, I have lots of env vars, which are available from any part of Jenkins' job. I also used <a href="https://github.com/jenkinsci/ghprb-plugin" rel="nofollow noreferrer">ghprb</a> plugin to trigger that job against GH PRs, which added some extra env vars.</p>
  
  <p>Later, that job became more complex, so I've decided to put it on Jenkins Pipeline. But now, the issue is, I can't retrieve those env vars in a pipeline, which were available before, in the Freestyle Project.</p>
  
  <p>I'm a newbie in writing Jenkins Pipeline, that's why I decided to use declarative approach for that purpose (but I don't think it's root cause). So, my question is, how to retrieve env vars in Jenkins Pipeline, such as: <code>${BUILD_CAUSE}</code>, <code>${ghprbTargetBranch}</code>, <code>${ghprbActualCommitAuthorEmail}</code>?</p>
  
Comments: CC BY-SA 4.0
Comments: 2019-04-10T13:57:30.047Z
Comments: Can you post an example snippet where you try to use the vars? 
  On which OS is your Jenkins instance based? Windows?
Comments: Chris
Comments: CC BY-SA 4.0
Comments: 2019-04-10T19:10:27.577Z
Comments: OS, in general, is Linux.
  
  Snippet: `if [ $BUILD_CAUSE == MANUALTRIGGER ]; then
      exit 0
  fi
  
  NC=$(git merge-base HEAD remotes/origin/$ghprbTargetBranch)`
Comments: acd
Comments: CC BY-SA 4.0
Comments: 2019-04-11T06:47:48.297Z
Comments: The bracket is missing here, isn't it? `${BUILD_CAUSE}` instead of `$BUILD_CAUSE`
Comments: Chris
Comments: CC BY-SA 4.0
Comments: 2019-04-11T08:55:49.020Z
Comments: No, man, it doesn't really matter, as it's a shell. In a Freestyle project it works fine.
Comments: acd
Comments: CC BY-SA 4.0
Comments: 2019-05-14T22:30:30.457Z
Comments: Are you using a multi-branch pipeline? The PRs are handled in a different way and many variables are different.
Comments: plmaheu
Tags: <jenkins-pipeline>
Title: Get vars in Jenkins Pipeline
UserDisplayName: acd
ThreadText: # Get vars in Jenkins Pipeline
  
  (Asked by: username_0 on 2019-04-08)
  
  In FreeStyle Project, I have lots of env vars, which are available from any part of Jenkins' job. I also used [ghprb](https://github.com/jenkinsci/ghprb-plugin) plugin to trigger that job against GH PRs, which added some extra env vars.
  
  Later, that job became more complex, so I've decided to put it on Jenkins Pipeline. But now, the issue is, I can't retrieve those env vars in a pipeline, which were available before, in the Freestyle Project.
  
  I'm a newbie in writing Jenkins Pipeline, that's why I decided to use declarative approach for that purpose (but I don't think it's root cause). So, my question is, how to retrieve env vars in Jenkins Pipeline, such as: `${BUILD_CAUSE}`, `${ghprbTargetBranch}`, `${ghprbActualCommitAuthorEmail}`?
  
  **Tags:** jenkins-pipeline
  
  > **Comments:**
  > **username_1**: Can you post an example snippet where you try to use the vars?  On which OS is your Jenkins instance based? Windows?  
  > **username_0**: OS, in general, is Linux.  Snippet: `if [ $BUILD_CAUSE == MANUALTRIGGER ]; then     exit 0 fi  NC=$(git merge-base HEAD remotes/origin/$ghprbTargetBranch)`  
  > **username_1**: The bracket is missing here, isn't it? `${BUILD_CAUSE}` instead of `$BUILD_CAUSE`  
  > **username_0**: No, man, it doesn't really matter, as it's a shell. In a Freestyle project it works fine.  
  > **username_2**: Are you using a multi-branch pipeline? The PRs are handled in a different way and many variables are different.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2019-09-09. Score: 2
  
  If you want to use environment variables with the `${VAR}` syntax in the declarative pipeline, then you need to define those variables in the `environment { }` block.
  
  for example:
  
  ```
  environment {
          DISABLE_AUTH = 'true'
          DB_ENGINE    = 'sqlite'
      }
  
      stages {
          stage('Build') {
              steps {
                  echo "Database engine is ${DB_ENGINE}"
                  echo "DISABLE_AUTH is ${DISABLE_AUTH}"
                  sh 'printenv'
              }
          }
      }
  
  ```
  
  If you want to use environment variables that come from other plugins etc, then you need to access them with the `env.VAR` syntax. For example:
  
  `echo "Running ${env.BUILD_ID} on ${env.JENKINS_URL}"`
  
  
  ---
  
  ### Answer by username_1 on 2019-04-10. Score: 0
  
  Not sure if you did, but put the vars into double quotes:
  
  ```
  ... "${BUILD_CAUSE}"
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks, username_1, for your try to help, but it's not about that. I tried different work-arounds, including setting vars in Groovy style (e.g. env.BUILD_CAUSE), but nothing helped.  
  
  ---
  
  