2016-02-12 39 views
25

我正在学习詹金斯管道的能力:多分支。据说最近推出的properties这一步可能会很有用,但我无法理解它的工作原理和目的。如何使用Jenkins管道属性步骤?

它的提示消息似乎并不十分清楚:

更新它运行这一步工作的性质。主要用于多分支工作流程,因此Jenkinsfile本身可以编码静态作业配置。

所以我创造了这个新的管道作为脚本(直接粘贴到詹金斯没有SCM):

properties [[$class: 'ParametersDefinitionProperty', 
    parameterDefinitions: [[$class: 'StringParameterDefinition', 
     defaultValue: '', description: '', name: 'PARAM1']] 
]] 

我跑它,什么都没有发生,工作并没有收到新的参数,即使它没有,我不明白为什么我可能需要这个。谁能解释一下吗?

UPDATE1:我试着把属性的虚拟管道放入我的git仓库,然后配置了一个多分支工作。

println 1 
properties [[$class: 'ParametersDefinitionProperty', parameterDefinitions: [[$class: 'StringParameterDefinition', defaultValue: 'str1', description: '', name: 'PARAM1']]], [$class: 'RebuildSettings', autoRebuild: false, rebuildDisabled: false]] 
println 2 

它发现我的分支,创建了一个工作,但构建与失败:

groovy.lang.MissingPropertyException: No such property: properties for class: groovy.lang.Binding 
at groovy.lang.Binding.getVariable(Binding.java:62) 
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:185) 
at org.kohsuke.groovy.sandbox.impl.Checker$4.call(Checker.java:241) 
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:238) 
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:23) 
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:17) 
at WorkflowScript.run(WorkflowScript:2) 
at ___cps.transform___(Native Method) 
at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:62) 
at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30) 
at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:54) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:601) 
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72) 
at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21) 
at com.cloudbees.groovy.cps.Next.step(Next.java:58) 
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154) 
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:19) 
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:33) 
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:30) 
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:106) 
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:30) 
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:164) 
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:277) 
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$000(CpsThreadGroup.java:77) 
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:186) 
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:184) 
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:47) 
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) 
at java.util.concurrent.FutureTask.run(FutureTask.java:166) 
at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112) 
at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28) 
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) 
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) 
at java.util.concurrent.FutureTask.run(FutureTask.java:166) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 
at java.lang.Thread.run(Thread.java:722) 

UPDATE2:当我把同样的脚本(如UPD1)回詹金斯和运行它,它要求新的许可method groovy.lang.GroovyObject getProperty java.lang.String。我批准了它,构建变成了绿色,但是,仍然没有出现对作业配置的更改。

我ENV是:詹金斯1.625.3,管道+多枝1.13

回答

33

使用properties有明确方法的语法都可以工作,即:
properties([ ... ])而不是properties [ ... ]

或者,它会没有,如果你的工作指定参数名称,例如:

properties properties: [ ... ] 

例如,定义三个属性非常简单:

properties([ 
    parameters([ 
    string(name: 'submodule', defaultValue: ''), 
    string(name: 'submodule_branch', defaultValue: ''), 
    string(name: 'commit_sha', defaultValue: ''), 
    ]) 
]) 

/* Accessible then with : params.submodule, params.submodule_branch... */ 
+1

非常感谢,它的工作原理。我的意思是代码片段生成器生成不正确的语句,同时groovy应该允许在给定的参数只有一个时省略括号。你知道它为什么这样吗? – izzekil

+0

@izzekil是的,它看起来很奇怪,只有一个参数的步骤不能按预期工作。我不知道这是为什么。您可以尝试[提交bug](https://wiki.jenkins-ci.org/display/JENKINS/How+to+report+an+issue)与'workflow-plugin'组件相对应,并指定'multibranch'标签。 –

+1

Long作为JENKINS-29711提交。 –

相关问题