Logo Cinquin Andy Signature

How to block branch naming in Azure DevOps

Développeur Freelance - Logo

The sites and the associated resources.

How to block branch naming in Azure DevOps

Posted on May 27, 2024 -  by Andy Cinquin

Azure DevOpsYAML pipelineBranchesOrganizationNaming conventionProject ManagementSoftware developmentAgile methodology

In this article, we'll look at how to block branch naming on Azure DevOps using a YAML pipeline. The aim is to ensure that branches respect a specific naming convention, thus keeping the organization of our project clear and consistent.

Prerequisites

Before getting started, let's make sure we have the following:
  • An Azure DevOps account
  • An Azure DevOps project
  • Basic knowledge of YAML and Azure DevOps pipelines

Getting started

Here's how I set up branch naming validation in our Azure DevOps pipeline.

1. Create a YAML file

I started by creating a new YAML file in our Azure DevOps project. I named it branch-naming-validation.yml, but you can choose any name you like.

2. Defining the trigger and branches

In the YAML file, I've defined the trigger and the branches for which we want to apply naming validation:
trigger: - dev - main pr: branches: include: - dev - main
Here, we've defined the trigger for the dev and main branches, as well as the pull requests targeting these branches.

3. Configuring the agent pool

Next, I configured the agent pool on which the pipeline will be executed:
pool: vmImage: ubuntu-latest
In this example, an Ubuntu image is used as the agent.

4. Add validation steps

Now, here's how I added the steps to validate branch naming:
steps: - bash: | echo "BUILD_SOURCEBRANCH: $BUILD_SOURCEBRANCH" echo "SYSTEM_PULLREQUEST_SOURCEBRANCH: $SYSTEM_PULLREQUEST_SOURCEBRANCH" if [[ "$BUILD_SOURCEBRANCH" =~ ^refs/pull/[0-9]+/merge$ ]]; then echo "Pull request detected. Extracting source branch name..." branch=$(echo "$SYSTEM_PULLREQUEST_SOURCEBRANCH" | sed 's/^refs\/heads\///') else echo "Regular branch detected. Extracting branch name..." branch=$(echo "$BUILD_SOURCEBRANCH" | sed 's/^refs\/heads\///') fi echo "Extracted branch name: $branch" echo "Checking if branch name matches the allowed patterns..." if [[ "$branch" =~ ^(main|dev|[a-zA-Z0-9]+/#[0-9]+-(.*))$ ]]; then echo "Valid branch name: $branch" else echo "Invalid branch name: $branch" echo "Branch name does not match the allowed patterns." echo "Allowed patterns:" echo " - main" echo " - dev" echo " - [folder]/[#ticketnumber]-[*]" exit 1 fi displayName: 'Branch name validation'
This step uses a Bash script to extract the branch name from the BUILD_SOURCEBRANCH and SYSTEM_PULLREQUEST_SOURCEBRANCH environment variables. Next, we use a regular expression to check whether the branch name matches the authorized templates.
The authorized templates we've defined are :
  • main
  • dev
  • [folder]/[#ticketnumber]-[*]
If the branch name does not match one of these templates, the pipeline will fail with an error message indicating which templates are allowed.

5. Add additional steps (optional)

I've also added other steps to our pipeline to perform additional tasks, such as installing Node.js, running tests, checking Prettier, linting and building the application :
- task: NodeTool@0 inputs: versionSpec: '20.x' displayName: 'Install Node.js' - script: | npm install --legacy-peer-deps displayName: 'npm install' - script: | npm run prettier:check displayName: 'Run Prettier check' - script: | npm run lint displayName: 'Run linting' - script: | npm run build displayName: 'Build the application'
These steps are specific to our Node.js project, but we can adapt them to suit the needs of our project.

Conclusion

That's how I set up an Azure DevOps pipeline that validates branch naming and makes sure they comply with our specific naming convention. This helps us maintain a clear and consistent organization in our project, facilitating collaboration and branch management.
Let's not forget to adapt the branch naming templates to suit our team's or company's conventions. We can also add additional steps to the pipeline to meet the specific needs of our project.
By implementing this branch naming validation in our Azure DevOps pipeline, we can automate the application of naming conventions and maintain a clean, organized branch structure.



Thank you for your visit, feel free to contact me for any information, quote or collaboration proposal. I will be happy to answer you as soon as possible.
Did you like this article? Feel free to share it!

DEVELOP YOUR PROJECTS TOGETHER

An idea, a project? I'm here to answer your questions and help you.
I'd be delighted to discuss your project with you!