Jenkinsfile-脚本管道语法中的条件阶段执行 [英] Jenkinsfile - Conditional stage execution in the Script Pipeline syntax

查看:466
本文介绍了Jenkinsfile-脚本管道语法中的条件阶段执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在为Jenkinsfile使用脚本管道语法,该语法具有很多阶段可用于构建和部署我们的代码.我们有一个用例,如果我要执行 Full Build ,则要运行所有阶段,但是如果我需要执行一些AWS路由,则只运行一个特定阶段.我知道我可以使用if(<expression>)跳过一个阶段或运行一个阶段.问题是我不想将if条件应用于我的Jenkinsfile中的每个阶段.

We are using the Script Pipeline syntax for our Jenkinsfile which has a lot of stage defined to build and deploy our code. We have a use case where I want to run all my stages if I am doing a Full Build but only run one specific stage if I need to perform some AWS routing. I know I can use the if(<expression>) to skip a stage or run a stage. Problem is I don't want to apply that if condition to every stage in my Jenkinsfile.

使用新的 Declarative Pipeline 语法,可以使用stage..when选项轻松实现.我们的基础架构中使用了很多自定义的Groovy帮助程序功能,因此目前我无法从 Script Pipeline 语法切换到新的 Declarative Pipeline 语法.我最终在现有的Jenkinsfile上所做的事情是这样的.

In the new Declarative Pipeline syntax this is easily possible using the stage..when option. We have a lot of custom Groovy helper function used in our infrastructure so it is not possible at this point for me to switch from the Script Pipeline syntax to the new Declarative Pipeline syntax. What I ended up doing on my existing Jenkinsfile is something like this..

原始Jenkinsfile

Original Jenkinsfile

  stage('Checkout Code') {}
  stage('Build') {}
  parallel(
    stage('Sonar Analysis') {}
    stage('CLM Analysis') {}
    stage('Security Analysis') {}
  )
  stage('Build Docker Image') {}
  ...
  ...
  stage('QA Deploy') {}
  stage('QA Routing') {}
  ...
  ...
  stage('Prod Deploy') {}
  stage('Prod Routing') {}

更改为

  if (fullDeploy){
    stage('Full Build') {
        stage('Checkout Code') {}
        stage('Build') {}
        parallel(
          stage('Sonar Analysis') {}
          stage('CLM Analysis') {}
          stage('Security Analysis') {}
        )
        stage('Build Docker Image') {}
        ...
        ...
        stage('QA Deploy') {}
        stage('QA Routing') {}
        ...
        ...
        stage('Prod Deploy') {}
        stage('Prod Routing') {}          
    }
  }

  if (routeOnly){
    stage('Prod Routing') {}    
  } 

这对我来说有点不客气,我在Jenkins文档中找不到任何可以提供实现此目的的好方法.

This feels a little hacky to me and I couldn't find any things on the Jenkins docs that provides a good way to do this.

有人能以更好的方式将其整合吗?

Has anyone got a better way in which I can incorporate this?

推荐答案

我也不喜欢在我的阶段{} 中使用多余的 if {} 块的想法. 我通过覆盖以下步骤来解决了这个问题

I also disliked the idea of having a redundant if{} block inside my stage{}. I solved this by overwriting the stage as follows

def stage(name, execute, block) {
    return stage(name, execute ? block : {echo "skipped stage $name"})
}

现在您可以按照以下步骤禁用阶段

now you can disable a stage as follows

stage('Full Build', false) { 
    ...
}

更新 您还可以使用以下def标记舞台已跳过

Update You could also mark stage skipped using below def

import org.jenkinsci.plugins.pipeline.modeldefinition.Utils

def stage(name, execute, block) {
    return stage(name, execute ? block : {
        echo "skipped stage $name"
        Utils.markStageSkippedForConditional(STAGE_NAME)
    })
}

这篇关于Jenkinsfile-脚本管道语法中的条件阶段执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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