接收“致命:不是 git 存储库(或任何父目录):.git";使用 pat 使用 azure powershell 将代码推送到 bitbucket 中 [英] Receiving "fatal: not a git repository (or any of the parent directories): .git" while using pat to push code into bitbucket using azure powershell

查看:17
本文介绍了接收“致命:不是 git 存储库(或任何父目录):.git";使用 pat 使用 azure powershell 将代码推送到 bitbucket 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

$v_JSON = Get-Content '$(system.defaultworkingdirectory)locationToBuildBuildOrder.json' -Raw |ConvertFrom-Json$v_JSON |Sort-Object -Property OrderNo |Where-Object {$_.MigratedFlag -like 'Y'} |ForEach{$Script = $_.Location写主机执行脚本:$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|设置内容 '$(system.defaultworkingdirectory)locationToBuildBuildOrder.json'$MyPat = 'mypatcode'git push https://mygitusername:$MyPat@bitbucket.org/xyz/abcd.git

得到错误,##[error]fatal: Not a git repository (or any parent directory): .git";但是当发出 git clone https://mygitusername:$MyPat@bitbucket.org/xyz/abcd.git - 出现无效的用户名/密码错误.我相信我们不应该再次克隆,因为我的管道获取源任务将克隆它并放入自托管代理.

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

非常感谢,DevOps,PowerShell 新手.

解决方案

在使用 pat 使用 azure powershell 将代码推送到 bitbucket 时收到致命的:不是 git 存储库(或任何父目录):.git"

那是因为我们使用的是git特性,但是我们不在git管理的文件夹中.

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

PS C:Users<用户名>

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

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

cd $(System.DefaultWorkingDirectory)

检查使用

I am writing an Azure PowerShell script that will consume the JSON file which has the location of all my SQL scripts and Migrationflag column (which holds to execute/to be executed) and execute all the sequence of scripts. upon execution, the flag will change to 'N' and the updated JSON file should be uploaded to bitbucket. Now, I am stuck with "fatal: not a git repository (or any of the parent directories): .git" error while trying to push.

I've created a pat token and service connection with username: santhoshsreshta and below is the code to push.

$v_JSON = Get-Content '$(system.defaultworkingdirectory)locationToBuildBuildOrder.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)locationToBuildBuildOrder.json'
                
$MyPat = 'mypatcode'
git push https://mygitusername:$MyPat@bitbucket.org/xyz/abcd.git

getting the error,"##[error]fatal: Not a git repository (or any of the parent directories): .git" but when issuing git clone https://mygitusername:$MyPat@bitbucket.org/xyz/abcd.git -- getting invalid username/password error. I believe we should not clone again as my pipelines get sources task will clone it and puts in a self-hosted agent.

this is my git url: https://mygitusername@bitbucket.org/xyz/abcd.git

Thanks a ton, A DevOps, PowerShell newbie here.

解决方案

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

That because we are using git feature, but we are not in the folder managed by git.

When we use Azure powershell task directly, the default work folder should be:

PS C:Users<Username>

Obviously, there are no files managed by our git in this directory. That the reason why you get the error Not a git repository.

So, to resolve this issue, we just need to switch the working folder to the repo folder by the command:

cd $(System.DefaultWorkingDirectory)

Check the Use predefined variables and this thread for some details.

Update:

The test sample:

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

这篇关于接收“致命:不是 git 存储库(或任何父目录):.git";使用 pat 使用 azure powershell 将代码推送到 bitbucket 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆