0

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)\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

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.

Santhosh
  • 21
  • 1
  • 8
  • How about the issue? Does the answer below resolved your question, If not, would you please let me know the latest information about this issue? – Leo Liu Jul 22 '20 at 09:18
  • 1
    Hi @LeoLiu-MSFT, I got to know from other sources that DevOps PAT is not supporting for my version control i.e. BitBucket. I have tried to add predefined variables as per your suggestion unfortunately there was the same error. so I am trying to directly pass username:password instead of using PAT, but still I am facing issues while pushing the code to bitbucket. – Santhosh Jul 22 '20 at 10:19
  • Thanks you reply. How about the result when you use `git clone` with username:password? We could use this command line to make sure we have the correct username:password. And If you update some files or add some files, we need use the git command `git add` and `git commit` before the command `git push`. I test it and it works fine on my side. – Leo Liu Jul 23 '20 at 09:40
  • BTW, I updated my answer with the sample I test, you could check it for some details. – Leo Liu Jul 23 '20 at 09:56
  • Have you check mu update answer? If it helps you? If not, please let me know for free. – Leo Liu Jul 24 '20 at 07:56
  • I've tried the same way by passing username:password, I still see some challenges in my case but I can close this as I feel, it should work, I just need to find the exact issue, its weird in my case that, the pipeline is completing without error but the file is not getting updated. its okay, I will figure it out and post it soon here. – Santhosh Jul 24 '20 at 11:37

2 Answers2

0

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

enter image description here

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
0

It means that there is no local .git & you need to do the below first:

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