Konzy262
Konzy262

Reputation: 3097

How do you delay and schedule a stage to only run the latest build in an Azure Devops yaml pipeline?

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...

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.

Upvotes: 2

Views: 2977

Answers (2)

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35194

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.

Upvotes: 3

Max Morrow
Max Morrow

Reputation: 1336

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.

Upvotes: 1

Related Questions