1

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?

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.