PSU New Installation with Azure DevOps Git Sync

Hi,
I’m trying to configure a git sync with azure devops in a new psu installation.

I deleted the .git folder under:
C:\ProgramData\UniversalAutomation\Repository

Repo only has one master branch, it’s completly empty.

I tried pushonly for the first sync, this is my config:

  "Data": {
    "RepositoryPath": "%ProgramData%\\UniversalAutomation\\Repository",
    "ConnectionString": "filename=%ProgramData%\\UniversalAutomation\\database.db;upgrade=true",
    "RunMigrations": true,
    "GitRemote": "https://myazuredevopsrepo",
    "GitUserName": "somestring",
    "GitPassword": "PATcomeshere",
    "GitBranch": "master",
    "GitSyncBehavior": "PushOnly",
    "GitInitializeBehavior": "init",
    "GitSyncInterval": "1",
    "GitSyncTimeout": 60,
    "ConfigurationScript": "",
    "Mode": "Manual"

I created a PAT with all access rights for code, so PSU should be able to handle the repo.

I start the PSU Service. I’m getting randomly the following errors:

Git synchronization failed. failed to write reference ‘refs/heads/master’: a reference with that name already exists.

or

Object reference not set to an instance of an object

or

too many redirects or authentication replays

Do I have to specify a valid GitUserName or can it be a random string? In Azure DevOps there’s just a PAT no Username

What am I doing wrong ?

Thanks!

Product: PowerShell Universal
Version: 4.2.21

Same setup here. Here is a copy of my Git setup, in case it helps to see another. The username can be random but I chose to have it be a valid username just so I knew which account had the PAT being used.

"Data": {
	"RepositoryPath": "%ProgramData%\\UniversalAutomation\\Repository",
	"ConnectionString": "filename=%ProgramData%\\UniversalAutomation\\database.db",
	"GitRemote": "https://<redacted>",
    "GitUserName": "jpeden",
    "GitPassword": "<redacted>", 
    "GitBranch": "main",
    "ConfigurationScript": "",
	"GitSyncBehavior": "TwoWay",
    "Mode": "automatic"

Another issue you may run into is an error stating that "There is no tracking information for the current branch, which is a “known bug”. The fix for that is to add

[branch "main"]
	remote = origin
	merge = refs/heads/main

to the config file in your .git folder.
(adjust the name of the branch as needed).

You can read more about the issue at There is no tracking information for the current branch. - Sal Ferrarello

1 Like

Thanks for your response. I still can’t get it to work, tried many different config settings, also yours. There are some additional errors in the log. I have no clue what to do…

2024-05-30 07:09:16.755 -07:00 [ERR] Error discovering modules
System.ComponentModel.Win32Exception (2): An error occurred trying to start process 'pwsh' with working directory 'C:\ProgramData\UniversalAutomation\Repository'. The system cannot find the file specified.
   at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
   at UniversalAutomation.JobProcessManager.StartProcessAsCurrentUser(String powerShellPath, String commandLine, Variable credential, Action`1 onAgentOutput, Dictionary`2 environmentVariables) in C:\actions-runner\_work\universal\universal\src\Universal.Server\Services\Automation\JobProcessManager.cs:line 457
   at UniversalAutomation.JobProcessManager.StartModuleDiscovery(ExecutionEnvironment environment) in C:\actions-runner\_work\universal\universal\src\Universal.Server\Services\Automation\JobProcessManager.cs:line 333
   at Universal.Server.Services.ModuleService.Discover(ExecutionEnvironment environment) in C:\actions-runner\_work\universal\universal\src\Universal.Server\Services\ModuleService.cs:line 217
2024-05-30 07:09:17.597 -07:00 [INF] Request starting HTTP/1.1 POST http://localhost:5000/notificationhub/negotiate?negotiateVersion=1 text/plain;charset=UTF-8 0
2024-05-30 07:09:17.603 -07:00 [INF] CORS policy execution failed.

2024-05-30 07:09:36.866 -07:00 [INF] Remote is empty. Init first.
2024-05-30 07:09:37.035 -07:00 [ERR] Failed to sync: failed to write reference 'refs/heads/master': a reference with that name already exists.    at LibGit2Sharp.Core.Ensure.HandleError(Int32 result) in C:\actions-runner\_work\universal\universal\src\libgit2sharp\LibGit2Sharp\Core\Ensure.cs:line 154
   at LibGit2Sharp.Core.Ensure.ZeroResult(Int32 result) in C:\actions-runner\_work\universal\universal\src\libgit2sharp\LibGit2Sharp\Core\Ensure.cs:line 172
   at LibGit2Sharp.Core.Proxy.git_branch_create_from_annotated(RepositoryHandle repo, String branch_name, String targetIdentifier, Boolean force) in C:\actions-runner\_work\universal\universal\src\libgit2sharp\LibGit2Sharp\Core\Proxy.cs:line 118
   at LibGit2Sharp.BranchCollection.Add(String name, String committish, Boolean allowOverwrite) in C:\actions-runner\_work\universal\universal\src\libgit2sharp\LibGit2Sharp\BranchCollection.cs:line 158
   at LibGit2Sharp.BranchCollection.Add(String name, String committish) in C:\actions-runner\_work\universal\universal\src\libgit2sharp\LibGit2Sharp\BranchCollection.cs:line 118
   at LibGit2Sharp.RepositoryExtensions.CreateBranch(IRepository repository, String branchName, String committish) in C:\actions-runner\_work\universal\universal\src\libgit2sharp\LibGit2Sharp\RepositoryExtensions.cs:line 151
   at LibGit2Sharp.RepositoryExtensions.CreateBranch(IRepository repository, String branchName) in C:\actions-runner\_work\universal\universal\src\libgit2sharp\LibGit2Sharp\RepositoryExtensions.cs:line 129
   at UniversalAutomation.Git.GitService.Init(GitSettings settings) in C:\actions-runner\_work\universal\universal\src\Universal.Server\Services\GitService.cs:line 316
   at UniversalAutomation.Git.GitService.Initialize() in C:\actions-runner\_work\universal\universal\src\Universal.Server\Services\GitService.cs:line 274
   at UniversalAutomation.GitSyncService.TrySync(GitStatus status, Boolean force, GitSettings settings) in C:\actions-runner\_work\universal\universal\src\Universal.Server\Services\GitSyncService.cs:line 157
   at UniversalAutomation.GitSyncService.Sync(Boolean force) in C:\actions-runner\_work\universal\universal\src\Universal.Server\Services\GitSyncService.cs:line 104

2024-05-30 07:11:12.354 -07:00 [ERR] Failed to sync: Object reference not set to an instance of an object.    at UniversalAutomation.Git.GitService.<>c__DisplayClass31_1.<Push>b__4(BranchUpdater x) in C:\actions-runner\_work\universal\universal\src\Universal.Server\Services\GitService.cs:line 628
   at LibGit2Sharp.BranchCollection.Update(Branch branch, Action`1[] actions) in C:\actions-runner\_work\universal\universal\src\libgit2sharp\LibGit2Sharp\BranchCollection.cs:line 290
   at UniversalAutomation.Git.GitService.Push() in C:\actions-runner\_work\universal\universal\src\Universal.Server\Services\GitService.cs:line 627
   at UniversalAutomation.GitSyncService.TrySync(GitStatus status, Boolean force, GitSettings settings) in C:\actions-runner\_work\universal\universal\src\Universal.Server\Services\GitSyncService.cs:line 240
   at UniversalAutomation.GitSyncService.Sync(Boolean force) in C:\actions-runner\_work\universal\universal\src\Universal.Server\Services\GitSyncService.cs:line 104

One of the errors I see says 2024-05-30 07:09:36.866 -07:00 [INF] Remote is empty. Init first.. Did you initialize the repo during its creation? If not, you need to use git init on the Azure side. In GitHub, initializing during creation is not mandatory, but I can’t recall on Azure DevOps if it’s automatic (I’m assuming not, otherwise you’d likely not get that message).

I already made some changes via SourceTree. Deleted all files, commited and pushed into master branch. For now the master branch is empty. From my understanding if the master branch is empty and I delete the .git folder under:
C:\ProgramData\UniversalAutomation\Repository PSU creates a new folder .git with the basic files and that should push into the master branch with my settings. Repository Settings also looking good. I also gave the PAT temporarly full rights, just in case…

@Lukas1 Okay. Just to be sure about the initialization topic/error, during the creation of the repo on the Azure DevOps side, did you leave the box checked to create README or did you uncheck that option? You shouldn’t need to have done that, since you have "GitInitializeBehavior": "init" in your config file.

Also, just to be sure, you are editing the appsettings.json file located at C:\ProgramData\PowerShellUniversal\appsettings.json and not the one at C:\Program Files (x86)\Universal\appsettings.json, with these Git settings, right?

The Repo is completly empty. I also created a new one for testing purposes. Still got the same errors:

failed to write reference ‘refs/heads/master’: a reference with that name already exists.

and

Object reference not set to an instance of an object.

I’m worried about using it productively if the installation is already so “shaky” and reading other threads with similar problems.

maybe I have to give it another try with the external client option…

Interesting. Have you tried giving the branch a different name rather than master (such as main, or production, etc.)? Just wondering if that’ll change anything since it’s saying master already exists as a reference.

I had issues with the Git functionality when I first stood up PSU, and tried many things (including using an external Git client) to fix the issues. In the end, I went back to using the built-in Git client after talking about the problem(s) I was facing and finding that they were essentially due to a misunderstanding of the steps the built-in Git client performs during a sync versus what I EXPECTED it to do during a sync, and needing to add a line to my scripts and APIs that make changes to files in the repository. Once I did that, I haven’t have any issues other than ones caused on the remote side (like Azure DevOps just being flaky sometimes) or connectivity issues.

Using an external client worked fine but brought new challenges with it, like having to make a custom Bash script and scheduled task (Cron job) to manage how often to sync, etc., as well as having to manage the credentials outside of PSU (just one more thing to have to remember).

Looking online, I see some other cases of this error where it ended up essentially being an issue of duplicated references and case sensitivity. Such as, having a branch named Master and another named master and Git getting confused about which one to use. I don’t suspect that to be the case here since you created new repos.

I changed my config to main. main branch doesn’t exist in my repo.

getting the error:

Git Synchronization Failed

Unknown branch: main

After that I created the empty branch main (with no readme file, double checked that).

Then restarted the PSU Service and getting now again the error:

too many redirects or authentication replays

Failed to sync: too many redirects or authentication replays at LibGit2Sharp.Core.Ensure.HandleError(Int32 result) in C:\actions-runner_work\universal\universal\src\libgit2sharp\LibGit2Sharp\Core\Ensure.cs:line 154
at LibGit2Sharp.Core.Ensure.ZeroResult(Int32 result) in C:\actions-runner_work\universal\universal\src\libgit2sharp\LibGit2Sharp\Core\Ensure.cs:line 172
at LibGit2Sharp.Core.Proxy.git_remote_push(RemoteHandle remote, IEnumerable1 refSpecs, GitPushOptions opts) in C:\actions-runner\_work\universal\universal\src\libgit2sharp\LibGit2Sharp\Core\Proxy.cs:line 2246 at LibGit2Sharp.Network.Push(Remote remote, IEnumerable1 pushRefSpecs, PushOptions pushOptions) in C:\actions-runner_work\universal\universal\src\libgit2sharp\LibGit2Sharp\Network.cs:line 372
at LibGit2Sharp.Network.Push(Remote remote, String pushRefSpec, PushOptions pushOptions) in C:\actions-runner_work\universal\universal\src\libgit2sharp\LibGit2Sharp\Network.cs:line 331
at LibGit2Sharp.Network.Push(IEnumerable`1 branches, PushOptions pushOptions) in C:\actions-runner_work\universal\universal\src\libgit2sharp\LibGit2Sharp\Network.cs:line 255
at LibGit2Sharp.Network.Push(Branch branch, PushOptions pushOptions) in C:\actions-runner_work\universal\universal\src\libgit2sharp\LibGit2Sharp\Network.cs:line 216
at UniversalAutomation.Git.GitService.Push() in C:\actions-runner_work\universal\universal\src\Universal.Server\Services\GitService.cs:line 632
at UniversalAutomation.GitSyncService.TrySync(GitStatus status, Boolean force, GitSettings settings) in C:\actions-runner_work\universal\universal\src\Universal.Server\Services\GitSyncService.cs:line 240
at UniversalAutomation.GitSyncService.Sync(Boolean force) in C:\actions-runner_work\universal\universal\src\Universal.Server\Services\GitSyncService.cs:line 104

The authentication errors, I’m used to seeing. That tends to just be something like an expired PAT, missing permissions, a typo, etc. Sometimes it does happen for random reasons, though, and then just goes away. For example, I’ll come in some mornings and see a log entry about Git failing to sync for that reason, but I’ll look and see Git is in sync and changes I make are synced without any issues, so it just makes me wonder if it’s due to general internet traffic causing latency, or the remote side doing some kind of maintenance, etc.

For the record, I sometimes get that same authentication error in my Git client that syncs to the same Azure DevOps repo, so it’s not a PSU issue.

Thanks again for your assistance. We look probably for another solution, I don’t want to put my time to dig into things where they should work out of the box. Already spend to much time with that. But I really appreciate you tried to help and gave some useful hints. Maybe I give it another try in a few weeks, I don’t know :slight_smile:

It’s your call. The forum isn’t going anywhere when you’re ready to try again. For the record, to account for the delay of messaging back and forth on this post, there is a live chat feature that might help to speed things along.