もりはやメモφ(・ω・ )

ITとか読書感想文とか

Jenkinsfileでタイムスケジュール実行

Jenkinsfileでcron定期実行指定

v2になってから久しい*1Jenkinsですが、Jenkinsfileによるジョブのコード化がとても良いです。

小ネタでJenkinsfileでのcron実行の方法を調べたのでメモです。

Documentのcron-syntaxに書いてある通りなのですが、以下のようにcronでかけます。

Jenkinsfile (Declarative Pipeline)
pipeline {
    agent any
    triggers {
        cron('H */4 * * 1-5')   <-ここ
    }
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

ここで面白いのが H の部分。 これは標準のcronには無いパラメタで、乱数的な意味になります。具体的にはサンプルの例だと、分に H が指定あるため 00-59 のどこか、となります。

しかし注意点として、ドキュメントにも以下のように記載があり、乱数(random)では無いとのことです。ジョブ名からハッシュで数値が決定され、毎回変動するわけではありません。

The H symbol can be thought of as a random value over a range, but it actually is a hash of the job name, not a random function, so that the value remains stable for any given project.

おまけ

簡単だと思って追加したところ、以下のエラーが発生しました。 これは読んで字のごとくなのですが、trigger句が複数回出ているぞ!というエラーです。

WorkflowScript: 23: Multiple occurrences of the triggers section @ line 23, column 5.

というのもサンプルを見てagent anyの下に以下を追加したのですが

   ...略...
    agent any
    triggers {
       cron('H 22 * * *')
    }
   ...略...

実はJenkinsfileの下の方にGitLab連携用にtriggersが記載されており、重複していたというお粗末な理由でした。これはサンプルをそのまま使うとありそうな内容なので、自戒として記載しておきます。

    triggers {
        cron('H 22 * * *')   <-ここに追加するのが正しかった
        gitlab(
            triggerOnPush: true,
            triggerOnMergeRequest: true,
            branchFilterType: 'All',
            addNoteOnMergeRequest: true,
            addCiMessage: true
        )
    }

参考までにエラー全文を記載すると以下(マスクしてます)。 Multiple occurrences of ther xxxxx section が出たら今後は一発でわかりますね。

Branch indexing
 > git rev-parse --is-inside-work-tree # timeout=10
Setting origin to https://mygitlab/oreno/project.git
 > git config remote.origin.url https://mygitlab/oreno/project.git # timeout=10
Fetching origin...
Fetching upstream changes from origin
 > git --version # timeout=10
 > git config --get remote.origin.url # timeout=10
using GIT_ASKPASS to set credentials Git username/password for https://mygitlab/oreno/project.git
 > git fetch --tags --progress origin +refs/heads/*:refs/remotes/origin/* # timeout=10
Seen branch in repository origin/master
Seen 1 remote branch
Obtained Jenkinsfile from e294460e7fba067df5a48f5faaf2e45780ba6187
Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 23: Multiple occurrences of the triggers section @ line 23, column 5.
       triggers {
       ^

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.doParse(CpsGroovyShell.java:131)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:125)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:560)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:521)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:320)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE

*1:2016/04/20らしい