8

Is there any way to pass parameters to azure devops build pipelines (YAML) when triggered on a schedule? E.g. if you want to build a release build nightly, but also a debug build weekly for example?

It doesn't seem right to have to duplicate the whole build pipeline to SomeBuild-Debug in order to make a build that has configuration=debug as default, to be able to schedule it?

Anders Forsgren
  • 10,827
  • 4
  • 40
  • 77
  • I am also looking for a way to do this, but it does not look like this can be done without scripting or using something like Azure Logic Apps. I created a feature request here: https://developercommunity.visualstudio.com/idea/1204205/allow-setting-parameters-for-scheduled-builds.html – Steve Glick Sep 29 '20 at 19:55
  • Couldn't you use a template pipeline for this? The template pipeline would contain all the pipeline stages, jobs and steps and you could create then two pipelines that extend the template pipeline providing only the parameters each defining different default values. – whatever Dec 18 '20 at 08:14

3 Answers3

8

It's possible to detect that a build was triggered via a schedule. Based on this information, you can set certain variables or trigger certain processes.

Azure DevOps pipelines give you access to a bunch of predefined variables. In particular, Build.Reason:

The event that caused the build to run.

  • Manual: A user manually queued the build.
  • IndividualCI: Continuous integration (CI) triggered by a Git push or a TFVC check-in.
  • BatchedCI: Continuous integration (CI) triggered by a Git push or a TFVC check-in, and the Batch changes was selected.
  • Schedule: Scheduled trigger.
  • ValidateShelveset: A user manually queued the build of a specific TFVC shelveset.
  • CheckInShelveset: Gated check-in trigger.
  • PullRequest: The build was triggered by a Git branch policy that requires a build.
  • ResourceTrigger: The build was triggered by a resource trigger or it was triggered by another build.

Based on this variable, it's possible to define a custom one:

variables:
   ${{ if eq( variables['Build.Reason'], 'Schedule' ) }}: 
    myCustomVariable: 'Weekly debug'

or trigger a custom process:

- task: CmdLine@2
  condition: eq( variables['Build.Reason'], 'Schedule' ))
  displayName: 'My scheduled script'
  inputs:
    script: echo "I was launched during a scheduled build"

Credit: this answer is inspired by Kevin Lu-MSFT'sanswer on Setting parameter value dynamically for automatic pipelines

Métoule
  • 13,062
  • 2
  • 56
  • 84
2

I came across this recently and I think there is. If you have access to the azure portal you could create an Azure Logic App that triggers on a schedule, and can start a "queue a new build". It allows for a json dictionary of parameters:

enter image description here

This allows to start the build with different parameters.

GetShifting
  • 461
  • 4
  • 12
  • Unfortunately I’m on an on-premises server. I can do the same with a scheduled task in Windows I suppose but would like to know it isn’t possible (or planned) as an official function. – Anders Forsgren Jan 25 '20 at 12:31
  • I'm not sure you can start a build using windows scheduled tasks. Let me know if you find a way to do that, that might be interesting.As far as I know it's not possible as an official function. – GetShifting Jan 27 '20 at 08:13
  • I haven't tried but I assume I can shedule anything e.g. a PowerShell snippet or my own custom program to trigger the build. For example: https://stackoverflow.com/a/55474158/678410 – Anders Forsgren Jan 27 '20 at 08:36
  • That looks promising indeed. – GetShifting Jan 28 '20 at 12:28
1

You can schedule an Azure DevOps build using an external service (ansible, automated task, etc) by using a REST API. In the rest API call you can specify the input parameters. This way you will need to schedule the trigger of the build pipeline using the external service and not schedule of the .yml file.

Example of triggering a build pipeline. You should replace the characters between **

POST https://dev.azure.com/**GeralexGR**/**test-project**/_apis/pipelines/**11**/runs?&api-version=6.1-preview.1

Body section

enter image description here

Keep in mind that you will also need authentication using a PAT to use the REST API of Azure Devops.

GeralexGR
  • 2,973
  • 6
  • 24
  • 33