'Delete remote tags using specific credentials
Is it possible to delete git remote tags in Azure by using a specific username and password? I know below code can delete remote tags but I was hoping to use a specific username and password with delete permissions for this operation.
git push --delete origin Tagname
Solution 1:[1]
You can try to write a script by calling the REST API (Refs - Update Refs) to delete the remote Git tags using specific credential,
Since Azure DevOps no longer support Alternate Credentials authentication, we can not use username and password to do that, however we can use the PAT of the specific user to authenticate.
Then you can create a secret variable for the PAT, and add a Command line task or PowerShell task to run the script in pipeline.
Below PowerShell script for your reference: (replace the parameters accordingly)
Param(
[string]$orgurl = "https://dev.azure.com/{organization}",
[string]$project = "ProjectName",
[string]$user = "user",
[string]$token = "PAT", #PAT of the specific user
[string]$repositoryid = "459956ea-5d9b-4f61-9cbf-dc5fb872d830", #Repository ID
[string]$tag = "Tag01" #Tag name which you want to be deleted from remote repo.
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
#Get oldObjectId of the tag
$tagurl = "$orgurl/$project/_apis/git/repositories/$repositoryid/refs?filter=tags&api-version=5.0"
$tagresponse = (Invoke-RestMethod -Method GET -Uri $tagurl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}).value | where({$_.name -like "*$tag*"})
$oldObjectId = $tagresponse.objectId
#Update refs to delete a tag
#Create Jason body
function CreateJsonBody
{
$value = @"
[{
"name": "refs/tags/$tag",
"newObjectId": "0000000000000000000000000000000000000000",
"oldObjectId": "$oldObjectId"
}]
"@
return $value
}
$json = CreateJsonBody
$deleteurl = "$orgurl/$project/_apis/git/repositories/$repositoryid/refs?api-version=5.1"
#Update refs to delete a tag
Invoke-RestMethod -Uri $deleteurl -Method POST -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andy Li-MSFT |
