This week I encountered the need to commit some code to a Git repository in an Azure DevOps pipeline. First, I had no idea how to could work because the repository has some branch policies and require the committer to create a pull request.
As it turns out, if you configure your pipeline with the right permissions, committing code in the pipeline is quite simple.
First, you have to configure the permissions of the build service to bypass pull request policies and also to be allowed to commit to your repository. To do that, go to the Project Settings –> Repository –> select your repository and then click on the Security tab.
Select your Build Service and set Bypass policies when pushing Contribute to allowed.
With the permissions in place, create a new YAML pipeline. For this demo, I use this very simple one.
All this pipeline does is to set an email address and user name in the gitconfig file and then it writes a new file called data.txt to the root folder and commits this change. You can split all these tasks also into separate tasks and don’t have to do them all in the same one.
Run the pipeline and it should finish successfully.
Open your repository and you should see the data.txt file there.
This demo is very simple but still should show you everything you need to know to create commits in your pipeline yourself. I will extend this demo in a future post and will use it to change some configurations of my application for an Azure Arc deployment.
This post showed how you can create Git commits inside your Azure DevOps YAML pipeline and how to configure your Build Service to bypass pull request policies.
You can find the code of the demo on GitHub.