Answers: <p><code>withEnv</code> doesn't persist environment variables.  Instead, you need to tell <code>withEnv</code> what steps to run with that environment by passing it a block.   Here is what I think you want your code to look like:</p>
  
  <pre><code>#!groovy
  
  node('superhost01'){
    String HOSTNAME="host01"
    String USERNAME="tech_user"
    withEnv(['PATH=/data/jdbc_connector']) {
      stage('Prepare') {
        checkout scm
      }
  
      stage('Deploy') {
        sh """
        scp -r config.yaml ${USERNAME}@${HOSTNAME}:$PATH
        """
      }
    }
  }
  </code></pre>
  
  <p>As a side note, I suspect you actually want to set <code>PATH</code> to <code>/data/jdbc_connector:$PATH</code>, otherwise your checkout step will not be able to find git and your deploy step will not be able to find scp.</p>
  
Answers: CC BY-SA 3.0
Answers: 2018-03-19T16:01:07.683Z
Answers: 2018-03-19T16:01:07.683Z
Answers: jayhendren
Body: <p>I write script for deploy files by ssh via Jenkins</p>
  
  <pre><code>#!groovy
  
  node('superhost01'){
      String HOSTNAME="host01"
      String USERNAME="tech_user"
      withEnv(['PATH=/data/jdbc_connector'])
      stage('Prepare') {
          checkout scm
      }
      stage('Deploy') {
          sh """
          scp -r config.yaml ${USERNAME}@${HOSTNAME}:$PATH
          """
  }
  
  }
  </code></pre>
  
  <p>BUT Jenkins returned error in line</p>
  
  <pre><code>withEnv(['PATH=/data/jdbc_connector'])
  </code></pre>
  
  <p>error:</p>
  
  <pre><code>[Pipeline] node
  Running on superhost01 in /data/jenkins/workspace/jdbc_connector
  [Pipeline] {
  [Pipeline] withEnv
  [Pipeline] }
  [Pipeline] // node
  [Pipeline] End of Pipeline
  [BFA] Scanning build for known causes...
  [BFA] No failure causes found
  [BFA] Done. 0s
  java.lang.IllegalStateException: There is no body to invoke
      at org.jenkinsci.plugins.workflow.cps.CpsStepContext.newBodyInvoker(CpsStepContext.java:283)
  </code></pre>
  
  <p>how to correctly assign the value of a variable?</p>
  
Tags: <jenkins><jenkins-pipeline>
Title: How set and get var with Path?
UserDisplayName: Nikolay Baranenko
ThreadText: # How set and get var with Path?
  
  (Asked by: username_0 on 2018-03-18)
  
  I write script for deploy files by ssh via Jenkins
  
  ```
  #!groovy
  
  node('superhost01'){
      String HOSTNAME="host01"
      String USERNAME="tech_user"
      withEnv(['PATH=/data/jdbc_connector'])
      stage('Prepare') {
          checkout scm
      }
      stage('Deploy') {
          sh """
          scp -r config.yaml ${USERNAME}@${HOSTNAME}:$PATH
          """
  }
  
  }
  
  ```
  
  BUT Jenkins returned error in line
  
  ```
  withEnv(['PATH=/data/jdbc_connector'])
  
  ```
  
  error:
  
  ```
  [Pipeline] node
  Running on superhost01 in /data/jenkins/workspace/jdbc_connector
  [Pipeline] {
  [Pipeline] withEnv
  [Pipeline] }
  [Pipeline] // node
  [Pipeline] End of Pipeline
  [BFA] Scanning build for known causes...
  [BFA] No failure causes found
  [BFA] Done. 0s
  java.lang.IllegalStateException: There is no body to invoke
      at org.jenkinsci.plugins.workflow.cps.CpsStepContext.newBodyInvoker(CpsStepContext.java:283)
  
  ```
  
  how to correctly assign the value of a variable?
  
  **Tags:** jenkins, jenkins-pipeline
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-03-19. Score: 0 | Accepted answer
  
  `withEnv` doesn't persist environment variables. Instead, you need to tell `withEnv` what steps to run with that environment by passing it a block. Here is what I think you want your code to look like:
  
  ```
  #!groovy
  
  node('superhost01'){
    String HOSTNAME="host01"
    String USERNAME="tech_user"
    withEnv(['PATH=/data/jdbc_connector']) {
      stage('Prepare') {
        checkout scm
      }
  
      stage('Deploy') {
        sh """
        scp -r config.yaml ${USERNAME}@${HOSTNAME}:$PATH
        """
      }
    }
  }
  
  ```
  
  As a side note, I suspect you actually want to set `PATH` to `/data/jdbc_connector:$PATH`, otherwise your checkout step will not be able to find git and your deploy step will not be able to find scp.
  
  
  ---
  
  