Ronald Ding
Ronald Ding

Reputation: 1

How do I get ArtifactoryMavenBuild to run with a variable name as an argument for declarative Jenkins?

I am trying to write a Jenkins pipeline using declarative syntax (if I really can't make any progress, I'll switch to script). However, I can't figure out how to get the return value of functions to store to a variable so I can use that variable as an argument of the next function.

The stage of my pipeline looks like this:

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

My error log is:

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

The ArtifactoryMavenBuild function works when I put it like this:

ArtifactoryMavenBuild(mavenBuild: newMavenBuild(), tool: "M3", pom: "pom.xml", goals: "-B clean test -Dmaven.test.failure.ignore", opts: "", buildInfo: newBuildInfo())

But I need to be able to reference mvBuild and buildInfo again for a later step.

The documentation for declarative jenkins for the Artifactory plugin is here: https://jenkins.io/doc/pipeline/steps/artifactory/

Upvotes: 0

Views: 476

Answers (1)

lanoxx
lanoxx

Reputation: 13051

Try to wrap you script code into a script {} step like so:

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

Upvotes: 1

Related Questions