使用特定凭据删除远程标签

发布于 2025-01-11 22:47:58 字数 142 浏览 3 评论 0原文

是否可以使用特定的用户名和密码删除Azure中的git远程标签?我知道下面的代码可以删除远程标签,但我希望使用具有删除权限的特定用户名和密码来执行此操作。

git push --delete origin Tagname

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2025-01-18 22:47:58

您可以尝试通过调用 REST API 来编写脚本 (Refs - 更新 Refs) 使用特定凭据删除远程 Git 标记,

因为 Azure DevOps 不再支持 Alternate凭据身份验证,我们不能使用用户名和密码来执行此操作,但是我们可以使用 PAT

然后,您可以为 PAT 创建一个秘密变量,并添加 命令行任务要运行的 PowerShell 任务管道中的脚本。

下面的PowerShell脚本供您参考:(相应地替换参数)

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)}

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)}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文