Custom Schedule for every build in Azure Pipelines for stages

128 views Asked by At

We are facing an issue with individual scheduling the prod stage where we have multiple deployments coming in a day and targets to same target servers. In our case the deployments are unique and not related to each other so dev-a will upload his code and trigger the pipeline manually and build the code and he wants schedule the deployment for next day non-bussiness hours. Dev-B comes up with the code and does the build and want to send the code to prod by EOD. since both the developers using the same pipeline i couldn't figure out how to make a custom schedule work in Azure Pipelines.

I have multi stage azure yaml pipeline which has Build, QA & Prod. I need a one time schedule for prod stage where I have to pass the parameters or select the time of deployment for prod stage everytime i trigger a pipeline. It will be helpful even if there are any plugins available in marketplace. Note: No cron schedule or classic triggers need something in human readable format.

Since I couldn't find any solution for above problem I am going with the below temporary solution, If anyone comes up with good suggestions That might help me a lot.

- stage: Time_Deployment
  condition: succeeded()
  displayName: '${{ parameters.DeploymentDateTime }} Wait Time'
  dependsOn: QA
  jobs:
  - deployment: Deploy
    displayName: '${{ parameters.DeploymentDateTime }} Wait Time'
    pool:
      name: $(Agent.PoolName1)
    environment: 
        name: Prod_Deploy
    timeoutInMinutes: 0
    strategy:
      runOnce:
        deploy:
          steps:
          - bash: |
              # If the DeploymentDateTime parameter is empty, use the current date and time
              if [[ -z "${{ parameters.DeploymentDateTime }}" ]]; then
                DEPLOY_TIME=$(date '+%Y-%m-%d %H:%M')
              else
                DEPLOY_TIME="${{ parameters.DeploymentDateTime }}"
              fi
              # Convert the deployment time to a Unix timestamp
              DEPLOY_TIME=$(date -d "$DEPLOY_TIME" +%s)
              CURRENT_TIME=$(date +%s)
              DELAY=$((DEPLOY_TIME - CURRENT_TIME))
              # If the calculated delay is negative, set it to 0
              if [ $DELAY -lt 0 ]; then
                DELAY=0
              fi
              echo "Deployment Time: $DEPLOY_TIME"
              echo "Current Time: $CURRENT_TIME"
              echo "Delay Time: $DELAY"
              # Set the delayTime variable
              echo "##vso[task.setvariable variable=delayTime]$DELAY"
            displayName: 'Calculate Deployment Time'
          - script: sleep $(delayTime)
            displayName: 'Wait for Deployment Time'

I am using the above script in my pipeline to delay the pipeline for certain period in the stage before Production to achieve the custom schedule but I am facing a red flag when it is delayed for more than 3 or mare days since the agent will be running for those days even though it is using the minimal resources if the Linux machine at the end of day the job is failing due to agent couldn't pickup the network after the long delay job and it is also not generating any logs after the job got failed.

1

There are 1 answers

2
Ziyang Liu-MSFT On

Based on your situation, it is recommended to create a separate pipeline for each developer and set the schedule trigger separately.

If you still want to use one pipeline, you can try to use cron scheduled trigger and use the pre-defined variable Build.CronSchedule.DisplayName in stage condition to determine whether to run that stage.

schedules:
- cron: '0 0 * * *'
  displayName: Daily midnight build
  branches:
    include:
    - main
    - releases/*
    exclude:
    - releases/ancient/*

stages:
- stage: stage1
  # Run this stage only when the pipeline is triggered by the "Daily midnight build" cron schedule
  condition: eq(variables['Build.CronSchedule.DisplayName'], 'Daily midnight build')
  jobs:
  - job: job1
    steps:
    - script: echo Hello from Stage 1 Job 1

In this example, stage1 only runs if the pipeline was triggered by the Daily midnight build schedule.

See the detailed info about schedule trigger from schedules.cron definition.