如何正确设置默认值?

时间:2018-06-21 12:06:22

标签: jenkins jenkins-plugins jenkins-pipeline

关于this documentation,设置默认值的正确方法是什么?

例如:如何使用文档中的示例,如何为config.name设置默认值?

如何使用Map config作为唯一参数来定义必需可选值?

1 个答案:

答案 0 :(得分:2)

您可以在包含默认值的脚本类级别上创建一个映射,并且可以简单地合并两个映射-一个包含默认值,另一个作为call(Map config)函数的参数提供。

关于必需值,您可以在assert config.fieldName : 'Error message'函数的开头添加call(),以测试用户是否通过config映射传递了必需值。看一下这个例子:

import groovy.transform.Field

// Map with the default values
@Field
Map defaults = [
    id: 0,
    name: 'test'
]

def call(Map config) {
    // Here we test if required config.id was set
    assert config.id : 'config.id must be set!'

    // Here we combine two maps and we allow to override default values
    config = defaults + config

    node {
        echo config.name
    }
}

现在,如果我将Jenkinsfile定义为:

buildPlugin name: 'lorem', id: 13

运行它,我将获得以下控制台输出:

[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test-pipeline
[Pipeline] {
[Pipeline] echo
lorem
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

如果我忘记指定id参数,则管道将在开始时失败:

[Pipeline] End of Pipeline
java.lang.AssertionError: config.id must be set!. Expression: assert config.id : 'config.id must be set!'
    at org.codehaus.groovy.runtime.InvokerHelper.assertFailed(InvokerHelper.java:406)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.assertFailed(ScriptBytecodeAdapter.java:650)
    at com.cloudbees.groovy.cps.impl.AssertBlock$ContinuationImpl.fail(AssertBlock.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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:83)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:122)
    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:261)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:174)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:331)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:82)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:243)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:231)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    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:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Finished: FAILURE

如果我不指定config.name参数,将使用默认参数。