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

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

问题描述

我们为我们的 Jenkinsfile 使用了 Script Pipeline 语法,它定义了很多阶段来构建和部署我们的代码.我们有一个用例,如果我正在执行完整构建,我想运行我的所有阶段,但如果我需要执行某些 AWS 路由,则只运行一个特定阶段.我知道我可以使用 if() 跳过一个阶段或运行一个阶段.问题是我不想将该 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.

在新的声明式流水线语法中,使用 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) { 
    ...
}

更新您还可以使用以下定义标记跳过阶段

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天全站免登陆