詹金斯管道传递变量

时间:2018-11-09 09:01:49

标签: jenkins jenkins-pipeline

我有一个管道,我正在通过docker容器构建图像,并输出图像标签,我想将该图像标签传递到下一个阶段,当我在下一个阶段回显它时,它将打印出来。但是当我在外壳中使用它时,它会变空。这是我的管道

pipeline {
  agent any
  stages {
    stage('Cloning Git') {
      steps {
      git( url: 'https://xxx@bitbucket.org/xxx/xxx.git',
            credentialsId: 'xxx',
            branch: 'master')

      }
    }
    stage('Building Image') {
      steps{
         script {
         env.IMAGE_TAG = sh script: "docker run -e REPO_APP_BRANCH=master -e REPO_APP_NAME=exampleservice -e DOCKER_HUB_REPO_NAME=exampleservice --volume /var/run/docker.sock:/var/run/docker.sock registry.xxxx/build", returnStdout: true

        }
      }
    }

    stage('Integration'){
      steps{
        script{
                echo "passed: ${env.IMAGE_TAG}"
          sh """
            helm upgrade exampleservice charts/exampleservice --set image.tag=${env.IMAGE_TAG}
            """
            sh "sleep 5"


        }
      }
    }

  }
}

管道输出

[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Integration)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
passed: 
 b79c3bf-b6eec4f


[Pipeline] sh
[test101] Running shell script
+ helm upgrade exampleservice charts/exampleservice --set image.tag=

获取空图片标签

1 个答案:

答案 0 :(得分:1)

您应该使用'env'覆盖它。

用以下代码替换您的代码:

pipeline {
  agent any
  stages {
    stage('Cloning Git') {
      steps {
      git( url: 'https://xxx@bitbucket.org/xxx/xxx.git',
            credentialsId: 'xxx',
            branch: 'master')

      }
    }
    stage('Building Image') {
      steps{
         script {
         env.IMAGE_TAG = sh script: "docker run -e REPO_APP_BRANCH=master -e REPO_APP_NAME=exampleservice -e DOCKER_HUB_REPO_NAME=exampleservice --volume /var/run/docker.sock:/var/run/docker.sock registry.xxxx/build", returnStdout: true

        }
      }
    }

    stage('Integration'){
      steps{
        script{
                echo "passed: ${env.IMAGE_TAG}"
          sh """
            helm upgrade exampleservice charts/exampleservice\            
            --set image.tag="${env.IMAGE_TAG}"
            """
            sh "sleep 5"


        }
      }
    }

  }
}