Jenkins-parsedVersion属性可以在管道脚本中使用吗?

时间:2018-07-01 18:46:24

标签: maven jenkins groovy jenkins-pipeline

我有一个Jenkins Mavin项目工作,该工作建立了SNAPSHOT版本并增加了POM版本。在“目标和选项”字段中:

  

全新安装build-helper:parse-version版本:set   -DnewVersion = $ {parsedVersion.majorVersion}。$ {parsedVersion.minorVersion}。$ {parsedVersion.nextIncrementalVersion} -SNAPSHOT   版本:提交

这按预期工作,没有问题。但是,当我尝试使用以下命令本质上执行相同的操作时,单独的Jenkins管道作业将失败:

  

sh mvn全新安装build-helper:parse-version版本:set   -DnewVersion = $ {parsedVersion.majorVersion}。$ {parsedVersion.minorVersion}。$ {parsedVersion.nextIncrementalVersion} -SNAPSHOT   版本:提交

控制台输出显示:

  

groovy.lang.MissingPropertyException:无此类属性:parsedVersion   适用于:WorkflowScript,网址为   org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)     在   org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)     在org.kohsuke.groovy.sandbox.impl.Checker $ 6.call(Checker.java:290)     在   org.kohsuke.groovy.sandbox.GroovyInterceptor.onGetProperty(GroovyInterceptor.java:68)     在   org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:326)     在org.kohsuke.groovy.sandbox.impl.Checker $ 6.call(Checker.java:288)     在   org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:292)     在   org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:268)     在   org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:268)     在   org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:268)     在   org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:268)     在   com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)     在   com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)     在WorkflowScript.run(WorkflowScript:33)在    cps.transform (本机方法),位于com.cloudbees.groovy.cps.impl.PropertyishBlock $ ContinuationImpl.get(PropertyishBlock.java:74)     在   com.cloudbees.groovy.cps.LValueBlock $ GetAdapter.receive(LValueBlock.java:30)     在   com.cloudbees.groovy.cps.impl.PropertyishBlock $ ContinuationImpl.fixName(PropertyishBlock.java:66)     在sun.reflect.GeneratedMethodAccessor974.invoke(未知源)处   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)在   com.cloudbees.groovy.cps.impl.ContinuationPtr $ ContinuationImpl.receive(ContinuationPtr.java:72)     在   com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)     在com.cloudbees.groovy.cps.Next.step(Next.java:83)处   com.cloudbees.groovy.cps.Continuable $ 1.call(Continuable.java:174)在   com.cloudbees.groovy.cps.Continuable $ 1.call(Continuable.java:163)在   org.codehaus.groovy.runtime.GroovyCategorySupport $ ThreadCategoryInfo.use(GroovyCategorySupport.java:122)     在   org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:261)     在com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)     在   org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access $ 101(SandboxContinuable.java:34)     在   org.jenkinsci.plugins.workflow.cps.SandboxContinuable.lambda $ run0 $ 0(SandboxContinuable.java:59)     在   org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)     在   org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:58)     在   org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:174)     在   org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:332)     在   org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access $ 200(CpsThreadGroup.java:83)     在   org.jenkinsci.plugins.workflow.cps.CpsThreadGroup $ 2.call(CpsThreadGroup.java:244)     在   org.jenkinsci.plugins.workflow.cps.CpsThreadGroup $ 2.call(CpsThreadGroup.java:232)     在   org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService $ 2.call(CpsVmExecutorService.java:64)     在java.util.concurrent.FutureTask.run(FutureTask.java:266)在   hudson.remoting.SingleLaneExecutorService $ 1.run(SingleLaneExecutorService.java:112)     在   jenkins.util.ContextResettingExecutorService $ 1.run(ContextResettingExecutorService.java:28)     在   java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:511)     在java.util.concurrent.FutureTask.run(FutureTask.java:266)在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)     在   java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)     在java.lang.Thread.run(Thread.java:748)

我想念什么吗?

2 个答案:

答案 0 :(得分:2)

我无法获得metalisticpain的解决方案,但是在进行故障排除时,我遇到了使用这种方法的GitHub groovy脚本:

  

sh'/ usr / local / maven / bin / mvn build-helper:parse-version版本:set   -DnewVersion = \\\ $ {parsedVersion.majorVersion}。\\\ $ {parsedVersion.minorVersion}。\\\ $ {parsedVersion.nextIncrementalVersion}-快照   版本:commit'

现在完美运行。

答案 1 :(得分:0)

正如khmarbaise所暗示的那样,您只需要用引号引起来。

特别是:

sh('mvn clean install build-helper:parse-version versions:set -DnewVersion=${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.nextIncrementalVersion}-SNAPSHOT versions:commit')

这将通过,而不会尝试将$ {var}评估为Jenkins / Groovy变量。如果使用双引号,则会遇到相同的错误,因为这是groovy中的GString。 http://docs.groovy-lang.org/latest/html/api/groovy/lang/GString.html

另一种方式, 首先将pom读入内存并提取必要的数据,然后通过mvn版本进行设置:set

def pom = readMavenPom file: 'pom.xml'
String major = pom.parsedVersion.majorVersion
String minor = pom.parsedVersion.majorVersion
String nextIncremental = pom.parsedVersion.majorVersion
sh("clean install build-helper:parse-version versions:set -DnewVersion=${major}.${minor}.${nextIncremental}-SNAPSHOT versions:commit")