【发布时间】:2022-09-23 09:56:50
【问题描述】:
我有一个包含许多工件的版本,并且每天晚上我都安排了此版本的部署。但通常只有 1-2 个工件有变化,其余的都没有。
我有 30 个工件,如果我因为更改了其中一个而想要发布,我必须发布所有工件。
如何仅部署已更改的工件,而不是全部?
标签: azure devops azure-pipelines-release-pipeline artifact
我有一个包含许多工件的版本,并且每天晚上我都安排了此版本的部署。但通常只有 1-2 个工件有变化,其余的都没有。
我有 30 个工件,如果我因为更改了其中一个而想要发布,我必须发布所有工件。
如何仅部署已更改的工件,而不是全部?
标签: azure devops azure-pipelines-release-pipeline artifact
没有直接的方法可以做到这一点。
我唯一能想到的是,您可以编写一个脚本来调用build 或pipeline run REST API 来检索先前构建或管道运行的源版本,然后将源版本与当前构建/管道运行进行比较(使用predefined variable Build.SourceVersion 用于当前构建)检查它们是否是相同的源版本(相同的commit 用于Git 或changeset 用于TFVC)。
如果它与之前的构建/管道运行具有相同的源版本,则使当前构建/管道失败,从而停止 CI/CD 进程。否则,构建工件并部署到您的环境。
更新:
以下 Yaml 供您参考:(添加 PowerShell 任务作为管道中的第一个任务,以运行脚本以比较当前源版本与最后成功构建的源版本。)
steps:
- task: PowerShell@2
displayName: Compare the source versions to fail or pass the CI build process
inputs:
targetType: 'inline'
script: |
# Get the last succeeded build source version
$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/builds?definitions=$env:SYSTEM_DEFINITIONID&resultFilter=succeeded&statusFilter=completed&api-version=6.0"
$lastbuild = (Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $(system.accesstoken)"}).value | Select-Object -First 1
$pervioussourceversion = $lastbuild.sourceVersion
Write-Host "The last successful build source version:$pervioussourceversion"
# Current build source version
$currentsourceversion = "$(Build.SourceVersion)"
Write-Host "Current build source version :$currentsourceversion"
# Compare the source versions to fail or pass the CI build process
if($currentsourceversion -eq $pervioussourceversion) {
# make pipeline to fail
Write-Host "Current source version:$currentsourceversion is the same as the last successful build:$(Build.BuildId), fail this step to stop current build pipeline."
exit 1
}
else {
# make pipeline to succeed
Write-Host "Current source version:$currentsourceversion is different from the last successful build:$(Build.BuildId) with source version:$pervioussourceversion."
Write-Host "Continue the pipeline to build a new artifact to deploy."
exit 0
}
env:
SYSTEM_ACCESSTOKEN: $(system.accesstoken)
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
# Do something here.
Write-Host "Add subsequent tasks to build your artifacts"
condition: succeeded()
更新2:PowerShell 脚本
# Get the last succeeded build source version
$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/builds?definitions=$env:SYSTEM_DEFINITIONID&resultFilter=succeeded&statusFilter=completed&api-version=6.0"
$lastbuild = (Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $(system.accesstoken)"}).value | Select-Object -First 1
$pervioussourceversion = $lastbuild.sourceVersion
Write-Host "The last successful build source version:$pervioussourceversion"
# Current build source version
$currentsourceversion = "$(Build.SourceVersion)"
Write-Host "Current build source version------------:$currentsourceversion"
# Compare the source versions to fail or pass the CI build process
if($currentsourceversion -eq $pervioussourceversion) {
# make pipeline to fail
Write-Host "Current source version:$currentsourceversion is the same as the last successful build:$(Build.BuildId), fail this step to stop current build pipeline."
exit 1
}
else {
# make pipeline to succeed
Write-Host "Current source version:$currentsourceversion is different from the last successful build:$(Build.BuildId) with source version:$pervioussourceversion."
Write-Host "Continue the pipeline to build a new artifact to deploy."
exit 0
}
【讨论】: