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

如何自动修改Azure DevOps经典容器应用流水线的ImageVersion并触发?

实现Azure DevOps经典流水线ImageVersion自动递增并触发的具体方案

前提条件

  • 拥有目标Azure DevOps项目的流水线编辑权限、变量编辑权限
  • 准备一个具备Build (Read & Execute)和Build (Read & Edit)权限的个人访问令牌(PAT),并在YAML流水线中配置为秘密变量(命名为PatToken)
  • 记录好经典流水线的ID(可从流水线页面URL中提取,如/build?definitionId=123里的123)

YAML流水线完整实现示例

以下示例假设ImageVersion是经典流水线的本地变量(如为变量组变量,可参考末尾注意事项调整):

1. 获取并递增ImageVersion

- task: PowerShell@2
  displayName: '获取并计算新版本号'
  name: VersionCalc
  inputs:
    targetType: 'inline'
    script: |
      # 配置参数
      $orgUrl = "$(System.TeamFoundationCollectionUri)"
      $projectName = "$(System.TeamProject)"
      $pipelineId = "123" # 替换为你的经典流水线ID
      $pat = "$(PatToken)"

      # 构建认证头
      $base64Auth = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat"))
      $headers = @{ Authorization = "Basic $base64Auth" }

      # 获取经典流水线定义
      $pipelineApiUrl = "$orgUrl$projectName/_apis/build/definitions/$pipelineId?api-version=7.1-preview.7"
      $pipelineDef = Invoke-RestMethod -Uri $pipelineApiUrl -Headers $headers -Method Get

      # 提取当前版本号并递增(假设为语义化版本,如1.0.0)
      $currentVersion = ($pipelineDef.variables | Where-Object {$_.key -eq "ImageVersion"}).value
      $versionSegments = $currentVersion -split '\.'
      $versionSegments[2] = [int]$versionSegments[2] + 1 # 递增第三位,可按需调整
      $newVersion = $versionSegments -join '.'

      # 输出变量供后续任务使用
      Write-Output "##vso[task.setvariable variable=NewImageVersion;isOutput=true]$newVersion"
      Write-Output "##vso[task.setvariable variable=PipelineDefinition;isOutput=true]$($pipelineDef | ConvertTo-Json -Depth 10)"

2. 更新经典流水线的ImageVersion变量

- task: PowerShell@2
  displayName: '更新流水线版本变量'
  inputs:
    targetType: 'inline'
    script: |
      $orgUrl = "$(System.TeamFoundationCollectionUri)"
      $projectName = "$(System.TeamProject)"
      $pipelineId = "123" # 替换为你的经典流水线ID
      $pat = "$(PatToken)"
      $newVersion = "$(VersionCalc.NewImageVersion)"
      $pipelineDef = $(VersionCalc.PipelineDefinition) | ConvertFrom-Json

      $base64Auth = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat"))
      $headers = @{
        Authorization = "Basic $base64Auth"
        "Content-Type" = "application/json"
      }

      # 更新变量值
      $pipelineDef.variables | Where-Object {$_.key -eq "ImageVersion"} | ForEach-Object {$_.value = $newVersion}

      # 提交更新请求
      $updateApiUrl = "$orgUrl$projectName/_apis/build/definitions/$pipelineId?api-version=7.1-preview.7"
      Invoke-RestMethod -Uri $updateApiUrl -Headers $headers -Method Put -Body ($pipelineDef | ConvertTo-Json -Depth 10)

      Write-Host "已成功将ImageVersion更新为: $newVersion"

3. 触发经典流水线

- task: PowerShell@2
  displayName: '触发经典流水线执行'
  inputs:
    targetType: 'inline'
    script: |
      $orgUrl = "$(System.TeamFoundationCollectionUri)"
      $projectName = "$(System.TeamProject)"
      $pipelineId = "123" # 替换为你的经典流水线ID
      $pat = "$(PatToken)"
      $newVersion = "$(VersionCalc.NewImageVersion)"

      $base64Auth = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat"))
      $headers = @{
        Authorization = "Basic $base64Auth"
        "Content-Type" = "application/json"
      }

      # 构建触发请求体
      $triggerBody = @{
        definition = @{ id = $pipelineId }
        parameters = "`"ImageVersion`: `"$newVersion`""
      } | ConvertTo-Json

      # 发送触发请求
      $triggerApiUrl = "$orgUrl$projectName/_apis/build/builds?api-version=7.1-preview.7"
      $buildResult = Invoke-RestMethod -Uri $triggerApiUrl -Headers $headers -Method Post -Body $triggerBody

      Write-Host "流水线已成功触发,构建ID: $($buildResult.id)"

注意事项

  • 如果ImageVersion存储在变量组中,需将获取/更新逻辑替换为变量组API:
    • 获取变量组:$orgUrl$projectName/_apis/distributedtask/variablegroups/变量组ID?api-version=7.1-preview.2
    • 更新时修改变量组内的对应变量值后,PUT回该API即可
  • 版本递增逻辑可根据实际格式调整,如两位版本号1.0可修改为递增第二位$versionSegments[1]
  • 确保PatToken的权限足够,避免API调用失败

内容的提问来源于stack exchange,提问作者durga prasad boddu

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 08:30:18