How to Commit Files to an Azure DevOps Repository Using an Azure DevOps Pipeline

Login to your Azure DevOps account and create a pipeline

Under steps add the below task

- stage: updating_manifest
  displayName: Updating GitOps Manifest
  jobs:
  - job: build
    displayName: 'Build Image'
    steps:
    - checkout: git://Test Pipeline/kubernetes-manifest@main
      persistCredentials: true

Here the checkout URL is like

git://{ Your Project }/{Your Repository Name}@{Branch Name}

Once you added this task, now create a bash script task that runs the commit commands.

Note: Before you begin, be sure your account’s default identity is set with the following code. This must be done as the very first step after checking out your code.

- stage: updating_manifest
  displayName: Updating GitOps Manifest
  jobs:
  - job: build
    displayName: 'Build Image'
    steps:
    - checkout: git://Test Pipeline/kubernetes-manifest@main
      persistCredentials: true
    - script: |
        git config --global user.name "Your Name"
        git config --global user.email "[email protected]"
        git checkout "your branch to be commited"
        echo "Hello World!" > test.txt
        git add .
        git commit -m "updated manifest"
        git push
      displayName: 'Run a multi-line script'

Additionally, you need to perform a checkout within the Bash script. This is because Azure DevOps checks out the branch under a different name, which can lead to an error stating “branch not found” when attempting to commit to your branch.

Once we run this pipeline, it will request access to the branch. Additionally, you need to grant Contributor access to the repository for the “pipeline_build_user” that is responsible for committing the changes.

To allow contributors access

Navigate to:

-> Project Settings -> Repositories
-> Select your branch
-> Security
-> Look for a user with the name containing “Build Service,” and click on that user
-> Grant “Contribute” access to allow the necessary permissions

Azure Doc

Leave a Reply

Your email address will not be published. Required fields are marked *