-1

I have one release with many artifacts, and every night I have scheduled deployments of this release. But usually only 1-2 artifacts have changes, the rest of it, none.

I have like 30 artifacts, and if I want to do a realease because I have changed one of them, I must release all the artifacts.

How can I deploy only the artifacts that have been changed, and not all of them?

EduardFTW
  • 11
  • 5

1 Answers1

0

There isn't a straight forward way to do that.

The only thing I can think of is that you can write a script to call the build or pipeline run REST API to retrieve the source version of the pervious build or pipeline run, then compare the source version with current build/pipeline run (use the predefined variable Build.SourceVersion for current build) to check if they are the same source version (same commit for Git or changeset for TFVC).

If it has the same source version with previous build/pipeline run, then fail the current build/pipeline, so that the CI/CD process will be stopped. Otherwise, build the artifact and deploy to your environment.

UPDATE:

The following Yaml for your reference: (Add a PowerShell task as the first task in your pipeline to run the script to compare current source version with the last succeeded build source version.)

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()

Fail the artifact pipeline if the source version is same as previous one: enter image description here

Continue the CI build process if a new version triggered: enter image description here

UPDATE2: PowerShell 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
}
Andy Li-MSFT
  • 28,712
  • 2
  • 33
  • 55
  • Hi, ive neen trying, yet with no result, could you give me a quick example of what you explained? Would be very useful for me... – EduardFTW Sep 21 '22 at 10:42
  • @EduardFTW See the updated answer, a sample for your reference. You just need to add a powershell task as the first task in your pipeline to run the script to identify and compare the source version. If the current source version is same as pervious one, it will fail the build process, otherwise it will continue the CI build process to build a new artifact to deploy further. – Andy Li-MSFT Sep 22 '22 at 08:00
  • Thanks Andy, i copy pasted the script in a powhershell task, and i get this error: 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 expression after unary operator '-'. 2022-09-22T11:10:27.1874756Z [0m – EduardFTW Sep 22 '22 at 11:12
  • @EduardFTW Seems to be a syntax issue, please make sure you copied the exact script, and did not change anything. Just try to remove all scripts, and then copy it back again to check if it works, I have posted the script separately for you, see the updated answer. – Andy Li-MSFT Sep 23 '22 at 02:01
  • Allright, now is working, but.... i have tryed to do two deploys, of one api, ok? And always the original version with the new version are the same, and that make no sense.. because i have not changed nothing on the api. If i deploy, from the release 1 time, then the second time, if i didnt make any changes, shouldn't deploy... but it deploys, because the new and the original version, are always the same, i don't understand.. – EduardFTW Sep 23 '22 at 06:53
  • This is usefull for the build of pipelines, yah, awsome is very useful, but i think my question was bad, or you didn't understand me. I have a release, unified, with 20 apis (artefacts) if i want to deploy only one of them, i must deploy all of them, but i only want to deploy the one with changes...Anyway, lot of thanks, this is usefull also!! – EduardFTW Sep 23 '22 at 10:04