I'd like to have separate pipelines for microservices, and also staging and production. It would look like this:

I'm just starting to setup the azure-pipelines.yaml and have this as the trigger for admin:
trigger:
branches:
include:
- staging
- production
paths:
include:
- admin/*
resources:
- repo: self
The issue I'm running into, as you might see right off the bat, is that when I commit to staging it is triggering both of the pipelines.
So my question is: is there a way to have one yaml for both staging and production for this microservice or do I need to have two separate yaml files?
EDIT:
Given these two things, I think what I'm trying to do is unlikely unless I make separate adminStaging.yaml and adminProduction.yaml:
You cannot use variables in triggers, as variables are evaluated at runtime (after the trigger has fired).
If you use templates to author YAML files, then you can only specify triggers in the main YAML file for the pipeline. You cannot specify triggers in the template files.
https://learn.microsoft.com/en-us/azure/devops/pipelines/repos/azure-repos-git?view=azure-devops&tabs=yaml
I agree with @Daniel Mann, you should move toward adopting a more modern branching strategy. Gitflow or Trunk-based with release branches is typically what I use:
They each have their advantages and disadvantages, but either will cover your use-case. You'd you a non-prod branch (develop or rc/*) and a production branch (master). UAT deployments would be triggered off of the non-prod branch(es) and prod releases would be triggered from master.
However, to address your question regarding if it's possible to deploy to both UAT and Production, while having a trigger pointed to both branches, yes. You're able to set up your YAML pipelines to be able to, but it's a bit inelegant.
Effectively, what you'd want to do is use either a condition or an if statement to selectively run your stages based upon the branch I've included both in the example below:
name: Stackoverflow-Example-Multiple-Triggers
trigger:
- staging
- production
stages:
- ${{ if contains(variables['Build.SourceBranch'], 'refs/heads/staging') }}:
- stage: StageA
displayName: "Stage A"
jobs:
- job: output_message_job_a
displayName: "Output Message Job"
pool:
vmImage: "ubuntu-latest"
steps:
- powershell: echo "Hello World!"
displayName: "Output Message"
condition: contains(variables['Build.SourceBranch'], 'refs/heads/staging')
- ${{ if contains(variables['Build.SourceBranch'], 'refs/heads/production') }}:
- stage: StageB
displayName: "Stage B"
jobs:
- job: output_message_job_b
displayName: "Output Message Job"
pool:
vmImage: "ubuntu-latest"
steps:
- powershell: echo "Hello World!"
displayName: "Output Message"
condition: contains(variables['Build.SourceBranch'], 'refs/heads/production')
You'll want to note that the behavior of conditions vs if statements differ:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With