Pass different parameter value to Azure DevOps Pipeline based on schedule

Viewed 5084

I have a long-running DevOps pipeline that sets up a complex environment every morning.

It has a parameter; let's call it "Version."

I need to schedule the pipeline to run three times each morning with Version values 1, 2, and 3 automatically.

Looking through the triggers, neither a schedule trigger nor a pipeline trigger seems to allow to pass a parameter value.

Is there a way to do that? Important is that they run independently of each other. Each execution takes between 30 and 60 minutes. So running them in a loop one after each other is not an option.


This is what my YAML code currently looks like:

trigger: none
pr: none
schedules:
  - cron: 0,5,10 12 * * mon,tue,wed,fri
    displayName: Scheduled most mornings
    branches:
      include:
      - CIBranch
    always: true

parameters:
- name: Version
  type: string
  default: '3'
3 Answers

If you want to run three times the same build changing just a parameter, you shoudl consider moving main steps to template. It may look like that:

#template.yaml
parameters:
- name: 'Versions'
  type: object
  default: {}
- name: 'server'
  type: string
  default: ''

steps:
- ${{ each version in parameters.Versions }}:
  - script: echo ${{ parameters.server }}:${{ version }}
  - script: echo ${{ parameters.server }}:${{ version }}
  - script: echo ${{ parameters.server }}:${{ version }}
  - script: echo ${{ parameters.server }}:${{ version }}
  - script: echo ${{ parameters.server }}:${{ version }}
  - script: echo ${{ parameters.server }}:${{ version }}

Build definition:

trigger: none
pr: none
schedules:
- cron: 0,5,10 12 * * mon,tue,wed,fri
  displayName: Scheduled most mornings
  branches:
    include:
    - master
  always: true

pool:
  vmImage: 'ubuntu-latest'

steps:
- template: template.yaml
  parameters:
    Versions: 
    - "1"
    - "2"
    - "3"
    server: someServer

While passing different parameters seems not possible, there is a way to achieve this.

In the yaml file, use the strategy section like this:

pool:
  vmImage: 'vs2017-win2016'

strategy:
  matrix:
    Run1:
      myvar: 12
    Run2:
      myvar: 14
    Run3:
      myvar: 16

This will create three "runs" setting myvar as an environment variable.

See Azure DevOps / Azure Pipelines / Pipeline basics / Jobs & stages / Define container jobs # Multiple Jobs for details. (Note: While this documentation talks about containers, this works without containers as well.)

Pass different parameter value to Azure DevOps Pipeline based on schedule

I am afraid there is no such out of box way to achieve this.

As workaround, you could try to use Azure DevOps counter to set the version from 1-3 for each scheduled build, like:

variables:
  internalVersion: 1
  semanticVersion: $[counter(variables['internalVersion'], 1)]

Then we could create powershell scripts to determine whether the counter value is equal to 3, if so, call the REST API Definitions - Reset Counter to reset the counter variable Value and return the seed.

Hope this helps.

Related