詹金斯:通过单个管道安排特定阶段 [英] Jenkins: Schedule Particular Stage with single pipeline

查看:72
本文介绍了詹金斯:通过单个管道安排特定阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个声明式的管道,并且有多个阶段,例如下面通过webhook触发的事件.

I have a single pipeline where it' declarative and I've several stages such as below triggers via webhook.

我想在某个时间执行和安排阶段B,该阶段也可以通过webhook触发而无需运行.显然,它需要在通过webhook触发时运行,也需要在预定时间运行.我可以在Jenkins中创建单独的作业或管道而无需处理吗?

I would like to execute and scheduled Stage B at a certain time which can also run without trigger via webhook. Clearly it needs to run when triggers via webhook and also run when it will be schedule. Can I handle this without creating seperate job or pipeline in Jenkins ?

           stage('A'){
                when{
                    beforeAgent true
                    expression{return env.GIT_BRANCH == "origin/development"}
                }
                steps{
                    script{
                         //Do something
            }  


            stage ('B'){
                when {
                    beforeAgent true
                    expression{return env.GIT_BRANCH == "origin/development"}
                steps {
                    script {
                        //Run Tests
                    }
                }
            }


            stage('C'){
                when{
                    beforeAgent true
                    expression{return env.GIT_BRANCH == "origin/development"}
                }
                steps{
                    script{
                       //Do something
            }

推荐答案

您可以找到导致管道运行的原因.这可能是cron触发器,手动触发器,代码提交触发器,webhook触发器,在GitHub上的注释,上游作业等(取决于安装的插件,列表可能很长.)

You can discover what caused your pipeline to run. This may be cron trigger, manual trigger, code commit trigger, webhook trigger, comment on GitHub, upstream job, etc. (depending on plugins installed, the list may be long.)

这里是代码示例,用以了解触发器是什么.本示例设置环境变量TRIGGERED_BY.

Here's and example of code to understand what the trigger was. This example sets the environment variable TRIGGERED_BY.

def checkForTrigger() {
    def timerCause = currentBuild.rawBuild.getCause(hudson.triggers.TimerTrigger.TimerTriggerCause)
    if (timerCause) {
        echo "Build reason: Build was started by timer"
        env.TRIGGERED_BY = 'timer'
        return
    }

    def userCause = currentBuild.rawBuild.getCause(hudson.model.Cause$UserIdCause)
    if (userCause) {
        echo "Build reason: Build was started by user"
        env.TRIGGERED_BY = 'user'
        return
    }

    def remoteCause = currentBuild.rawBuild.getCause(hudson.model.Cause$RemoteCause)
    if (remoteCause) {
        echo "Build reason: Build was started by remote script"
        env.TRIGGERED_BY = 'webhook'
        return
    }
    // etc.
    println "We haven't caught any of triggers, might be a new one, here is the dump"
    def causes = currentBuild.rawBuild.getCauses()
    println causes.dump()
}

我认为一个构建可能有多个触发器,如果​​这样的话,子句的顺序很重要.

I think that a build might have more than one trigger, if so the order of your clauses is important.

一旦弄清了这个问题,就只能在触发器符合您的定义的情况下运行阶段.

Once you have this figured out, you can run your stages only when the trigger fits your definition.

            stage ('B'){
                when {
                    beforeAgent true
                    anyOf {
                       expression{return env.GIT_BRANCH == "origin/development"}
                       environment name: 'TRIGGERED_BY', value: 'timer'
                       environment name: 'TRIGGERED_BY', value: 'webhook'
                    }

这篇关于詹金斯:通过单个管道安排特定阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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