如何自定义 Jenkins 管道阶段视图?
我想自定义 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.
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?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使舞台内容有条件执行时(如您的示例中所示),无论条件是否满足,舞台都会显示。如果您想在不满足条件时隐藏舞台,唯一的选择是将舞台放在条件内。不过,该插件的作者警告不要这样做:
缺点是在大多数情况下,当阶段数量发生变化时,整个阶段视图都会重置。因此,每次您的构建更改分支时,舞台视图中所有先前的构建都会消失。
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:
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.