You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure DevOps流水线中检查Helm Release存在后执行卸载

在Azure DevOps流水线中仅当Release存在时执行Helm卸载

要实现只在infra Release存在时执行卸载,核心思路是先检查Release状态,再通过流水线变量控制卸载任务是否执行,具体步骤如下:

1. 添加检查Release的脚本任务

新增一个脚本任务(选Bash或PowerShell均可),执行Helm命令判断test命名空间下的infra Release是否存在,然后设置流水线变量标记状态:

Bash版本脚本任务

- task: Bash@3
  displayName: 检查infra Release是否存在
  inputs:
    targetType: 'inline'
    script: |
      # 检查test命名空间下的infra release
      helm list -n test | grep -q "infra"
      if [ $? -eq 0 ]; then
        echo "##vso[task.setvariable variable=releaseExists]true"
        echo "找到infra Release,准备执行卸载"
      else
        echo "##vso[task.setvariable variable=releaseExists]false"
        echo "没找到infra Release,跳过卸载步骤"
      fi

PowerShell版本脚本任务

- task: PowerShell@2
  displayName: 检查infra Release是否存在
  inputs:
    targetType: 'inline'
    script: |
      $releaseInfo = helm list -n test --output json | ConvertFrom-Json
      $exists = $releaseInfo | Where-Object Name -eq 'infra'
      if ($exists) {
        Write-Host "##vso[task.setvariable variable=releaseExists]true"
        Write-Host "找到infra Release,准备执行卸载"
      }
      else {
        Write-Host "##vso[task.setvariable variable=releaseExists]false"
        Write-Host "没找到infra Release,跳过卸载步骤"
      }

2. 修改原Helm卸载任务,添加执行条件

给你原来的HelmDeploy@0任务加上condition字段,只有当releaseExists变量为true时才会触发卸载:

- task: HelmDeploy@0
  displayName: Helm uninstall test
  condition: eq(variables.releaseExists, 'true')
  inputs:
    connectionType: Kubernetes Service Connection
    kubernetesServiceEndpoint: dev-test
    command: uninstall
    chartType: FilePath
    chartPath: infra
    waitForExecution: true
    namespace: test
    arguments: infra

额外说明

  • 确保流水线使用的Kubernetes服务账号有执行helm list的权限
  • 也可以用helm status infra -n test替代helm list,该命令返回码为0表示Release存在,非0表示不存在,脚本逻辑可相应调整

内容的提问来源于stack exchange,提问作者Ukchat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 06:45:27