管道运行保留期

Azure DevOps Services |Azure DevOps Server |Azure DevOps Server 2022 |Azure DevOps Server 2020

通过创建保留租约来处理管道运行的时间长于配置的项目设置。 临时保留租约通常由自动进程创建,而更永久的租约则通过操作 UI 或发布管理系统保留制品时创建,但也可以通过REST API进行操作。 下面是可以添加到 yaml 管道以供保留的任务的一些示例。

先决条件

默认情况下,参与者、生成管理员、项目管理员和发布管理员组的成员可以管理保留策略。

示例:覆盖项目级短期保留窗口

在此示例中,项目配置为仅在 30 天后删除管道运行。

示例 1:项目设置保留策略

如果此项目中的管道很重要,并且运行应保留 30 天以上,则此任务会通过 添加新的保留租约来确保运行有效期为两年。

- task: PowerShell@2
  condition: and(succeeded(), not(canceled()))
  name: RetainOnSuccess
  displayName: Retain on Success
  inputs:
    failOnStderr: true
    targetType: 'inline'
    script: |
      $contentType = "application/json";
      $headers = @{ Authorization = 'Bearer $(System.AccessToken)' };
      $rawRequest = @{ daysValid = 365 * 2; definitionId = $(System.DefinitionId); ownerId = 'User:$(Build.RequestedForId)'; protectPipeline = $false; runId = $(Build.BuildId) };
      $request = ConvertTo-Json @($rawRequest);
      $uri = "$(System.CollectionUri)$(System.TeamProject)/_apis/build/retention/leases?api-version=6.0-preview.1";
      Invoke-RestMethod -uri $uri -method POST -Headers $headers -ContentType $contentType -Body $request;

问:能否将管道保留为 小于 配置的项目值?

不,租约不能逆向操作。 如果项目配置为保留两年,系统在此期间不会删除管道的运行记录。 若要更早删除这些运行,请手动删除它们或使用等效的 REST API。

示例:仅在名称为 releases/* 的分支上运行的内容应长时间保存。

这类似于上述情况,只需更改条件:

- task: PowerShell@2
  condition: and(succeeded(), not(canceled()), startsWith(variables['Build.SourceBranch'], 'releases/'))
  name: RetainReleaseBuildOnSuccess
  displayName: Retain Release Build on Success
  inputs:
    failOnStderr: true
    targetType: 'inline'
    script: |
      $contentType = "application/json";
      $headers = @{ Authorization = 'Bearer $(System.AccessToken)' };
      $rawRequest = @{ daysValid = 365 * 2; definitionId = $(System.DefinitionId); ownerId = 'User:$(Build.RequestedForId)'; protectPipeline = $false; runId = $(Build.BuildId) };
      $request = ConvertTo-Json @($rawRequest);
      $uri = "$(System.CollectionUri)$(System.TeamProject)/_apis/build/retention/leases?api-version=6.0-preview.1";
      Invoke-RestMethod -uri $uri -method POST -Headers $headers -ContentType $contentType -Body $request;

示例:根据阶段成功更新多阶段流水线的保留窗口

考虑一个两阶段的流水线,首先运行构建,然后运行发布。 成功后,Build 阶段将保留运行三天,但项目管理员希望在 Release 阶段成功后能将租约延长到一年。

阶段 Build 可以像上述示例一样保留管道,但添加一个附加内容:通过在输出变量中保存新租约 Id ,可以在发布阶段运行时稍后更新租约。

- task: PowerShell@2
  condition: and(succeeded(), not(canceled()))
  name: RetainOnSuccess
  displayName: Retain on Success
  inputs:
    failOnStderr: true
    targetType: 'inline'
    script: |
      $contentType = "application/json";
      $headers = @{ Authorization = 'Bearer $(System.AccessToken)' };
      $rawRequest = @{ daysValid = 365; definitionId = $(System.DefinitionId); ownerId = 'User:$(Build.RequestedForId)'; protectPipeline = $false; runId = $(Build.BuildId) };
      $request = ConvertTo-Json @($rawRequest);
      $uri = "$(System.CollectionUri)$(System.TeamProject)/_apis/build/retention/leases?api-version=6.0-preview.1";
      $newLease = Invoke-RestMethod -uri $uri -method POST -Headers $headers -ContentType $contentType -Body $request;
      $newLeaseId = $newLease.Value[0].LeaseId
      echo "##vso[task.setvariable variable=newLeaseId;isOutput=true]$newLeaseId";

若要 更新 保留租约 ,需要不同的 REST API 调用。

- stage: Release
  dependsOn: Build
  jobs:
  - job: default
    variables:
    - name: NewLeaseId
      value: $[ stageDependencies.Build.default.outputs['RetainOnSuccess.newLeaseId']]
    steps:
    - task: PowerShell@2
      condition: and(succeeded(), not(canceled()))
      name: RetainOnSuccess
      displayName: Retain on Success
      inputs:
        failOnStderr: true
        targetType: 'inline'
        script: |
          $contentType = "application/json";
          $headers = @{ Authorization = 'Bearer $(System.AccessToken)' };
          $rawRequest = @{ daysValid = 365 };
          $request = ConvertTo-Json $rawRequest;
          $uri = "$(System.CollectionUri)$(System.TeamProject)/_apis/build/retention/leases/$(newLeaseId)?api-version=7.1-preview.2";
          Invoke-RestMethod -uri $uri -method PATCH -Headers $headers -ContentType $contentType -Body $request;

后续步骤

通过这些示例,你了解了如何使用自定义管道任务来管理运行保留。

你已了解如何执行以下操作:

  • 覆盖短保留窗口
  • 覆盖特定分支上运行的保留策略
  • 当需要将运行保留更长时间时,更新保留期限。