【问题标题】:Jenkins Declarative Pipeline: Trigger Specific Stage In PipelineJenkins 声明式管道:触发管道中的特定阶段
【发布时间】:2021-08-02 22:09:42
【问题描述】:

我正在使用 Jenkins 声明式管道,想知道是否有任何方法可以仅定期触发特定阶段。

我的意思是当我们检查 SCM 时,管道会触发,但是对于我们的某些项目来说,第 2 阶段花费的时间太长。因此,我不想等待这个阶段,而是希望每天运行这个阶段,但仍将这个阶段保留在 jenkinsfile 中。

有什么方法可以实现吗?这样做的最佳方法是什么?

    stage('Stage 1') {
        steps {
            script {
                  // do something
                )
            }
        }
    }

    stage('Stage 2') {
      triggers {
        cron('H H 0 0 0')
        }
        steps {
            script {
                  // do something
                )
            }
        }
    }
    stage('Stage 2') {
        steps {
            script {
                  // do something
                )
            }
        }
    }

【问题讨论】:

  • 我提供的解决方案对你有用吗
  • @Altaf,我现在正在尝试,但令人困惑的是管道是如何触发的?我的意思是如何在不触发的情况下启动它?我希望它需要在没有 SCM 的情况下触发,例如triggers{} 正在做什么
  • @seumral : 您可以从构建触发器部分下的 jenkins 配置中触发,因此您不需要在管道脚本中添加它
  • @Altaf,所以它会只触发这个阶段还是完整的管道?

标签: jenkins jenkins-pipeline jenkins-declarative-pipeline


【解决方案1】:

请参阅下面的代码/方法,它将让您了解如何在特定日期或特定条件下执行特定阶段。您可以根据您的要求更改条件
使用插件 Timestamper https://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

请注意:在 jenkins 管道中可以使用 Triggeres 指令,如下所示,您可以在其中放置 cron 条件,但它将在所有阶段而不是单个阶段执行。

// Declarative //
pipeline {
    agent any
    triggers {
        cron('H H 0 0 0')
    }
    stages {
        stage('Stage 1') {
        steps {
            script {
                  // do something
                )
            }
        }
      }
    }
}

【讨论】:

  • 我无法修复错误Invalid option type "timestamps".. 它需要插件吗? Jenkins 2.263.4 是版本
  • @semural : 是的,你可以使用插件 Timestamper https://plugins.jenkins.io/timestamper/
  • 不幸的是我又遇到了一个错误。java.lang.ArrayIndexOutOfBoundsException: 3.. env.BUILD_TIMESTAMP 的输出是 2021-05-14 12:50:35 CEST .. 我看不到工作日信息
  • @semural: 哦,你需要配置它:管理 Jenkins -> 配置系统 -> 构建时间戳 -> 点击启用 BUILD_TIMESTAMP。设置模式:yyyy-MM-dd HH:mm:ss z EEE。立即在答案中添加此信息
  • 我可以通过另一种方式获得这一天,但也许是我的错没有正确澄清我的情况..Day 不是正确的参数,也许或者可能没有办法做到这一点.. 让我们说今天是星期五和我检查开发分支..由于它的星期五它会再次工作,但我希望它应该在没有检查的情况下自动触发,但只有这个阶段应该每天在管道中运行
猜你喜欢
  • 2022-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-11
  • 2018-08-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多