如果在 yaml 中将前一阶段部署条件设置为 false,则阻止部署
Prevent deployment if previous stage deployment condition is set to false in yaml
我使用 yaml 创建了一个 build/release 管道:
这里,第一阶段是'Build'。一旦构建完成并成功,下一阶段是 'deploy to ua'。如果部署到 ua 完成并成功,下一阶段是 'deploy to prod'.
如此处所示,'deployment to ua' 的条件返回了 'false'。如果 'deployment to ua' 的条件为假,我将如何跳过部署到生产。
我在 deployment to ua
阶段为作业设置条件时重现了上述场景。由于条件为假,作业“部署资源-Ip”将被跳过。但是 deployment to ua
阶段评估成功。下一阶段将继续 运行。
解决此问题的方法是在阶段级别而不是作业级别设置 conditions and dependencies。对于以下示例:
Stage Release B依赖于Release A,当Stage Release A不成功时将被跳过(/skipped)。当条件为假时,将跳过 Stage Release A(而不是如果在作业级别设置条件,则跳过作业 B)。
stages:
- stage: Build
jobs:
- job: A
steps:
- powershell: echo "Stage A"
- stage: Release_A
dependsOn: Build
condition: and(succeeded('Build'), eq(variables['build.sourceBranch'], 'refs/heads/master'))
jobs:
- job: B
steps:
- powershell: echo "Stage B"
- stage: Release_B
dependsOn: Release_A
condition: succeeded('Release_A')
jobs:
- job: C
steps:
- powershell: echo "Stage C"
但是如果你想在职位级别设置条件。您需要在阶段 deployment to ua
中添加另一个作业,以便在条件为假时故意使自身失败(以便评估该阶段失败)。
希望以上对您有所帮助!
我使用 yaml 创建了一个 build/release 管道:
这里,第一阶段是'Build'。一旦构建完成并成功,下一阶段是 'deploy to ua'。如果部署到 ua 完成并成功,下一阶段是 'deploy to prod'.
如此处所示,'deployment to ua' 的条件返回了 'false'。如果 'deployment to ua' 的条件为假,我将如何跳过部署到生产。
我在 deployment to ua
阶段为作业设置条件时重现了上述场景。由于条件为假,作业“部署资源-Ip”将被跳过。但是 deployment to ua
阶段评估成功。下一阶段将继续 运行。
解决此问题的方法是在阶段级别而不是作业级别设置 conditions and dependencies。对于以下示例:
Stage Release B依赖于Release A,当Stage Release A不成功时将被跳过(/skipped)。当条件为假时,将跳过 Stage Release A(而不是如果在作业级别设置条件,则跳过作业 B)。
stages:
- stage: Build
jobs:
- job: A
steps:
- powershell: echo "Stage A"
- stage: Release_A
dependsOn: Build
condition: and(succeeded('Build'), eq(variables['build.sourceBranch'], 'refs/heads/master'))
jobs:
- job: B
steps:
- powershell: echo "Stage B"
- stage: Release_B
dependsOn: Release_A
condition: succeeded('Release_A')
jobs:
- job: C
steps:
- powershell: echo "Stage C"
但是如果你想在职位级别设置条件。您需要在阶段 deployment to ua
中添加另一个作业,以便在条件为假时故意使自身失败(以便评估该阶段失败)。
希望以上对您有所帮助!