首页 > 解决方案 > 如何在 Azure DevOps 构建期间使用 PowerShell 脚本将构建工件推送到 Git 存储库?

问题描述

我是 Azure DevOps 和学习的新手,想将文件从构建工件目录推送到 git 存储库。我正在使用这个内联 PowerShell 脚本来执行此操作:

Write-Host "Hello World"
write-host " Staging directory "$(build.artifactstagingdirectory)
git config --global user.email <useremail>
git config --global user.name <username>
git status
$CurrentPath = Get-Location
write-host "current path" $CurrentPath
$PackagePath =new-item -type directory $(get-date -f MM-dd-yyyy_HH_mm_ss)
$PackageZipPath = "$(build.artifactstagingdirectory)\<artifact to be copied>"
Copy-Item $PackageZipPath -Destination $PackagePath 
git add .
git commit -m "Adding file"
git push origin master
git status
write-host "Package Uploaded to GitHub"

但我收到此错误,无法在 repo 中推送文件:

fatal: could not read Password for 'https://<username>@dev.azure.com': terminal prompts disabled

我错过了显而易见的事情吗?我应该如何使用 PAT/Password 进行身份验证?

标签: gitazurepowershellazure-devops

解决方案


不建议将工件存储在 Git 中!

但是,如果您必须这样做并且您希望push意志起作用,那么在该git push行中输入用户名和密码:

git push https://username:password@azure-devops.com/repo.git master

azure-devops.com/repo.git放置 repo url(它替换origin)。

您也可以通过这种方式使用 PAT(而不是用户名和密码):

git push https://Personal%20Access%20Token:{TokenHere}@azure-devops.com/repo.git master

推荐阅读