Jenkins管道:用shell命令调度每个阶段的任何策略



我很高兴使用Jenkins管道来自动触发一些shell任务和Jenkins作业。其中我有一个阻断剂,我研究了很多参考文献,但找不到任何解决方案。如果您有以下内容,请提供。

问题:我们有任何策略来安排每个阶段吗?

描述:我有一个声明性的管道,我在每个阶段触发一组shell命令,我想实现每个阶段的调度,就像下面的一样

pipeline{
agent {label 'somenode'}
stages{
stage('first stage'){
triggers{cron ('* * * * *')}
steps{
//Some shell commands
}
}
stage('second stage'){
triggers{cron('* 10 * * *')}
steps{
steps{
//some other shell commands
}
}
}
}
}

如果你有任何其他路径而不是使用触发器指令,请与我分享。

Triggers指令在jenkins管道中可用,您可以在其中放置cron条件,但它将在所有阶段执行,而不是在单个阶段执行
如果需要在特定条件下执行阶段,则可以使用while子句
请参阅下面的代码/方法,它将让您了解如何在特定日期或特定条件下执行特定阶段。您可以根据自己的要求更改条件
使用插件Timestamphttps://plugins.jenkins.io/timestamper/以获取与时间戳相关的信息。

# Get the day from the build timestamp
def getday = env.BUILD_TIMESTAMP
getday = getday .split(" ")
// Extract current day
getday = getday [3]
pipeline 
{
agent any
options { timestamps () }
stages {
stage('Stage 1') {
steps {
script {
// do something
)
}
}
}
stage('Stage 2') {
when 
{
// Note: You can change condition as per your need
// Stage will run only when the day is Mon 
expression {
getday == "Mon"
}
}
steps {
script {
// do something
)
}
}
}

}

配置生成时间戳:如果您的生成时间戳未配置为获取日期信息,则可以使用以下方法进行配置https://plugins.jenkins.io/build-timestamp/管理Jenkins->配置系统->生成时间戳->单击启用BUILD_TIMESTAMP。将模式设置为:yyyy-MM-dd HH:MM:ss z EEE

相关内容

最新更新