Search code examples
azure-devopsyamlazure-pipelines

Pass parameters to Azure DevOps tasks depending on the trigger with YAML


I have a YAML Pipeline in Azure DevOps that uses different triggers:

  • Continuous Integration
  • PR Completion

This pipeline uses VsTest to execute UnitTests, and i would like to have different parameters depending on how the pipeline was triggered, i.e:

  • Enable code coverage and run only on impacted tests if the pipeline is triggered for PR completion
  • Run all the tests if this is for continuous integration

Is there any environment variable i could use to use to affect different values to my variables (both are Booleans).

I couldn't find any environment variables in Microsoft documentation that refers to how the pipeline was triggered.

I could use two different pipelines with different YAML configurations, but that's not ideal.

Thanks for your time and your help!


Solution

  • You can find here the env variable Build.Reason:

    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.

    BuildCompletion: The build was triggered by another build

    ResourceTrigger: The build was triggered by a resource trigger or it was triggered by another build.

    So in your steps add the corresponding condition, for example:

    and(succeeded(), eq(variables['Build.Reason'], 'IndividualCI'))