使用 pat 使用 azure powershell 将代码推送到 bitbucket 时接收 "fatal: not a git repository (or any of the parent directories): .git"

Receiving "fatal: not a git repository (or any of the parent directories): .git" while using pat to push code into bitbucket using azure powershell

我正在编写一个 Azure PowerShell 脚本,该脚本将使用 JSON 文件,该文件包含我所有 SQL 脚本的位置和 Migrationflag 列(保存 execute/to 被执行)并执行所有脚本序列。 执行后,标志将更改为 'N' 并且更新后的 JSON 文件应上传到 bitbucket。 现在,我在尝试推送时遇到“致命:不是 git 存储库(或任何父目录):.git”错误。

我已经使用用户名创建了一个 pat 令牌和服务连接:santhoshsreshta 下面是要推送的代码。

$v_JSON = Get-Content '$(system.defaultworkingdirectory)\locationToBuild\BuildOrder.json' -Raw | ConvertFrom-Json
$v_JSON | Sort-Object -Property OrderNo | Where-Object {$_.MigratedFlag -like 'Y'} | ForEach {
                 $Script =  $_.Location
Write-Host "Executing Script: $Script"    
Invoke-Sqlcmd -ServerInstance "myservername" -Database $(database) -Username $(testauto_username)  -Password $(testauto_password) -InputFile $(system.defaultworkingdirectory)$Script
                    $_.MigratedFlag = 'N'
                    }
$v_JSON | ConvertTo-Json -depth 32| set-content '$(system.defaultworkingdirectory)\locationToBuild\BuildOrder.json'
                
$MyPat = 'mypatcode'
git push https://mygitusername:$MyPat@bitbucket.org/xyz/abcd.git

出现错误,“##[错误]严重:不是 git 存储库(或任何父目录):.git” 但是在发出 git clone https://mygitusername:$MyPat@bitbucket.org/xyz/abcd.git 时——变得无效 username/password 错误。 我相信我们不应该再次克隆,因为我的管道获取源任务将克隆它并放入一个自托管代理。

这是我的 git url:https://mygitusername@bitbucket.org/xyz/abcd.git

非常感谢, 这里是 DevOps、PowerShell 新手。

Receiving “fatal: not a git repository (or any of the parent directories): .git” while using pat to push code into bitbucket using azure powershell

那是因为我们正在使用 git 功能,但我们不在 git 管理的文件夹中。

当我们直接使用Azure powershell任务时,默认的工作文件夹应该是:

PS C:\Users\<Username>

很明显,这个目录下没有我们git管理的文件。这就是您收到错误 Not a git repository.

的原因

所以,为了解决这个问题,我们只需要通过命令将工作文件夹切换到repo文件夹:

cd $(System.DefaultWorkingDirectory)

查看使用 predefined variables and this thread 了解一些详细信息。

更新:

测试样本:

cd $(System.DefaultWorkingDirectory)
cd leotest

echo 123>Test.txt

git add Test.txt

git commit -m "Add a test file"

git push https://Username:password@bitbucket.org/Lsgqazwsx/leotest.git HEAD:master

表示没有local.git&你需要先做以下操作:

git init
git commit -m "first commit"
git branch -M main
git remote add origin https://your-repo-url
git push -u origin main

I just copied the below from GitHub page & it works for Azure Repos :-) further showing the power & advantage of companies following universal standards