Azure multi branch pipeline - azure

How do we create multi branch pipeline in azure.
Say there are 4 developers committing to the same branch, for each commit there should be a seperate build pipeline with unit tests.

for each commit there should be a seperate build pipeline with unit tests.
If you want to have a seperate build pipeline with unit tests for each commit, you need to create multi pipeline for each branch:
And if just want to one build pipeline for those multi branch, you may need to have create multi branch triggers:

I think what you need is a CI pipeline. So lets say, there is a main/master branch, 4 developers have pulled this main branch in their PC/laptop. And all 4 of them are about to work on 4 different defects/bugs say bug1,Bug2, Bug3 and Bug4
Each one of them are supposed to create a branch from main. Say bug/Bug1, bug/Bug2, bug/Bug3 and bug/Bug4. So now we got 4 branches.
What you can do now is switch on pull request branch policies in main branch. Please see below screenshot
Once you switch on this pull request policy, no code can be merged in your main branch without creating a pull request.
Now second part, you have to create a pipeline that will for example, build the code and run some tests.
Now part three: In the same branch policies page, if you scroll down a bit, you will see a setting for "Build Validation" as shown below
What you have to do here is
Press that + sign and configure the pipeline that you have created earlier here.
What this means is
When say Dev 1 whose branch is bug/Bug1 will raise a pull request to merge in main branch, this Build validation will trigger the pipeline that you have configured here.
So when all 4 Devs will raise 4 pull requests, based on their code changes, this pipeline will get triggered and validate those code changes for build + tests.
Also, based on the code review, if there are any further code changes pushed into these branches (bug/Bug1 etc.), you can configure this Build validation in such a way that this pipeline will get automatically triggered whenever new code changes are pushed into a branch which is connected to a pull request.

Related

Refer to pre-merge pipelines from post-merge pipelines

Context
I’m using CI for developing a solution using a monorepo. It has two custom python libraries, and high level orchestration scripts using these packages.
The CI is split into 3 parts: build, test and deploy.
During build, I create an image (tag being a fixed name pattern followed by pipeline id) in ECR (staging repository) with kaniko.
This "base" image is then used in test stage to run unit tests and integration tests.
These two stages run always for any merge request into default branch (main). If both of these pass, I’ll merge the development branch into main branch, and a new pipeline gets triggered post merge.
This will repeat the build and test stage, and then deploy stage starts.
Some very high level wrapper scripts are added to the "base" image created in build stage and publish it with a versioned tag (identified by a fixed name pattern followed by commit id) and also as latest tag in ECR (deploy repository).
Question
Everything works in this approach, but I don't like the fact that the build and test stages repeat between pre-merge (triggered as merge request pipeline) and post-merge (triggered as branch pipeline) pipelines. These are identical time consuming steps, and shall always be identical as we allow only fast forward merges and each merge creates a merge commit. Is there a way to handle this scenario?
Issues
The main challenge I’m facing is how to identify the "base" image created during build stage (identified by pipeline id). If I can do that, it becomes simple. But I do not know how to get that pipeline id. I have to use an identifier in image tag as a lot of people are working and there can be simultaneous pipelines by other people, but as soon as I use pipeline id or commit id, it is becoming impossible to track those after merge.

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.

How to Trigger a pull request using script or pipeline once build is successful from azure devops

I have a requirement. Whether it is possible or not I don't know
Requirement: I have three branches 1. Master, 2. Dev, 3. Test.
Here every time new code is committed to dev or test and we create a build to execute the code. Now the requirement is I don't want to merge the code to Master branches unless build is success for master. When build is successful then only PR will be triggered and code will be merged to Master Branch.
Master build ---> Success ----> Then merge code with Master Branch.
Is this possible, without merging the new code into main branch, how can we test the new code from master branch in the build. And if that build is success then only PR has to take place and merge the code in master branch.
My requirement is Once the normal build is success then only PR has to be triggered and merge the code with master branch. Can this be achievable with powershell/bash script that can trigger a PR from pipeline (Build)
I'm pretty sure what you are looking for is branch polices Build validation instead of trigger a PR from pipeline.
You may not fully understand the logic of Build validation and PR.
What you want to achieve is to protect the master and not merge the PR on the branch to the master until the build verification success.
So, the key point is to verify the code after the PR merge, instead of triggering the PR after the build is completed. Otherwise, your build will always be the code on the master before the merge, and this verification will be meaningless.
Adding the Build validation on the master branch, which will trigger the build pipeline when we create a pull request. When we create a PR, it will create a temp commit to save the merged code and build pipeline will checkout this new temp commit to build. If build successful, then we could complete the PR, otherwise, we could not complete the PR, the master code would not be updated.
That should be what you want.
Azure DevOps uses branch polices to achieve this which you can read about Here

Trigger Azure Pipeline on Merge

I have a use case where I have two pipelines configured. One for running tests which are triggered by a Pull Request and another pipeline that is handling deployment. I am using Bitbucket cloud in Azure.
Pipeline A - Run through tests on PR.
Pipeline B - Deploy upon successful merge (approved pull request).
My current setup is to trigger pipeline A when a pull request is submitted. This is no problem and works as intended. The issue I am running into is with Pipeline B. It is triggered regardless of whether the PR is approved or even merged. I know that expected behavior for the CI trigger is to fire off with any action on the branch. The PR trigger also doesn't fit my needs though as it would be redundant with Pipeline A.
What I would like to happen is since Pipeline A fires off on PR, a code reviewer will Approve and Merge the PR and only if the PR ends up getting merged into the Release branch is Pipeline B triggered. Is this possible?
Developer completes code.
Developer submits PR against a Release1.0 branch.
Pipeline A triggers. Tests pass or fail.
Code reviewer approves and merges PR into Release1.0 branch.
Pipeline B triggers and deploys.
UPDATE: For those who bump into the same issue it's actually quite a simple fix. Simply setting the trigger...
pr: none
in Pipeline B will do the trick.
To be clear, this solution is specific to Bitbucket Cloud and GitHub repositories only. The 'pr' syntax in YAML pipelines doesn't exist for Azure Repositories (use branch policy instead).

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

Resources