Search code examples
jenkinscronjenkins-pipelinejenkins-declarative-pipeline

Jenkins Triggering of a Build Step/Stage(not the entire job) at a given interval


I am trying to build a pipeline where i would need to chain multiple jobs and some of them has to start at a certain time.

Ex: Job1(starts at Midnight) -> Job2 -> Job3 ->Job4(starts at 4 PM)

Using Declarative Syntax:

pipeline {
    agent any
    stages{
        stage('Fetch Latest Code-1') { 
            steps{
                build job: 'Get Latest - All Nodes', quietPeriod: 60
            }        
        }
        stage('CI Day - 1') {
            parallel {
                stage('ANZ CI'){
                    steps{
                        build job: 'ANZ - CI', quietPeriod: 120    
                    }
                }
                stage('BRZ CI'){
                    steps{
                        build job: 'BRZ_CI', quietPeriod: 120
                    }
                }
                stage('NAM CI'){
                    steps{
                        build job: 'NAM - CI', quietPeriod: 120    
                    }
                }
            }
        }
        stage('BEP Part 2') { 
            steps{
                build job: 'BEP_CI_Verification_Job', quietPeriod: 180
            }        
        }
        stage('Intermediate Results') {
            steps{
                build job: 'CI Automation Results', parameters: [string(name: 'Files', value: '_CI_')], quietPeriod: 300
            }
        }        
    }
}

As I create this job, I had configured the Job to start at 12 Midnight. Therefore, the 1st job automatically gets started at midnight. enter image description here

But, I would also need the second job(CI Day - 1) to begin at 1 AM & the last Job 'Intermediate results' to start at 6 PM.

As these jobs are Multi-Configuration Jobs(already tried setting them individually at the desired timings but they get overwritten when called through pipeline).

Also, did try triggers{ cron(0 1 * * 6) } within the stage/steps. No luck!


Solution

  • Here is a quick idea for launching another job at a given time of day. Using Groovy code, calculate the difference in seconds between the desired launch time and the current time and use it as argument for the quietPeriod parameter.

    If you get an error "Scripts not permitted to use method", you have to approve the methods using "Manage Jenkins" > "In-process script approval".

    import groovy.time.*
    
    pipeline {
        agent any
        stages{
            stage('Stage 1') { 
                steps{
                    script {
                        def secs = secondsUntil hourOfDay: 15, minute: 30, second: 0
                        echo "anotherjob will be triggered in $secs seconds"
    
                        build job: 'anotherjob', quietPeriod: secs
                    }
                }        
            }
        }
    }
    
    long secondsUntil( Map dateProperties ) {
        def now = new Date()
        def to = now.clone()
        to.set( dateProperties )
        long duration = TimeCategory.minus( to, now ).toMilliseconds() / 1000
        return duration > 0 ? duration : 0
    }