I have a devops pipeline that is designed to do some deployment. Due to some requirements , I need to delete the branch that triggered the pipeline once the deployment is completed. The first approach I thought was using below script snippet is bash/script task:
git remote remove origin
git remote add origin https://<PAT>@<company_machineName>.visualstudio.com:/<path-to-git-repo>
git push -d origin $branch
The problems with this is the usage of PAT in the authentication step. I don’t want to use it as if the person that owns the PAT leaves the organization , then we have to change this PAT again. Is there any other way to achieve the same ? Can we do the authentication with a service connection
2
Answers
You may use AzureCLI@2 to run
az
command line through a service connection. Check az repos ref delete subcommand.Instead of
service connection
, i suggest you to use$(system.accesstoken)
which is a DevOps built-in token.build service account
hascontribute
andForce push (rewrite history, delete branches and tags)
on the repositories.ref objectid
of the sourcebranch which can get fromaz repos ref list
command.For example, i have a
CI
build as below, i add thedeletion task
to the end:It will delete the source branch
dev1
which triggers the build:Please note the
build service account
could be different due to your project setting, it could becollection-scoped
(Project Collection Build Service ({OrgName})) orproject-scoped
({Project Name} Build Service ({Org Name})) . please refer to the doc for more details, grant repo permission above on correct identity.In addition, above yaml sample is basic build, it could happen the build is triggered by a pull request, other repository…etc, make sure to use correct predefined variables for the parameters of azure cli.