如何使用变量名作为声明性Jenkins的参数运行ArtifactoryMavenBuild?

时间:2017-07-12 02:05:16

标签: jenkins groovy jenkins-pipeline artifactory

我正在尝试使用声明性语法编写Jenkins管道(如果我真的无法取得任何进展,我将切换到脚本)。但是,我无法弄清楚如何获取要存储到变量的函数的返回值,因此我可以将该变量用作下一个函数的参数。

我的管道阶段如下:

stage ('Build') {
   steps {
      def artServer = getArtifactoryServer(artifactoryServerID: 'my-server')
      def mvBuild = newMavenBuild()
      def buildInfo = newBuildInfo()
      ArtifactoryMavenBuild(mavenBuild: mvBuild, tool: "M3", pom: "pom.xml", goals: "-B clean test -Dmaven.test.failure.ignore", opts: "", buildInfo: buildInfo)
   }
}

我的错误日志是:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 19: Expected a step @ line 19, column 17.
                   def artServer = getArtifactoryServer(artifactoryServerID: 'GE-Propel-Artifactory')
                   ^

WorkflowScript: 20: Expected a step @ line 20, column 17.
                   def mvBuild = newMavenBuild()
                   ^

WorkflowScript: 21: Expected a step @ line 21, column 17.
                   def buildInfo = newBuildInfo()

当我这样说时,ArtifactoryMavenBuild函数会起作用:

ArtifactoryMavenBuild(mavenBuild:newMavenBuild(),工具:“M3”,pom:“pom.xml”,目标:“ - B clean test -Dmaven.test.failure.ignore”,opts:“”,buildInfo:newBuildInfo ())

但我需要能够再次引用mvBuild和buildInfo以进行后续步骤。

Artifactory插件的声明性jenkins文档位于:https://jenkins.io/doc/pipeline/steps/artifactory/

1 个答案:

答案 0 :(得分:1)

尝试将脚本代码包装成script {}步骤,如下所示:

stage ('Build') {
  steps {
    script {
       def artServer = getArtifactoryServer(artifactoryServerID: 'my-server')
       def mvBuild = newMavenBuild()
       def buildInfo = newBuildInfo()
       ArtifactoryMavenBuild(mavenBuild: mvBuild, tool: "M3", pom: "pom.xml", goals: "-B clean test -Dmaven.test.failure.ignore", opts: "", buildInfo: buildInfo)
    }
  }
}