jenkinsfile没有将环境传递给sh

roy*_*roy 2 jenkins jenkins-pipeline

我正在尝试通过以下方式在Jenkinsfile中设置环境变量,

pipeline {
    agent { label 'slave1' }

    stages {
        stage ('Build') {
            steps {
                script {
                    BUILD_VERSION = sh (
                        script: 'python get_firmware_version.py',
                        returnStdout: true
                    ).trim()
                }
                echo "${BUILD_VERSION}"
                withCredentials([file(credentialsId: 'image-sign', variable: 'IMAGE_SIGN')]) {
                    dir('firmware/') {
                        echo "${BUILD_VERSION}"
                        sh '''
                           echo "Building"
                           echo "${BUILD_VERSION}"
                           echo "${env.BUILD_VERSION}"
                        '''
                    }
                }
            }
        }
    }
    post {
        failure {
            script {
                echo "Pipeline Failed"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是它失败并出现以下错误 Bad substitution

[Pipeline] echo
0_2_0
[Pipeline] sh
+ echo Building
Building
/home/jenkins/jenkins_slave/workspace/Firmware/Branch/firmware@tmp/durable-54e04481/script.sh: 3: /home/jenkins/jenkins_slave/workspace/Firmware/Branch/firmware@tmp/durable-54e04481/script.sh: Bad substitution
Run Code Online (Sandbox Code Playgroud)

为什么我不能设置ENV Var并在sh step中使用它?

Pra*_*tne 6

我认为这是詹金斯的事情。当您将sh块与'; 它将无法访问诸如环境变量之类的东西。尝试使用"代替。那应该工作

sh """
    echo "Building"
    echo "${env.BUILD_VERSION}"
    echo "${env}"
"""
Run Code Online (Sandbox Code Playgroud)

詹金斯应该认识到外壳块,逃离"的范围内"""自动完成。

pipeline {
    agent { label 'slave1' }

    stages {
        stage ('Build') {
            steps {
                script {
                    BUILD_VERSION = sh (
                        script: 'python get_firmware_version.py',
                        returnStdout: true
                    ).trim()
                }
                echo "${BUILD_VERSION}"
                withCredentials([file(credentialsId: 'image-sign', variable: 'IMAGE_SIGN')]) {
                    dir('firmware/') {
                        echo "${BUILD_VERSION}"
                        sh """
                           echo "Building"
                           echo "${BUILD_VERSION}"
                           echo "${env.BUILD_VERSION}"
                        """
                    }
                }
            }
        }
    }
    post {
        failure {
            script {
                echo "Pipeline Failed"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的测试用例

pipeline {
  agent {
    node {
      label 'devops-jenkins-slave'
    }
  }

  options {
    timestamps()
  }

  stages {

    stage('Setup'){
      steps {
        dir("${WORKSPACE}/"){
          script {
            BUILD_VERSION = "1"
          }

          sh """
           echo "${BUILD_VERSION}"
          """
        }
      }
    }

  }

    post {
      always {
        dir("${WORKSPACE}/"){
          deleteDir()
        }
      }
    }
}
Run Code Online (Sandbox Code Playgroud)

结果

Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on devops-jenkins-slave in /home/jenkins/workspace/Samples/Test
[Pipeline] {
[Pipeline] timestamps
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Setup)
[Pipeline] dir
22:09:55 Running in /home/jenkins/workspace/Samples/Test
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] }
[Pipeline] // script
[Pipeline] sh
22:09:56 [Test] Running shell script
22:09:56 + echo 1
22:09:56 1
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] dir
22:09:56 Running in /home/jenkins/workspace/Samples/Test
[Pipeline] {
[Pipeline] deleteDir
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // timestamps
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Run Code Online (Sandbox Code Playgroud)