An Azure pipeline which uses a YAML file for its configuration, and an Azure Repos Git repository
The pipeline's trigger has been set to 'none', since it will be triggered by Build Validation branch policies (ie. when a Pull Request is created)
The pipeline's YAML configuration file has 6 jobs, one per environment, each job is based on a template (see below)
The template YAML file includes a single job, with several steps
# Triggered by Azure DevOps Build Validation branch policies (configured per branch)
trigger: none
jobs:
- ${{ if eq(variables['System.PullRequest.TargetBranch'], 'refs/heads/dev') }}:
- template: ../templates/deployment-job-template.yml
parameters:
environment: 'Dev'
- ${{ if eq(variables['System.PullRequest.TargetBranch'], 'refs/heads/sit') }}:
- template: ../templates/deployment-job-template.yml
parameters:
environment: 'SIT'
- ${{ if eq(variables['System.PullRequest.TargetBranch'], 'refs/heads/uat') }}:
- template: ../templates/deployment-job-template.yml
parameters:
environment: 'UAT'
...
Since the configuration file includes a job per environment, I only ever need one job to be executed at a time - based on the target branch of the pull request that triggered the pipeline. As such, I've added this "if" expression before every job:
${{ if eq(variables['System.PullRequest.TargetBranch'], 'refs/heads/xxx') }}:
I receive the following error when the branch policy triggers the pipeline:
The pipeline must contain at least one job with no dependencies
In my circumstances, I can't have a job with no dependencies/conditions - because only one of the six jobs will need to be executed at a time (depending on the pull request's target branch).
How can I avoid this error while ensuring that only the relevant job gets executed when the pipeline is triggered?
Ideally the solution would also prevent every job (for all environments) from appearing in the build results page (screenshot above). But not sure if this can be achieved given that I have a single YAML configuration file for the pipeline.
Any help would be much appreciated.
I looked up the Microsoft documentation and I found this.
When you define multiple jobs in a single stage, you can specify dependencies between them. Pipelines must contain at least one job with no dependencies.
The error shown by DevOps is also in line with this GitHub change. Indeed you said you can't have a job with no dependencies/conditions, but Azure DevOps needs at least one job without any dependencies.
I see that the environment name has also the same value as the branch name specified in the condition of your if
statements. I think you can simplify the whole logic by passing the System.PullRequest.TargetBranchName predefined variable to your environment
parameter. You won't then need 3 if
statements and you can probably solve your issue.
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