I have a pipeline that needs to run on a schedule. The pipeline currently has variables for each environment, that look like this:
variables:
- ${{ if eq(parameters.envName, 'Test') }}:
- name: subscription
value: testsub
- name: storage_account
value: testsa
- name: other_resource_branch
value: test
- ${{ if eq(parameters.envName, 'Staging') }}:
- name: subscription
value: stagingsub
- name: storage_account
value: stagingsa
- name: other_resource_branch
value: staging
- ${{ if eq(parameters.envName, 'Prod') }}:
- name: subscription
value: prodsub
- name: storage_account
value: prodsa
- name: other_resource_branch
value: prod
I would like to make schedule for each of these environments, something like this, which would make the pipeline run on a schedule on Monday, Tuesday, and Wednesday per environment:
schedules:
- cron: '0 0 * * 1'
displayName: Test schedule
branches:
include:
- master
- cron: '0 0 * * 2'
displayName: Staging schedule
branches:
include:
- master
- cron: '0 0 * * 3'
displayName: Prod schedule
branches:
include:
- master
Is there a way to correlate each of these schedules to corresponding variables? As far as I could see in the ADO documentation, there is no official way, but is there maybe a workaround?
I am afraid that there is no out-of-box variables can distinguish three schedule triggers.
As far as I could see in the ADO documentation, there is no official way, but is there maybe a workaround?
Yes. You can refer to the following workarounds:
1.You can split the Pipeline into 3 separate pipelines based on schedule trigger date.
In this case, you can set the corresponding variables for each pipeline.
When the Pipelines are triggered by the corresponding schedule trigger, the variables will be set as expected.
2.If you need to keep the environments in the same pipeline, you can add an additional stage to check the schedule trigger date.
Then we can set the Pipeline variable and pass it to the next stages to confirm which environment should be run.
Here is an example:
schedules:
- cron: '0 0 * * 1'
displayName: Test schedule
branches:
include:
- master
- cron: '0 0 * * 2'
displayName: Staging schedule
branches:
include:
- master
- cron: '0 0 * * 3'
displayName: Prod schedule
branches:
include:
- master
stages:
- stage: CheckDate
jobs:
- job: CheckDate
pool:
vmImage: windows-latest
steps:
- powershell: |
$Time = Get-Date
$Dayofweek = $Time.ToUniversalTime().DayOfWeek
echo $Dayofweek
#noted the time here is UTC time, please change the time value accordingly
if($Dayofweek -eq "Wednesday"){
echo "##vso[task.setvariable variable=envName;isOutput=true]Prod"
}
if($Dayofweek -eq "Monday"){
echo "##vso[task.setvariable variable=envName;isOutput=true]Test"
}
if($Dayofweek -eq "Tuesday"){
echo "##vso[task.setvariable variable=envName;isOutput=true]Staging"
}
name: CheckDate
- script: echo $(CheckDate.envName)
- stage: RunPipeline
variables:
- name: 'envName'
value: $[stageDependencies.CheckDate.CheckDate.outputs['CheckDate.envName']]
jobs:
- job: Prod
condition: eq(variables['envName'], 'Prod')
variables:
- name: subscription
value: testsub
- name: storage_account
value: testsa
- name: other_resource_branch
value: test
steps:
- script: echo $(envName)
- job: Test
condition: eq(variables['envName'], 'Test')
variables:
- name: subscription
value: testsub
- name: storage_account
value: testsa
- name: other_resource_branch
value: test
steps:
- script: echo $(envName)
- job: Staging
condition: eq(variables['envName'], 'Staging')
variables:
- name: subscription
value: testsub
- name: storage_account
value: testsa
- name: other_resource_branch
value: test
steps:
- script: echo $(envName)
Result: Today is Wednesday.
For more detailed info, you can refer to the doc about Cross Stages variables