Jenkins Build Pipeline Scheduled Trigger

前端 未结 6 752
独厮守ぢ
独厮守ぢ 2020-12-13 06:15

How can a build pipeline be scheduled to execute at a certain time of the night just like a regular job can be?

相关标签:
6条回答
  • 2020-12-13 06:40

    Into the main job configuration of your pipeline (the first), set the "Build periodically" checkbox, and specify the schedule that you want.

    follow the syntax indications.

    the field follows the syntax of cron (with minor differences). Specifically, each line consists of 5 fields separated by TAB or whitespace:
     MINUTE HOUR DOM MONTH DOW
     MINUTE   Minutes within the hour (0–59)
     HOUR The hour of the day (0–23)
     DOM  The day of the month (1–31)
     MONTH    The month (1–12)
     DOW  The day of the week (0–7) where 0 and 7 are Sunday.
    
     To specify multiple values for one field, the following operators are available. In the order of precedence,
    
        * specifies all valid values
        M-N specifies a range of values
        M-N/X or */X steps by intervals of X through the specified range or whole valid range
        A,B,...,Z enumerates multiple values
    
     Examples:
    
     # every fifteen minutes (perhaps at :07, :22, :37, :52)
     H/15 * * * *
     # every ten minutes in the first half of every hour (three times, perhaps at :04, :14, :24)
     H(0-29)/10 * * * *
     # once every two hours every weekday (perhaps at 9:38 AM, 11:38 AM, 1:38 PM, 3:38 PM)
     H 9-16/2 * * 1-5
     # once a day on the 1st and 15th of every month except December
     H H 1,15 1-11 *
    
    0 讨论(0)
  • 2020-12-13 06:40

    If you want to run the job periodically for a specific branch using a multibranch pipeline you can do this in your Jenkinsfile:

    def call(String cronBranch = 'master') {
    
        // Cron job to be run from Monday to Friday at 10.00h (UTC)
        String cronString = BRANCH_NAME == cronBranch ? "0 10 * * 1-5" : ""
    
        pipeline {
            agent any
    
            triggers {
                cron(cronString)
            }
            stages {
                stage('My Stage') {
                    //Do something
                }
            }
        }
    }
    

    You need to run this job manually the first time in order to be added.

    0 讨论(0)
  • 2020-12-13 06:45

    You can set the job parameters using the following syntax:

    properties([pipelineTriggers([cron('H 23 * * *')])])
    

    Adding this line to your build script or Jenkinsfile will configure the job to run every night at 11PM.

    0 讨论(0)
  • 2020-12-13 06:48

    Declarative pipeline has triggers directive, one uses it like this:

    triggers { cron('H 4/* 0 0 1-5') }
    

    I took it from Pipeline Syntax docs

    0 讨论(0)
  • 2020-12-13 06:51

    If you use the Build pipeline plugin you can simply add a trigger to the first job and that will trigger the full pipeline

    If you are using Jenkins 2.0 and create a new item of type pipeline, then you can simply schedule it like you would any other job

    0 讨论(0)
  • 2020-12-13 07:00

    Complete Example (taken from docs) Ref: https://jenkins.io/doc/book/pipeline/syntax/#triggers

    pipeline {
        agent any
        triggers {
            cron('H */4 * * 1-5')
        }
        stages {
            stage('Example') {
                steps {
                    echo 'Hello World'
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题