如何自定义 Jenkins 管道阶段视图?

发布于 2025-01-17 07:23:54 字数 1957 浏览 2 评论 0原文

我想自定义 Jenkins 管道阶段。
在下面的屏幕截图中,我不希望步骤批准 K8s 开发部署创建并部署到 k8s 开发环境 显示在管道阶段视图中,因为我跳过了这些步骤根据我的分支名称。下面是当前的输出。

输入图片此处描述

我希望在没有批准 K8s 开发部署创建并部署到 k8s 开发环境的情况下看到如下所示的管道阶段视图。我想要我的预期输出如下。我错过了什么插件吗?我怎样才能实现这个目标?

下面是我的常规代码:

stages{
    stage('Checkout') {
        steps{
            checkout scm
        }
    }

    // Maven Build and Unit Tests Dev
    stage('Build and Unit Tests') {
        steps{
            build(configuration)
        }
    }

    // SonarQube Analysis
    stage('SonarQube analysis') {
        steps{
            sonarQubeGating(configuration)
        }
    }

    // Build Docker Image and Push to Artifactory
    stage('Build Docker Image and Push to Artifactory') {
        steps{
            artifactoryImagePush(configuration)
        }
    }

    // Approve DEV Deployment
    stage('Approve K8s Dev Deployment') {
        when {
            anyOf {
                expression {
                    return (env.GIT_BRANCH.startsWith('master') || env.GIT_BRANCH.startsWith('hotfix-'))
                }
            }
        }
        steps {
            approveDeployment()
        }
    }

    // Create and Deploy to Dev Environment
    stage('Create and Deploy to k8s Dev Environment') {
        when {
            anyOf {
                expression {
                    return (env.GIT_BRANCH.startsWith('master') || env.GIT_BRANCH.startsWith('hotfix-'))
                }
            }
        }
        steps {
            withCredentials([string(credentialsId: "$env.K8S_DEV_NS_TOKEN", variable: 'DEV_TOKEN')]) {
                kubernetesDeploy(hcEnv: 'dev', hcToken: "${DEV_TOKEN}")
            }
        }
    }
}

I want to customize the Jenkins pipeline stage.
In the below screenshot, I don't want the steps Approve K8s Dev Deployment and Create and Deploy to k8s Dev Environment to show in the pipeline stage view as I am skipping these based on my branch names. Below is the current output.

enter image description here

I want the pipeline stage view to be seen something like below without the Approve K8s Dev Deployment and Create and Deploy to k8s Dev Environment. I want my expected output as below. Am I missing out any plugins? How can I achieve this?

enter image description here

Below is my groovy code:

stages{
    stage('Checkout') {
        steps{
            checkout scm
        }
    }

    // Maven Build and Unit Tests Dev
    stage('Build and Unit Tests') {
        steps{
            build(configuration)
        }
    }

    // SonarQube Analysis
    stage('SonarQube analysis') {
        steps{
            sonarQubeGating(configuration)
        }
    }

    // Build Docker Image and Push to Artifactory
    stage('Build Docker Image and Push to Artifactory') {
        steps{
            artifactoryImagePush(configuration)
        }
    }

    // Approve DEV Deployment
    stage('Approve K8s Dev Deployment') {
        when {
            anyOf {
                expression {
                    return (env.GIT_BRANCH.startsWith('master') || env.GIT_BRANCH.startsWith('hotfix-'))
                }
            }
        }
        steps {
            approveDeployment()
        }
    }

    // Create and Deploy to Dev Environment
    stage('Create and Deploy to k8s Dev Environment') {
        when {
            anyOf {
                expression {
                    return (env.GIT_BRANCH.startsWith('master') || env.GIT_BRANCH.startsWith('hotfix-'))
                }
            }
        }
        steps {
            withCredentials([string(credentialsId: "$env.K8S_DEV_NS_TOKEN", variable: 'DEV_TOKEN')]) {
                kubernetesDeploy(hcEnv: 'dev', hcToken: "${DEV_TOKEN}")
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜血缘 2025-01-24 07:23:54

当您使舞台内容有条件执行时(如您的示例中所示),无论条件是否满足,舞台都会显示。如果您想在不满足条件时隐藏舞台,唯一的选择是将舞台放在条件内。不过,该插件的作者警告不要这样做:

动态阶段:一般来说,如果您想要可视化动态变化的阶段,请使其有条件执行阶段内容,而不是有条件包含阶段** 阶段视图可以处理您要附加附加内容的有限子集的情况阶段,但基于单元的视图与不断变化的阶段结构不匹配
(来自 https://plugins.jenkins.io/pipeline-stage-view

缺点是在大多数情况下,当阶段数量发生变化时,整个阶段视图都会重置。因此,每次您的构建更改分支时,舞台视图中所有先前的构建都会消失。

When you make the stage content conditionally executed (as in your example), the stage will show up whether or not the condition is met. If you want to hide the stage when the condition is not met, the only option is to put the stage inside the condition. The authors of the plugin caution against it though:

Dynamic stages: in general, if you want to visualize dynamically changing stages, make it conditional to execute the stage contents, not conditional to include the stage** Stage view can handle a limited subset of cases where you're appending additional stages, but the cell-based view is not matched to changing stage structures
(from https://plugins.jenkins.io/pipeline-stage-view)

The downside is that in most cases, when the number of stages change, the whole stage view is reset. So every time your build changes branches, all previous builds in the stage view are gone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文