Home > Mobile >  Check if build job finished before certain time
Check if build job finished before certain time

Time:08-26

I maintain a jenkins pipeline which has the requirement to finish before 10am in the morning.

Is there a way to detect that the job finished after 10am and print a warning for example?

CodePudding user response:

Here is a sample Pipeline to achieve your requirement. Here I have used a post-build step to check the time. You can decide what you want to do after checking the time. Eg: Setting the build to unstable, sending a mail etc.

pipeline {
    agent any
    stages{
        stage('Build') { 
            steps{
                echo "RUNNING THE BUILD!!!!" 
            }
        }
    }
    post { 
        always { 
            script{
                echo 'Checking the time'
                def timeToCheckBefore = [hourOfDay: 10, minute: 0, second: 0] // 10PM will be 23, 0, 0
                def now = new Date()
                def check = now.clone()
                check.set(timeToCheckBefore)
                
                if(now.after(check)) {
                    echo "Time is Passed: Current Time : $now, Should finish before: $check"
                } else {
                    echo "Job finished timely: Current Time : $now, Should finish before: $check"
                }
            }
            
        }
    }
}
  • Related