jenkinsfile 没有将 env 传递给 sh

标签 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 步骤中使用它?

最佳答案

我认为这是 Jenkins 的事情。当您使用 sh' 阻止;它将无法访问诸如环境变量之类的东西。尝试使用 "反而。那应该工作

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

Jenkins 应该识别 shell 块并逃脱 """"自动地。
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"
            }
        }
    }
}

My test case


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

Result


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

关于jenkinsfile 没有将 env 传递给 sh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55071024/

相关文章:

jenkins-pipeline - "Scripts not permitted to use staticMethod..."但没有未决的签名批准

linux - Jenkins 管道选择特定分支

linux - 在 linux 中更改 jenkins 的工作区

java - 如何在 Jenkins 插件中将对象绑定(bind)到 URL

jenkins - 如何从jenkinsfile中获取管道的名称

jenkins - 如何修复管道脚本 "Expected a step"错误

javascript - 随着时间的推移,Allure 报告不断增长并需要更长的时间来生成

jenkins - Gradle:如何将URL解析为存储库中SNAPSHOT版本的最新版本?

bash - Jenkins Pipeline Step withEnv 在没有 BASH 的情况下无法工作

windows - 如何在 Jenkins 中使用 cygwin 调用私有(private)仓库上的 git 命令来执行 bash 脚本