Search code examples
azureazure-devopsyamlazure-pipelines-yaml

Deploying the stage based on schedule


I am trying to deploy one of the stages based on an schedule. I have defined the cron job in the azure-pipelines.yml file.

I have 5 stages.

  1. Build
  2. Deployment to Test - Country A
  3. Deployment to Test - Country B
  • I want both of those stages being deployed after approval on monday till thursday at night. It must not be recurring only after approval.
  1. Deployment to Prod - Country A
  2. Deployment to Prod - Country B The same for those two stages.
schedules:
  - cron: '31 12 * 1-12 1-5'
    displayName: WeekDays
    branches:
      include:
      - AddStage
    always: true
  - cron: '13 20 * 1-12 5-6'
    displayName: Weekend
    branches:
      include:
      - releases/*
    always: true

I set the condition for the country stages, but unfortunately I am not able to achieve what I want:

condition: eq(variables['Build.CronSchedule.DisplayName'], 'WeekDays')

Solution

  • Based on your description, I generated the sample pipeline below. It contained 5 stages with 4 stages including a deployment job to run against 4 pipeline environments.

    I added the Approvals checks for each environment and different conditions for each stage.

    trigger: none
    
    schedules:
      - cron: '56 07 * 1-12 1-5' # To be triggered by the tests in my time zone
        displayName: WeekDays
        branches:
          include:
          - AddStage
        always: true
      - cron: '13 20 * 1-12 5-6'
        displayName: Weekend
        branches:
          include:
          - releases/*
        always: true
    
    stages:
    - stage: StageBuild
      jobs:
      - job: BuildJob
        steps:
        - publish: $(System.DefaultWorkingDirectory)
          artifact: drop
    - stage: StageTestA
      condition: and( succeeded(), eq(variables['Build.CronSchedule.DisplayName'], 'WeekDays') )
      dependsOn: StageBuild
      jobs:
      - deployment: DeploymentTestCountryA
        environment: Env-Test-CountryA
        strategy:
          runOnce:
            deploy:
              steps:
              - script: |
                  echo System.StageName is $(System.StageName)
    - stage: StageTestB
      condition: and( succeeded(), eq(variables['Build.CronSchedule.DisplayName'], 'WeekDays') )
      dependsOn: StageBuild
      jobs:
      - deployment: DeploymentTestCountryB
        environment: Env-Test-CountryB
        strategy:
          runOnce:
            deploy:
              steps:
              - script: |
                  echo System.StageName is $(System.StageName)
    - stage: StageProdA
      condition: and( succeeded(), eq(variables['Build.CronSchedule.DisplayName'], 'Weekend') )
      dependsOn: StageBuild
      jobs:
      - deployment: DeploymentProdCountryA
        environment: Env-Prod-CountryA
        strategy:
          runOnce:
            deploy:
              steps:
              - script: |
                  echo System.StageName is $(System.StageName)
    - stage: StageProdB
      condition: and( succeeded(), eq(variables['Build.CronSchedule.DisplayName'], 'Weekend') )
      dependsOn: StageBuild
      jobs:
      - deployment: DeploymentProdCountryB
        environment: Env-Prod-CountryB
        strategy:
          runOnce:
            deploy:
              steps:
              - script: |
                  echo System.StageName is $(System.StageName)
    

    enter image description here

    Please note that to have the schedule trigger work for a certain branch (AddStage), we need to check the pipeline reference .yml file on that branch and make sure the branch name is included. enter image description here enter image description here

    When the pipeline is triggered by the cron schedule Weekdays, the stages with the condition eq(variables['Build.CronSchedule.DisplayName'], 'WeekDays') will ask for approvals to continue, while the two stages with the condition eq(variables['Build.CronSchedule.DisplayName'], 'Weekend') will be skipped, enter image description here

    By the way, pipeline environment supports Business hours checks as well, which you may consider to add into your environment checks.

    If this is not your specific scenario, please share a simple structure of your YAML pipeline and additional details to help understand your requirements. Hope the information helps.