Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git hook failed on CI/CD git checkout #109

Open
darthkurak opened this issue May 6, 2024 · 3 comments
Open

Git hook failed on CI/CD git checkout #109

darthkurak opened this issue May 6, 2024 · 3 comments

Comments

@darthkurak
Copy link

Version

Newest

Details

I have an advanced build system in Nuke for my .NET apps.
That build system makes sure that dotnet husky install is called each time when I build the app.
It works almost perfectly fine.
I have one issue in CI/CD case.
When my Azure DevOps pipeline uses git checkout task, and this is not a first run - I am getting:
.husky/post-checkout: 2: .: Can't open .husky/_/husky.sh error.
I debugged this and the flow is following:

  • When first run on Azure Devops agent - repository is freshly cloned, so, there is not git hooks yet.
  • Then the build system is run, which calls "dotnet husky install" - and adds git hook
  • Build succeed
  • On the next pipeline run on this agent, the git checkout task makes git clean which removes _/husky.sh (because it is not committed) - but git hook already exists. So, git checkout fails due to missing _/husky.sh from .husky/post-checkout.

What is the best recommendation here?
I found a few workarounds, but I am not sure if they are valid and proper:

  • check for the existence of _/husky.sh in post-checkout script
  • run dotnet husky install in post-checkout script
  • commit _/husky.sh and change _/.gitignore from * to cache - this one unfortunately is overridden each time when someone/something run dotnet husky install

Steps to reproduce

I described above.

@darthkurak darthkurak added the bug Something isn't working label May 6, 2024
@alirezanet
Copy link
Owner

alirezanet commented May 6, 2024

Hi @darthkurak,

Typically, I'd recommend disabling Husky in CI/CD pipelines by setting the environment variable HUSKY=0. However, if you need to keep it enabled, here are two options:

  • Commit the script: Commit the .husky/_/husky.sh file for simplicity.
  • Exclude from git clean: Use git clean -xf -e .husky/_/husky.sh to preserve Husky files during cleanup. You might also find helpful insights in this

(I haven't personally tested reinstalling Husky in post-checkout, but it's worth considering.)
Let me know if you have any questions!

@darthkurak
Copy link
Author

darthkurak commented May 6, 2024

But how is disabling with HUSKY=0 supposed to work?
It fails on the first line in post-checkout: . "$(dirname "$0")/_/husky.sh"
Isn't too early for HUSKY=0 to work? :)

@alirezanet
Copy link
Owner

alirezanet commented May 6, 2024

You're right now I understand the problem,
I would say this is outside the Husky.Net scope, you need to adjust your CI/CD pipeline to either avoid using git clean in a way that removes necessary files for your project setup or somehow explicitly restore the husky.sh file after the git clean step, (maybe even copy it manually in a separate step after git clean, since this file rarely changes)

@alirezanet alirezanet removed the bug Something isn't working label May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants