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

查看:27
本文介绍了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.

但是,我还需要在凌晨 1 点开始第二份工作(CI Day - 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天全站免登陆