Jenkins pipelineTriggers 选项崩溃

标签 jenkins groovy cron continuous-integration

尝试在 Jenkins 声明式管道脚本中使用 cron 触发的构建:

options
{
    disableConcurrentBuilds()
    timeout(time: 1, unit: 'HOURS')
    buildDiscarder(logRotator(numToKeepStr: '5'))
    pipelineTriggers([cron('H 0 * * *')])
}

运行时抛出此异常:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 30: Invalid option type "pipelineTriggers". Valid option types: [buildDiscarder, catchError, disableConcurrentBuilds, overrideIndexTriggers, retry, script, skipDefaultCheckout, skipStagesAfterUnstable, timeout, timestamps, waitUntil, withContext, withCredentials, withEnv, ws] @ line 30, column 3.
        pipelineTriggers([cron('H 0 * * *')])
     ^

1 error

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
    at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
    at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:67)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:430)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:393)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:238)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:405)
Finished: FAILURE

有人知道管道脚本中 cron 触发器的正确​​语法或解决方法吗?

最佳答案

JENKINS-37731 中,我看到使用以下语法的 cron:

properties([
    pipelineTriggers([
        cron('H/5 * * * *')
    ])
])

请参阅 jenkins-infra/jenkins.io/Jenkinsfile 示例。

由于您使用的是 declarative pipeline ,因此您需要注入(inject)这些属性:请参阅“Jenkins Declarative Pipeline: How to inject properties

You can use the script step inside the steps tag to run arbitrary pipeline code.

这是因为 pipelineTriggers and parameters are excluded from job properties ,如 pipeline/modeldefinition/model/Options.groovy 中所示

public final static List<String> BLOCKED_PROPERTIES = ["pipelineTriggers", "parameters"]

关于Jenkins pipelineTriggers 选项崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43510110/

相关文章:

windows - robocopy 导致成功退出(1)

jenkins - simctl + xcodebuild 改变模拟器状态

gradle - Gradle/Groovy:将复制闭包移动到单独的util方法

optimization - 在 groovy 中将变量标记为 final 是否有意义?

用于 SNAPSHOT 和 RELEASE Artifact 的 Maven 不同本地存储库

jenkins - 执行自定义 shell 脚本以在 jenkins 中构建

git - 通过 Groovy 访问 Jenkins 凭据存储

python - 使用 Linux ASP 在 Azure 应用服务上使用 Flask 命令进行 Crontab

python - 在 Google App Engine 上移植基本脚本

cron - 我如何以 raspbian 上的用户身份执行 crontab?