Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git commit and git tag in azure devops yml based pipeline

Tags:

azure-devops

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'
like image 884
Samit Kumar Patel Avatar asked Apr 04 '20 22:04

Samit Kumar Patel


People also ask

How do I tag a commit in Azure DevOps?

To create a tag directly from the commits view, right-click the desired tag and choose Create tag. You can create annotated tags using the web portal from both the Tags view and the Commits view. You can only create annotated tags in the web portal.

What are tags in Azure DevOps?

Tags are used in Azure DevOps for marking a particular release (or branch) with an identifier that will be shared internally in your team to identify, for example, the "version" of your code base.

How do I commit code from Azure DevOps to VS code?

Open the repo within Visual Studio Code when prompted to do so (after cloning the repo in the previous objective). Make changes to the README.md file. Commit the changes locally with a comment. Push/sync the changes to Azure DevOps.


3 Answers

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 
like image 85
Levi Lu-MSFT Avatar answered Oct 21 '22 22:10

Levi Lu-MSFT


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'
like image 23
Paul Hatcher Avatar answered Oct 21 '22 21:10

Paul Hatcher


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.

like image 23
Samit Kumar Patel Avatar answered Oct 21 '22 22:10

Samit Kumar Patel