在詹金斯的重要管道的不同阶段重用对象/财产 [英] Reuse object/property in different stages of a declerative pipeline in Jenkins

查看:211
本文介绍了在詹金斯的重要管道的不同阶段重用对象/财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们创建一个新的maven构建:

  def rtMaven = Artifactory.newMavenBuild()

现在我们想在不同的阶段重用这个rtMaven;
,如下面的代码所示:

  pipeline {
代理任何

...
阶段{

阶段('stage1'){
步骤{
脚本{
def rtMaven = Artifactory.newMavenBuild()



$ stage
$ {
$ {
// REUSE rtMaven(现在不得而知)




$ b $ / code>

是否有可能在第二阶段重新使用rtMaven而不重新定义它?



现在我们有一个错误:

  groovy.lang.MissingPropertyException:没有这样的属性:rtMaven for class:groovy.lang.Binding 


解决方案

在全局范围内定义var

  def rtMaven =''
pipeline {
代理任何
阶段{
阶段('stage1'){
步骤{
脚本{
rtMaven = Artifactory.newMavenBuild()
}
}
}

stage('stage2'){
步骤{
脚本{
echo$ rtMaven
}
}
}
}
}


We create a new maven build:

def rtMaven = Artifactory.newMavenBuild()

Now we want to reuse this rtMaven in a different stage than the current one; like in the code below:

pipeline {
agent any

...
stages {

    stage('stage1') {
        steps {
            script {
                def rtMaven = Artifactory.newMavenBuild()
            }
    }

    stage('stage2') {
         steps {
            script {
                //REUSE rtMaven (now it's unknown)
            }
         }

     }
}

Is it possible to reuse the rtMaven without redefining it again in the second stage?

Now we have an error like:

groovy.lang.MissingPropertyException: No such property: rtMaven for class: groovy.lang.Binding

解决方案

Define the var in global scope

def rtMaven = ''
pipeline {
agent any
stages {
stage('stage1') {
    steps {
        script {
           rtMaven = Artifactory.newMavenBuild()
        }
   }
}

stage('stage2') {
     steps {
        script {
            echo "$rtMaven"
        }
     }
    }
  }
}

这篇关于在詹金斯的重要管道的不同阶段重用对象/财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆