0

I am new to Azure DevOps and learning and want to push a file from builds artifact directory to git repository. I am using this inline PowerShell script to do so:

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"

But I am getting this error and not able to push the file in repo:

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

Am I missing the obvious? How should I use PAT/Password to authenticate?

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
Jagrati Modi
  • 2,038
  • 1
  • 13
  • 26
  • First I would wonder why you want to commit your build artifact to source control. Then I would suggest you mark the option that scripts are allowed to access the OAuth token. See [this answer](https://stackoverflow.com/a/54728908/4629442) and [this Azure document](https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/powershell?view=azure-devops#use-the-oauth-token-to-access-the-rest-api). – Josh Gust Apr 19 '19 at 13:44
  • 2
    You should **not** be putting build outputs in source control under any circumstances. Use the `Publish Build Artifacts` task, or turn the artifact into a package (i.e. a NuGet package) and push it to an artifacts feed. – Daniel Mann Apr 19 '19 at 13:46
  • I think [this walkthrough](https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/git-commands?view=azure-devops&tabs=designer) is what you want to do so that you don't end up putting passwords or PATs in your pipeline. Again, if you have a use case where putting build artifacts in source control provides value, you have a broken process and need to rethink why you need this. – Josh Gust Apr 19 '19 at 13:55

1 Answers1

1

It's not recommended to store artifacts in Git!

But if you must to do it and you want the push will work, so in the git push line put the username & password:

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

In the azure-devops.com/repo.git put the repo url (it's replace the origin).

You can also use PAT (instead of username & password) in this way:

git push https://Personal%20Access%20Token:{TokenHere}@azure-devops.com/repo.git master
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114