Samit Kumar Patel
Samit Kumar Patel

Reputation: 2098

git commit and git tag in azure devops yml based pipeline

Is there a nice way to commit the changes file and create a tag in azure DevOps yaml based pipeline?

My scenario will be for a node js based build: Each build, it will change the package.json version by using npm version patch In the end, It will push the package.json to the build branch (Obviously with condition branch==master) and will tag and push a branch as well.

The dirty way can be:

- bash : |
     git add filename.ext
     git push origin HEAD:branchName
     git tag -a tagName -m 'tag message' 
     git push --tags
  displayName: 'Git Commit and Tag from pipeline'

Upvotes: 10

Views: 15715

Answers (4)

osim_ans
osim_ans

Reputation: 455

For tagging, you can use 'Label Sources' feature which is also available for yaml based pipelines. Refer to this : https://learn.microsoft.com/en-us/azure/devops/pipelines/repos/azure-repos-git?view=azure-devops&tabs=yaml#label-sources

Upvotes: 2

Samit Kumar Patel
Samit Kumar Patel

Reputation: 2098

Azure DevOps publish good documentation now `Enable scripts to run Git commands´. Please take a look here

  1. You have to configure
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
  1. Go to the Project settings --> select repository --> choose repository from the list(The one you want to commit from a pipeline)--> click on Permission(on top)-->In the search box , search for Project Collection Build Service, once it appears in the dropdown, select it and set the permission you want (attached image for your reference) List item

Then you are done. you can do all the git activity you want based on the permission you set on the above step. Note- Take extra care of your Build Trigger configuration, Otherwise when you commit anything from your pipeline on the repository, your pipeline will trigger again and again ..and will become circular. To avoid that, on your commit message add [skip ci] to avoid circular build.

Upvotes: 3

Paul Hatcher
Paul Hatcher

Reputation: 8156

You can do this in as yaml build steps, but the important part is that you need the pipeline to have permission to do the push and the simplest way is to keep the credentials from the checkout as below.

You then need to grant the Project Collection Build Service Create tag and Contribute (add/remove files) permissions.

I also found I had to set the working directory for the push to work

steps:
# Check out the code and keep the token so we can tag the repository
- checkout: self
  persistCredentials: true

--- rest of pipeline

# Tag the current branch with the version number
- script: |
    git add filename.ext
    git push origin
    git tag -a tagName -m 'tag message' 
    git push --tags
  workingDirectory: $(Build.SourcesDirectory)
  displayName: 'Git Commit and Tag from pipeline'

Upvotes: 8

Levi Lu-MSFT
Levi Lu-MSFT

Reputation: 30313

You are right, to commit the changes and push to source repo in azure devops pipeline, you probably have to run the git commands in script tasks.

In the script task, your accountName in the repo clone url needs to replaced with $(System.AccessToken) for authentication purpose (eg. https://$(System.AccessToken)@dev.azure.com/yourOrg/yourProj/_git/repoName).

You can check below example to tag and push a azure branch.

- bash: | 
        git config --global user.email "[email protected]"
        git config --global user.name "yourUsername"

        #git add filename.ext
        git add .
        git commit -m "message" 

        git push https://$(System.AccessToken)@dev.azure.com/yourOrg/yourProj/_git/repoName HEAD:master -q

        git tag -a tagName -m 'tag message'
        git push https://$(System.AccessToken)@dev.azure.com/yourOrg/yourProj/_git/repoName tagName 

Upvotes: 13

Related Questions