Husky.Net icon indicating copy to clipboard operation
Husky.Net copied to clipboard

Git hook failed on CI/CD git checkout

Open darthkurak opened this issue 9 months ago • 3 comments

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 avatar May 06 '24 09:05 darthkurak

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!

alirezanet avatar May 06 '24 10:05 alirezanet

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? :)

darthkurak avatar May 06 '24 11:05 darthkurak

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 avatar May 06 '24 15:05 alirezanet