【问题标题】:How to push build artifacts to Git repository using PowerShell script during Azure DevOps build?如何在 Azure DevOps 构建期间使用 PowerShell 脚本将构建工件推送到 Git 存储库?
【发布时间】:2019-09-09 18:12:15
【问题描述】:

我是 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 进行身份验证?

【问题讨论】:

  • 首先我想知道您为什么要将构建工件提交到源代码控制。然后我建议您标记允许脚本访问 OAuth 令牌的选项。见this answerthis Azure document
  • 在任何情况下,您都应该将构建输出置于源代码控制中。使用 Publish Build Artifacts 任务,或将工件转换为包(即 NuGet 包)并将其推送到工件源。
  • 我认为 this walkthrough 是您想要做的,这样您就不会在管道中输入密码或 PAT。同样,如果您有一个使用案例,其中将构建工件放入源代码控制中会提供价值,那么您的流程就会中断,需要重新考虑为什么需要它。

标签: git azure powershell azure-devops


【解决方案1】:

不建议将工件存储在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    • 2019-10-18
    • 1970-01-01
    • 2012-06-24
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多