2

How do you schedule a stage to run at a particular time of day in a multi stage azure devops pipeline but for only the latest build?

For example, let's say I have a combined build and release pipeline...

  • I do 4 check-ins that day
  • 4 pipelines, each with a build and release stage are created (1 for each check-in). The build phases complete as the code is merged
  • I don't want to deploy after every check-in so I schedule the release stage to run overnight

Once it reaches the release scheduled time isn't it going to kick off 4 simultaneous deployments for each pipeline that was created that day? How would I ensure it only schedules the latest? What if I wanted to ensure it also runs that deployment stage even if there have been no code changes?

I can solve this requirement with classic releases by scheduling the release to be created and deployed at a certain time with the latest artifact. It then doesn't matter how many builds were run that day.

Konzy262
  • 2,747
  • 6
  • 42
  • 71

2 Answers2

3

How do you schedule a stage to run at a particular time of day in a multi stage azure devops pipeline but for only the latest build?

Based on your requirement, I suggest that you could use the cron parameters(Scheduled triggers) to set the deployment. Then it could schedule a stage to run at a particular time of day.

How would I ensure it only schedules the latest

To use the latest artifacts , you could add the [download build/pipeline artifacts task] 2 in your deployment stage and set the version as latest.

At the same time, you could use condition to determine the stage to run.

Here is my example:

schedules:
- cron: "0 0 * * *"
  displayName: Daily midnight build
  branches:
    include:
    - main
 
pool:
  vmImage: ubuntu-latest

stages:
- stage: A
  condition: eq(variables['Build.Reason'], 'IndividualCI')
  jobs:
  - job: A1
    steps:
      - task: PublishBuildArtifacts@1
        inputs:
          PathtoPublish: '$(Build.Sourcesdirectory)'
          ArtifactName: 'drop'
          publishLocation: 'Container'


- stage: B
  condition: eq(variables['Build.Reason'], 'Schedule')
  jobs:
  - job: B1
    steps:
      - task: DownloadBuildArtifacts@0
        inputs:
          buildType: 'specific'
          project: 'Googletest'
          pipeline: '507'
          buildVersionToDownload: 'latest'
          downloadType: 'single'
          artifactName: 'drop'
          downloadPath: '$(System.ArtifactsDirectory)'

Result:

When you checkin changes and trigger the build , it will run the first stage(build).

When the pipeline is triggered by Schedule , it will run the second stage(deployment).

enter image description here

The deployment stage will download the latest artifacts.It then doesn't matter how many builds were run that day.

Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • 1
    Thanks for this. It did help although I've now run into the issue of not being able to manually deploy a stage after the pipeline has been created. I believe it's explained here - https://developercommunity.visualstudio.com/t/specify-manual-stages-in-multi-stage-yaml-pipeline/629260 – Konzy262 Mar 18 '21 at 18:17
  • Yes. You are right. This is a feature request. If the answer could give you some help, you may consider accepting it . Thank you. – Kevin Lu-MSFT Mar 19 '21 at 01:15
1

I'd recommend breaking out your pipelines into two distinct processes:

  1. Build Verification: Have this run with every check-in to verify the integrity of your code.
  2. Nightly Deployments: Run a full build & deployment on a schedule every night.

This takes the complexity out of the scenario, while still offering you build verification at check-in.

Max Morrow
  • 1,206
  • 4
  • 13
  • Do you think those could be combined in the same yaml file? – Konzy262 Mar 10 '21 at 21:46
  • Not really. The only method that might work is using **separate branches for your nightly releases vs your build verification. In this case, you could use the **condition:** attribute to restrict which branches you'd build verification is triggered from. You'd include both a branch trigger and a schedule in this case. This would allow build verification to only proceed, while your nightly builds are released from a separate branch. I'd not recommend this though, two separate pipelines would be the cleanest way to accomplish what you're looking for. – Max Morrow Mar 11 '21 at 01:23