【问题标题】:How can i release only artifacts that have changed in Azure DevOps?如何仅发布 Azure DevOps 中已更改的工件?
【发布时间】:2022-09-23 09:56:50
【问题描述】:

我有一个包含许多工件的版本,并且每天晚上我都安排了此版本的部署。但通常只有 1-2 个工件有变化,其余的都没有。

我有 30 个工件,如果我因为更改了其中一个而想要发布,我必须发布所有工件。

如何仅部署已更改的工件,而不是全部?

    标签: azure devops azure-pipelines-release-pipeline artifact


    【解决方案1】:

    没有直接的方法可以做到这一点。

    我唯一能想到的是,您可以编写一个脚本来调用buildpipeline 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()
    

    如果源版本与前一个版本相同,则工件管道失败:

    如果触发了新版本,则继续 CI 构建过程:

    更新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
    }
    

    【讨论】:

    • 嗨,我一直在尝试,没有结果,你能给我一个你解释的简单例子吗?对我很有用...
    • @EduardFTW 查看更新后的答案,一个示例供您参考。您只需添加一个 powershell 任务作为管道中的第一个任务,即可运行脚本来识别和比较源版本。如果当前源版本与之前的版本相同,则构建过程将失败,否则它将继续 CI 构建过程以构建新的工件以进一步部署。
    • 谢谢安迪,我将脚本复制粘贴到 powhershell 任务中,我收到此错误:2022-09-22T11:10:27.1871126Z [91mParserError: [0m/home/vsts/work/_temp/c3342a5c-aadf-4c75-94b5 -81d1d8b64798.ps1:3 2022-09-22T11:10:27.1872306Z [96mLine | 2022-09-22T11:10:27.1872966Z [96m 3 | [0m -[96m [0mtask: PowerShell@2 2022-09-22T11:10:27.1873529Z [96m | [91m ~ 2022-09-22T11:10:27.1874150Z [91m[96m | [91mMissing 一元运算符“-”后的表达式。 2022-09-22T11:10:27.1874756Z [0m
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    • 1970-01-01
    • 2021-03-27
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多