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'
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.
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.
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.
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
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'
Azure DevOps publish good documentation now `Enable scripts to run Git commands´. Please take a look here
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
Project Collection Build Service
, once it appears in the dropdown, select it and set the permission you want (attached image for your reference)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With