N
N
Nikolay Baranenko2018-03-16 14:05:00
Jenkins
Nikolay Baranenko, 2018-03-16 14:05:00

How to correctly assign a value to a variable with a path and pass it to sh?

Hello.
During the development of the script file for jenkins, it was necessary to assign the path value to the variable in order to further pass it to sh
prepared the script

#!groovy

node('superhost01'){
    String HOSTNAME="host01"
    String USERNAME="tech_user"
    env.PATH="/data/jdbc_connector"
    stage('Prepare') {
        checkout scm
    }
    stage('Deploy') {
        sh """
        scp -r config.yaml ${USERNAME}@${HOSTNAME}:$PATH
        """
}

}

BUT when it is executed by Jenkins, the task fails by mistake in the line
env.PATH="/data/jdbc_connector"
How to correctly assign a value to a variable with a path and pass it to sh?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolay Baranenko, 2018-03-19
@drno-reg

correct option is to use withEnv

#!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
      """
    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question