GitHub Actions `failed to execute command 'git'` for dotnet project that works locally?
Version
0.7.2
Details
Pre-commit hooks work fine locally
I can run dotnet husky run locally for testing.
I added a dotnet husky run as part of my github actions test steps.
When I test the action locally using Nektos ACT it works fine.
When my action runs on github I get this error:
4s
Run dotnet tool restore
dotnet tool restore
dotnet husky run
shell: /usr/bin/bash -e {0}
env:
DOTNET_ROOT: /usr/share/dotnet
Tool 'csharpier' (version '1.0.3') was restored. Available commands: csharpier
Tool 'husky' (version '0.7.2') was restored. Available commands: husky
Restore was successful.
[Husky] ๐ Loading tasks ...
failed to execute command 'git'
Could not find Husky path
Error: Process completed with exit code 1.
I found an issue with similar error, but this prjoctect has only 1 root and is in the root.
Any ideas what I'm doing wrong?
task runner:
{
"$schema": "https://alirezanet.github.io/Husky.Net/schema.json",
"tasks": [
{
"name": "CSharpier Format",
"command": "dotnet",
"args": [
"csharpier",
"format",
"--log-level=debug",
"${staged}"
],
"include": [
"**/*.cs"
]
},
{
"name": ".Net Format",
"command": "dotnet",
"args": [
"format",
"style",
"--verify-no-changes",
"--severity=info",
"--verbosity=detailed",
"--exclude-diagnostics=IDE0055"
],
"include": [
"**/*.cs"
]
}
]
}
Github actions:
test:
name: Test
runs-on: ubuntu-latest
steps:
# https://github.com/marketplace/actions/setup-net-core-sdk
- name: Setup .NET SDK
uses: actions/setup-dotnet@v4
with:
dotnet-version: "9.x"
# https://github.com/marketplace/actions/checkout
- name: Checkout code
uses: actions/checkout@v4
# Format checks
- name: Format checks
run: |
dotnet tool restore
dotnet husky run
# https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test
- name: Run unit tests
run: dotnet test ./PlexCleanerTests/PlexCleanerTests.csproj
Steps to reproduce
- Run locally, ok
- Run locally in github actions, ok
- Run remote github actions, fail
Hi @ptr727,
The error says failed to execute command 'git',
Husky is relying on the locally installed Git, and in your case, the remote environment doesnโt have Git installed โ thatโs why itโs failing during the GitHub Action run. Make sure Git is available in that environment for the hook to work.
Or you can simply disable Husky in the GitHub actions:
You can set the HUSKY environment variable to 0 in order to disable husky in CI/CD pipelines.
Git 2.50.1 is a standard tool installed in the runner image, see ubuntu-latest that is currently 24.04
Also note that when running locally using ACT, that uses the same/similar images, it works.
I do not want to disable Husky, I want to explicitly call it like a CLI tool, not a git hook, it is very convenient to package all formatting checks in one tool, and to use that as part of the build, for it will catch any hook bypasses or hook install failures.
Any other ideas?
To show that git is working, I called git status right before calling dotnet husky run:
- name: Check code style
run: |
dotnet tool restore
dotnet csharpier check --log-level=debug .
dotnet format style --verify-no-changes --severity=info --verbosity=detailed --exclude-diagnostics=IDE0055
# TODO: https://github.com/alirezanet/Husky.Net/issues/137
- name: Check code style (using Husky, ignore errors)
run: |
dotnet tool restore
git status
dotnet husky run || true
Run dotnet tool restore
Tool 'csharpier' (version '1.0.3') was restored. Available commands: csharpier
Tool 'husky' (version '0.7.2') was restored. Available commands: husky
Restore was successful.
On branch develop
Your branch is up to date with 'origin/develop'.
nothing to commit, working tree clean
[Husky] ๐ Loading tasks ...
failed to execute command 'git'
Could not find Husky path
I did some more experimenting, and I discovered that dotnet tool restore is not enough to get husky working, it is also required to run dotnet husky install again whenever checking out a fresh repo, this was unexpected, and the error is very confusing.
It looks like dotnet husky install creates .husky/_ and adds .gitignore that excludes * from git, and maybe the error is related to .husky/_/husky.sh not being found, vs. git not being found.
Or, I also noticed that when I install husky on Windows, and then sync the repo to Linux, it fails to run on Linux, due to pre-commit not having execute permissions. This can be fixed by either adding chmod +x on pre-commit, or to dotnet husky install from Linux.
Is it possible to:
- update the install docs to make it clear that if running on Linux then
dotnet husky installmust be done on Linux even if also dev on Windows - update the error to be more specific about the actual problem not being a missing git but execute permissions on
pre-commit, and note a similar issue will occur ifpre-commitis CRLF as bash will fail with not found error?
pieter@ThinkCenterM90s:~/PlexCleaner$ dotnet husky run
[Husky] ๐ Loading tasks ...
failed to execute command 'git'
Could not find Husky path
pieter@ThinkCenterM90s:~/PlexCleaner$ dotnet husky install
Git hooks installed
pieter@ThinkCenterM90s:~/PlexCleaner$ dotnet husky run
[Husky] ๐ Loading tasks ...
pieter@ThinkCenterM90s:~/PlexCleaner/.husky$ ls -la
total 20
drwxr-xr-x 3 pieter pieter 4096 Jul 19 10:24 .
drwxr-xr-x 12 pieter pieter 4096 Jul 19 08:27 ..
drwxr-xr-x 2 pieter pieter 4096 Jul 19 09:00 _
-rw-r--r-- 1 pieter pieter 59 Jul 19 10:24 pre-commit
-rw-r--r-- 1 pieter pieter 770 Jul 19 08:27 task-runner.json
pieter@ThinkCenterM90s:~/PlexCleaner/.husky$ ls -la
total 20
drwxr-xr-x 3 pieter pieter 4096 Jul 19 10:24 .
drwxr-xr-x 12 pieter pieter 4096 Jul 19 08:27 ..
drwxr-xr-x 2 pieter pieter 4096 Jul 19 09:00 _
-rwxr-xr-x 1 pieter pieter 59 Jul 19 10:24 pre-commit
-rw-r--r-- 1 pieter pieter 770 Jul 19 08:27 task-runner.json
Hi @ptr727,
Thanks for the investigation, yeah the husky install is always required so I think we should try to improve the error message in this case