jenkinsfile没有将环境传递给sh

时间:2019-03-08 21:09:02

标签: 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"
            }
        }
    }
}

但是失败,并显示以下错误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

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

1 个答案:

答案 0 :(得分:3)

这是詹金斯的想法。当您将sh块与'一起使用时;它将无法访问诸如环境变量之类的东西。尝试改用"。那应该工作

sh """
    echo "Building"
    echo "${env.BUILD_VERSION}"
    echo "${env}"
"""

Jenkins应该识别外壳程序块并自动"中的"""转义。

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

我的测试用例

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()
        }
      }
    }
}
  

结果

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
相关问题