首页 > 解决方案 > 如何在 Jenkins 管道中运行特定阶段

问题描述

如何在特定阶段运行Jenkins declarative pipeline

例子:

如果我想我run only the stage 3(Deploy Nexus artifact) without running the stage 1, 2 and 4.怎么能做到这一点?

标签: jenkins-pipeline

解决方案


您可以在要跳过的每个阶段使用when表达式。变量checkoutCode, runSonarScan,deployNexusArtifact可以静态或动态设置为环境变量。

environment {
   checkoutCode = true
   runSonarScan = true
   deployNexusArtifact = true
}

stage('Gitlab code Checkout') {
    when { expression { "${checkoutCode}" == 'true' } }
    ...
}

stage('Sonarqube scan') {
    when { expression { "${runSonarScan}" == 'true' } }
    ...
}

stage('Deploy Nexus artifact') {
    when { expression { "${deployNexusArtifact}" == 'true' } }
    ...
}

推荐阅读