Azure DevOps 服務 |Azure DevOps Server |Azure DevOps Server 2022
保留管線執行的時間超過已設定的 專案設定 的時間,是透過建立 保留租約來處理。 暫時保留租用通常是由自動處理程序所建立,而更永久的租用則則會透過操作 UI 或 Release Management 保留構件來建立,但也可以透過 REST API 來操作。 以下是一些您可以新增至 yaml 管線以保留的工作範例。
先決條件
根據預設,參與者、組建管理員、專案管理員和版本管理員群組的成員都可以管理保留原則。
範例:覆蓋短期的專案層級保留期限
在此範例中,專案設定為僅在 30 天後刪除管線執行。
如果此專案中的管線很重要,且執行應該保留超過 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;
後續步驟
透過這些範例,您瞭解如何使用自訂管線工作來管理執行保持。
您已經學會如何:
- 覆寫簡短的保留期間
- 覆寫特定分支上運行資料的保留政策
- 當需要延長保留期限時,更新保留契約以保持執行紀錄更長時間