Answers: <p>I figured it out. I was cloning directly into the workspace and then setting my environment variables to point to the workspace as well. I modified both those things. I now create a dir in my workspace and clone into it and I also set my environment variables to directories inside my workspace. Like so:</p>
  
  <pre><code>node('build-01') {
      withEnv(["CMAKE_INSTALL_DIR=${WORKSPACE}/cmake_install", "SDK_INSTALL_DIR=${WORKSPACE}/sdk"]){
          stage('Building') {
              echo "[*] Starting build (id: ${env.BUILD_ID}) on ${env.JENKINS_URL}"
              try {
                  sh 'ls -l'
                  //ls shows the damn file
                  dir('path/to/checkout') {
                      sh '. ./setup-target'
                  }
              } catch(all) { 
                  sh "echo 'Failed to run setup-target script with error: ' ${all}"
              }
          }    
      }
  }
  </code></pre>
  
  <p>This works. </p>
  
  <p>Note: Since this site is in beta phase and I barely got any views I posted the same question to stackoverflow, here it is for refernce: <a href="https://stackoverflow.com/questions/56015463/set-environment-variables-then-run-script-in-jenkins-scripted-pipeline">https://stackoverflow.com/questions/56015463/set-environment-variables-then-run-script-in-jenkins-scripted-pipeline</a> </p>
  
Answers: CC BY-SA 4.0
Answers: 2019-05-07T18:28:42.450Z
Answers: 2019-05-07T23:21:12.350Z
Answers: 2019-05-07T23:21:12.350Z
Answers: Perplexabot
Body: <p>I am new to Jenkins, Groovy and pipelines. I have created a simple pipeline stage like so:</p>
  
  <pre class="lang-java prettyprint-override"><code>//working build but not setting env variables
  node('build-01') {
      stage('Building') {
          echo "[*] Starting build (id: ${env.BUILD_ID}) on ${env.JENKINS_URL}"
          try {
              sh 'ls -l'
              //ls shows the damn file
              sh '. setup-target'
          } catch(all) { 
              sh "echo 'Failed to run setup-target script with error: ' ${all}"
          }
      }    
  }
  </code></pre>
  
  <p>This works. But I want to modify/add environment variables to the session running this script (this script is a bash file with the correct shebang line on top). So I did:</p>
  
  <pre class="lang-java prettyprint-override"><code>node('build-01') {
      withEnv(["CMAKE_INSTALL_DIR=${WORKSPACE}", "SDK_INSTALL_DIR=${WORKSPACE}"]){
          stage('Building') {
              echo "[*] Starting build (id: ${env.BUILD_ID}) on ${env.JENKINS_URL}"
              try {
                  sh 'ls -l'
                  //ls shows the damn file
                  sh '. setup-target'
              } catch(all) { 
                  sh "echo 'Failed to run setup-target script with error: ' ${all}"
              }
          }    
      }
  }
  </code></pre>
  
  <p>This errors out with:</p>
  
  <blockquote>
    <blockquote>
      <p>/home/jenkins-sw/ci/workspace/myWorkSpace@tmp/durable-6d30b48d/script.sh: line 1: .: setup-target: file not found</p>
    </blockquote>
  </blockquote>
  
  <p>and</p>
  
  <blockquote>
    <blockquote>
      <p>Failed to run setup-target script with error:  hudson.AbortException: script returned exit code 1</p>
    </blockquote>
  </blockquote>
  
  <p>But the environment variables are set, I check this by doing a <code>sh 'printenv'</code> right below the <code>ls -l</code> line. Interestingly <code>ls -l</code> does show the script. </p>
  
  <p>What am I missing?</p>
  
Tags: <jenkins><jenkins-pipeline><shell-script><groovy><environment-variables>
Title: Set environment variables then run script in Jenkins Scripted Pipeline
UserDisplayName: Perplexabot
ThreadText: # Set environment variables then run script in Jenkins Scripted Pipeline
  
  (Asked by: username_0 on 2019-05-07)
  
  I am new to Jenkins, Groovy and pipelines. I have created a simple pipeline stage like so:
  
  ```java
  //working build but not setting env variables
  node('build-01') {
      stage('Building') {
          echo "[*] Starting build (id: ${env.BUILD_ID}) on ${env.JENKINS_URL}"
          try {
              sh 'ls -l'
              //ls shows the damn file
              sh '. setup-target'
          } catch(all) { 
              sh "echo 'Failed to run setup-target script with error: ' ${all}"
          }
      }    
  }
  
  ```
  
  This works. But I want to modify/add environment variables to the session running this script (this script is a bash file with the correct shebang line on top). So I did:
  
  ```java
  node('build-01') {
      withEnv(["CMAKE_INSTALL_DIR=${WORKSPACE}", "SDK_INSTALL_DIR=${WORKSPACE}"]){
          stage('Building') {
              echo "[*] Starting build (id: ${env.BUILD_ID}) on ${env.JENKINS_URL}"
              try {
                  sh 'ls -l'
                  //ls shows the damn file
                  sh '. setup-target'
              } catch(all) { 
                  sh "echo 'Failed to run setup-target script with error: ' ${all}"
              }
          }    
      }
  }
  
  ```
  
  This errors out with:
  
  > > /home/jenkins-sw/ci/workspace/myWorkSpace@tmp/durable-6d30b48d/script.sh: line 1: .: setup-target: file not found
  
  and
  
  > > Failed to run setup-target script with error: hudson.AbortException: script returned exit code 1
  
  But the environment variables are set, I check this by doing a `sh 'printenv'` right below the `ls -l` line. Interestingly `ls -l` does show the script.
  
  What am I missing?
  
  **Tags:** jenkins, jenkins-pipeline, shell-script, groovy, environment-variables
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-05-07. Score: 0 | Accepted answer
  
  I figured it out. I was cloning directly into the workspace and then setting my environment variables to point to the workspace as well. I modified both those things. I now create a dir in my workspace and clone into it and I also set my environment variables to directories inside my workspace. Like so:
  
  ```
  node('build-01') {
      withEnv(["CMAKE_INSTALL_DIR=${WORKSPACE}/cmake_install", "SDK_INSTALL_DIR=${WORKSPACE}/sdk"]){
          stage('Building') {
              echo "[*] Starting build (id: ${env.BUILD_ID}) on ${env.JENKINS_URL}"
              try {
                  sh 'ls -l'
                  //ls shows the damn file
                  dir('path/to/checkout') {
                      sh '. ./setup-target'
                  }
              } catch(all) { 
                  sh "echo 'Failed to run setup-target script with error: ' ${all}"
              }
          }    
      }
  }
  
  ```
  
  This works.
  
  Note: Since this site is in beta phase and I barely got any views I posted the same question to stackoverflow, here it is for refernce: <https://stackoverflow.com/questions/56015463/set-environment-variables-then-run-script-in-jenkins-scripted-pipeline>
  
  
  ---
  
  