Jenkins以给定间隔触发构建步骤/阶段(不是整个作业) [英] Jenkins Triggering of a Build Step/Stage(not the entire job) at a given interval

查看:197
本文介绍了Jenkins以给定间隔触发构建步骤/阶段(不是整个作业)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个管道,在该管道中我需要链接多个作业,其中一些作业必须在某个特定时间启动.

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.

例如:Job1(从午夜开始)-> Job2-> Job3-> Job4(从下午4点开始)

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

使用声明性语法:

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
            }
        }        
    }
}

在创建此作业时,我已将作业配置为从午夜12点开始.因此,第一个作业会在午夜自动开始.

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

但是,我还需要第二份工作(CI Day-1),从凌晨1点开始.最后一个作业中间结果"的开始时间是下午6点.

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).

此外,是否在阶段/步骤中尝试了触发器{cron(0 1 * * 6)}.没有运气!

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

推荐答案

这是在一天中的指定时间启动另一个作业的快速方法.使用Groovy代码,计算所需的启动时间和当前时间之间的时间差(以秒为单位),并将其用作quietPeriod参数的参数.

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.

如果出现错误不允许使用方法的脚本",则必须使用管理Jenkins">进程内脚本批准"来批准方法.

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
}

这篇关于Jenkins以给定间隔触发构建步骤/阶段(不是整个作业)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆