Azure DevOps Build Pipeline triggers on pull request - azure

I have a .Net project that uses the Azure DevOps pipelines. The setup is that I have a build pipeline that creates an artifact. The artifact then automatically gets published through the release pipeline. This is working perfectly.
The problem is after I turned on the policy Build Validation, pull requests now triggers the build pipeline which then triggers the release pipeline. So every pull requests gets published. The build step is correct, but the release should not happen. The pre-deployment trigger "Pull request deployment" is disabled.
What I did to try to solve this is that I added a condition to the build step where the artifact gets created. So pull requests does not create artifacts, while merges does. This also works as intended. However, the release pipe still gets triggered, but this time without an artifact (which fails the pipe).
TLDR:
Release pipe triggers on pull requests, settings for this behavior is off. WTD?
My CI/CD settings:

Your release triggers on any of your builds and branches (PR also has a branch). You have to add the branch filter: Continuous deployment triggers. Restrict your filter with the master branch or any other. Also, you can define 2 build definitions:
A pipeline to validate your pull requests without linked releases.
CI pipeline that triggers a release.
Additionally, I think, this is a bug. Because the PR trigger is not enabled. Let's check dev community comments: https://developercommunity.visualstudio.com/content/problem/1292039/release-pipelines-ignore-pull-request-settings.html

What we have here is that we've set up build pipelines tied to YAML files stored in the repository, together with source code. And release pipelines have their Source set up to each of the build pipelines.
This is part of the Master Build:
trigger:
batch: false
branches:
include:
- master
And this is part of the Pull Request Build:
trigger: none
pr:
- master
We have Release pipelines for each of the Source builds, having Pull Request triggers enabled in one of them only, but you can have only one for your master artifacts, so PRs won't be published.

Related

Azure Devops pipeline triggering twice with Build Validation

I have created a pipeline in my repository which is used to validate code by executing unit tests for code that is being pushed to features/* branches. The same pipeline is used as Build validation pipeline set as Branch Policy on the develop branch to validate incoming PRs. This is the trigger of the pipeline.
# pipeline.yml
trigger:
batch: false
branches:
include:
- features/*
However we have come across the following condition: Given an open PR from refs/heads/features/azure-pipelines -> refs/heads/develop we push a commit on the features/azure-pipelines branch.
This causes the pipeline to trigger twice. To my understanding one of the runs is due to the trigger of the pipeline (The one marked as Individual CI on the screenshot) and the second run is due to branch policy trying to validate code being pushed onto the open PR to develop. (The PR Automated)
Is there any way to disable one of the executions since it's essentially a duplicate? I was maybe looking for a way to retrieve open PRs and abort execution of a pipeline for Individual CI if there is an open PR for a branch but I am not sure that's the best way around that and I am looking for options.
You can set
trigger: none
This way only the branch policy will trigger the pipeline.
Is there any way to disable one of the executions since it's essentially a duplicate?
As we know, we could not disable the Build validation pipeline set as Branch Policy on the develop branch to validate incoming PRs unless we cancel the Build validation.
For your situation, you could try to include [skip ci] in the commit message or description of the HEAD commit to make the Azure Pipelines skip running CI when you plan to merge the features branch to the develop branch.
You could check the document Skipping CI for individual commits for some more details.
Here it depends if they does the same. You can have conditional checks in the pipeline which does a different things for PR and CI runs. However, I'm pretty sure that this is not possible, because one is defined on the YAML and the second on the Azure DevOps portal. So even if you disnle PR trigger here in YAML, a branch policy still runs a PR. And you can specify antyhing in YAML to block branch policy.

Azure Devops - How to force a CI to be triggered on new branch, or work with Release branches?

It used to be the case that Azure DevOps would run a new CI build when you pushed a new branch to Origin. My company relied on this, as we would issue releases by creating a release/* branch off of develop which would trigger a build and a subsequent release into our testing environment. If everything passed UAT/QA, we would deploy the that same build into production.
Our Build Pipelines are both Classic UI and have two Branch filters, develop and release/*. The one product in question has two build pipelines - one for the Webjob and one for the API, and as such each pipeline has a Path filter (to, or not to, include the Webjob folder). Creating a release/* branch does not trigger either CI pipeline.
Our Release Pipeline looks like the following, where DEV is triggerd on artifacts produced from develop and TEST->PROD is triggered on artifacts built from release/* -
This allows us to have a release environment where we iteratively make changes to get it ready, while we simultaneously make changes to develop for the next release. This is a common Gitflow model.
The Problem
Now that Azure no longer triggers a build on branch creation, I'm forced to manually run a build on Release in order to trigger the build and subsequent TEST deployment depicted in the image above.
What can I do to have automated builds and deployments in Azure while maintaining Gitflow?
I was also stuck not knowing why my TEST build wouldn't trigger. This post helped explain why. More searching found the way to resolve it. Simple as removing any path checks from your TEST build.
From the linked documentation:
If your pipeline has path filters, it will be triggered only if the new branch has changes to files that match that path filter.
If your pipeline does not have path filters, it will be triggered even if there are no changes in the new branch.
See:
Behavior of triggers when new branches are created
Not sure if you are using YAML or not but using the GUI you can add a 'path' filter for Continuous Integration and set the build to run anytime a path contains 'release'.
YAML pipelines are configured by default with a CI trigger on all branches.
YAML pipelines are configured by default with a CI trigger on all branches.
When defining pipelines using YAML and defining a repository resource (repo for shorthand). A new pipeline run gets triggered when a commit has occurred. You can further configure the trigger to ONLY act on certain branches.
See:
CI triggers - MSDN
Repo triggers
Example (All branches by default):
resources:
- repo: self
clean: true
pool:
vmImage: 'ubuntu-20.04'
steps:
- task: CMake#1
displayName: 'Generate Makefile'
inputs:
workingDirectory: $(Build.SourcesDirectory)
cmakeArgs: '-B$(Agent.BuildDirectory) -H.'
- task: Bash#3
displayName: 'Build project'
inputs:
workingDirectory: $(Agent.BuildDirectory)
targetType: 'inline'
script: 'make -j4'
Limit to release branches (add trigger to repository resource):
trigger: # Optional; Triggers are enabled by default
branches: # branch conditions to filter the events, optional; Defaults to all branches.
include:
- release/*

What is the purpose of `Build branch filters` in Continuous deployment trigger?

A build pipeline can be tied to only 1 source branch.
In release pipeline, we configure an artifact by selecting the source build pipeline. So a release artifact can be tied to only 1 build pipeline.
What is the purpose of Build branch filters in Continuous deployment trigger?
Let say that your code base was updated. You want deploy only if build was made over specific branch (example develop).
In that scenario Build Branch Filter looks fairly redundant... but what if:
You want to trigger new deployment for every latest artifact, built from each feature branch to get them tested. You have to filter for feature/*.
You want to trigger new deployments if your release branches are updated, but some of them are deprecated and you need to filter them out. I that scenario specify one include filter release/* and second exclude filter release/old*.
Anyway.. in most of the cases the filter and the branch name (in artifact) will match each other. Still sometimes it can be heady to trigger deployment from multiple branches or filter something out.
In regards to your comment, I uploaded part of the yaml build. In fact one build can create artifacts from all branches in repository if you want.
trigger:
branches:
include:
- feature/*
- bugfix/*
- release/*
- develop
- master
exclude:
- experimental/*

What is the difference between Continuous deployment trigger and Pull request trigger?

Azure DevOps Release pipeline artifact settings has 2 options:
Continuous deployment trigger - Enabling the trigger will create a new release every time a new build is available.
Pull request trigger - Enabling this will create a release every time a selected artifact is available as part of a pull request workflow
I am trying to understand what is the difference between these options with respect to the highlighted parts and whether the build validation policy causes release to trigger with both options, if so then why do we have pull request trigger?
In my opinion, these two triggers have different working scope.
Assuming we set one CI build as release artifact, according to my test:
1.Continuous deployment trigger:
Whenever we have a new version of the Build, it triggers the release. It means that no matter the build pipeline is triggered by manual run, CI trigger or build validation in branch policy, the release is triggered when there's one newer build.
2.Pull request trigger:
It has a smaller scope, it will be trigger by the build pipeline which is trigger by build validation in branch policy.
(Which is triggered by PR, so if we create new PR=>It triggers PR build=>It triggers PR release)
Feel free to correct me if I misunderstand anything.
Update1:
Here's one pic about my two tests:
Release-8 is triggered by my manual running Build pipeline with only CD triggered enabled. And Release-7 is triggered by PR build with only PR triggered enabled. (I only enable the Pull Request for deployment in Stage 1)
Apart from working scope, these two triggers also have a little difference here. The Pull Request Deployment in stage for now is only for PR trigger in Artifact.
This explains that very well (taken from documentation):
Pull requests (PRs) provide an effective way to have code reviewed
before it is merged to the codebase. However, certain issues can be
tricky to find until the code is built and deployed to an environment.
Before the introduction of pull request release triggers, when a PR
was raised, you could trigger a build, but not a deployment. Pull
request triggers enable you to create pull request releases that
deploy your PR code or PR builds to detect deployment issues before
the code changes are merged. You can use pull request triggers with
code hosted on Azure Repos or GitHub.
New build basically means that your pipeline was executed.
Creating pull request trigger you need to define an artifact which will be later deployed. For this kind of trigger Azure Devops runs your pipeline and produce artifact according to your pipeline/build definition and later use this artifact for deployment.
Both trigger are similar, the difference is when your code will be deployed before or after being merged into main branch.
PR trigger is part of CI - applies when your merge your branch to master
CD trigger - applies only to the master branch

When the "pull request trigger" on the build artifact is disabled, Why is build due to a PR (build validation policy) triggering a release?

In my release pipeline I have configured build artifact. Enabled the 'continuous deployment trigger' (not added any branch filter) and disabled 'Pull request trigger'. Now when I raise PR, then the PR triggers a build (since I also have build validation configured in branch policy).
Once build completes, it triggers a release. Why does this happen when I have the "pull request trigger" disabled? Why is build due to a PR triggering a release when the "pull request trigger" on the build artifact is disabled?
The triggering of release prior to PR completion is prevented only when I add a branch filter to the "continuous deployment trigger".
Here I'll restore your scene and add some screenshots to illustrate it.
First we need to understand the definition of Continuous deployment triggers:
This instructs Azure Pipelines to create new releases automatically
when it detects new artifacts are available.
Because you set the pr build vaildation policy, when a pr is created, it will automatically trigger a pr build.
At this time, this pr build is equivalent to generating a new available artifact, which will automatically trigger the release.
This release is actually caused by Continuous deployment trigger, not because of Pull request trigger.
This the expected behavior, if you configured CD to any branch so after build is finished the release is started, no matter if the build is queued by Pull Request.
If you want to upload artifacts in part of the PR you should disable the CD trigger or use the artifacts filter.
Please read here and here the PR trigger docs.

Resources