4

Create Git Commits in an Azure DevOps YAML Pipeline

 2 years ago
source link: https://www.programmingwithwolfgang.com/create-git-commits-in-azure-devops-yaml-pipeline/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
10 days ago2021-12-20T00:00:00+01:00 by Wolfgang Ofner

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.

Configure the Pipeline to bypass Pull Request Policies

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.

Set the permissions of the Build Service

Create a Pipeline to Checkout and Commit to Git Branches

With the permissions in place, create a new YAML pipeline. For this demo, I use this very simple one.

trigger: - master

pool: vmImage: ubuntu-latest

steps: - checkout: self persistCredentials: true

- script: | git config --global user.email [email protected] & git config --global user.name "Pipeline" workingDirectory: $(System.DefaultWorkingDirectory)

- script: | git checkout -b master echo 'This is a test' > data.txt git add -A git commit -m "deployment $(Build.BuildNumber)" git push --set-upstream origin master displayName: Add data.txt file workingDirectory: $(System.DefaultWorkingDirectory)

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.

The pipeline ran successfully

Open your repository and you should see the data.txt file there.

The test file got created and committed

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.

Conclusion

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK