我该如何使用Jenkins Pipeline中的properties步骤?

56

我正在研究Jenkins Pipeline:Multibranch的能力。据说最近引入了一个properties步骤可能会有用,但我无法理解它是如何工作及其目的。

它的提示信息似乎不太清晰:

更新运行此步骤的作业的属性。主要从多分支工作流程中有用,以便Jenkinsfile本身可以编码本来就是静态作业配置的内容。

因此,我创建了一个新的Pipeline,并将其作为脚本(直接粘贴到Jenkins而不是在SCM中)使用:

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

我运行了它,但什么也没有发生,作业没有收到新参数,即使有我也不明白为什么需要这个。请问有人可以解释一下吗?

更新1:我尝试在我的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)

更新2: 当我将与UPD1相同的脚本放回Jenkins并运行时,它要求新的权限 method groovy.lang.GroovyObject getProperty java.lang.String。我批准了它,构建成功,但是作业配置仍然没有变化。

我的环境是: Jenkins 1.625.3,Pipeline+Multibranch 1.13

3个回答

79

使用显式方法语法与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...  */

3
谢谢,出人意料地它有效了。我的意思是Snippet Generator生成了一个不正确的语句,同时Groovy应该允许在只有一个参数的情况下省略括号。你知道为什么会这样吗? - izzekil
1
长字段作为JENKINS-29711。 - Jesse Glick
4
请不要批准使用method groovy.lang.GroovyObject getProperty java.lang.String,这是不安全的。较新版本的_Script Security_将在执行此操作前严格警告您。 - Jesse Glick
5
代码片段生成器已经被修复。 - Jesse Glick
5
是的,重新发布此帖。katrash - 你错了,“properties()”的调用是一个重写而不是编辑,针对Jenkins有一个已记录的问题:https://issues.jenkins-ci.org/browse/JENKINS-43758...包括一个丑陋而繁琐的解决方法。@ChristopherOrr我也正在寻找修改现有属性的方法(场景-我编写了一个库,开发人员调用它。开发人员的代码已经设置了一些属性,例如“参数”。 我需要添加disallowConcurrentBuilds(),如果我调用“properties([disallowConcurrentBuilds()])”,那么由开发人员设置的属性就会被清除。 - Mike Rysanek
显示剩余4条评论

5

在官方的Jenkins文档中,有一个关于参数使用的绝佳示例。请查看下面的流水线:

pipeline {
    agent any
    parameters {
        string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')

        text(name: 'BIOGRAPHY', defaultValue: '', description: 'Enter some information about the person')

        booleanParam(name: 'TOGGLE', defaultValue: true, description: 'Toggle this value')

        choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'Pick something')

        password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'Enter a password')
    }
    stages {
        stage('Example') {
            steps {
                echo "Hello ${params.PERSON}"

                echo "Biography: ${params.BIOGRAPHY}"

                echo "Toggle: ${params.TOGGLE}"

                echo "Choice: ${params.CHOICE}"

                echo "Password: ${params.PASSWORD}"
            }
        }
    }
}

感谢 Jenkins GUI 中生成复选框的布尔参数,您可以有条件地运行/跳过测试:

pipeline {
    agent any
    parameters {
        booleanParam(name: 'RUN_TESTS', defaultValue: true, description: 'Should we run tests before deployment?')
    }

    stages {
        stage('Test') {
            when {
                expression {
                    return params.RUN_TESTS
                }
            }

            steps {
                sh '${FABRIC} test'
            }
        }

        stage('Deploy') {
            steps {
                sh '${FABRIC} deploy'
            }
        }
    }
}

4

Jenkins脚本管道中的多选项

properties([
  parameters([
        choice(choices: 'sprint_6\nsprint_7\nsprint_8\nSprint_9', description: 'Select branch to Build', name: 'Branch'),
        choice(choices: 'No\nYes', , name: 'choice2'),
        choice(choices: 'No\nYes', name: 'choice3')
  ])
])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接