Install husky git hooks in Jenkins pipeline job by overriding "CI detected, skipping Git hooks installation"

Viewed 2100

I am trying to use husky to install git hooks as part of a Jenkins pipeline job.

I've added this to the Jenkins job:

npm install husky --save-dev

But when the job runs I see this in the Jenkins output:

> node-sass@4.14.1 install /home/jenkins/agent/workspace/<branch_name>/node_modules/node-sass
> node scripts/install.js

Downloading binary from https://github.com/sass/node-sass/releases/download/v4.14.1/linux_musl-x64-72_binding.node
Download complete
Binary saved to /home/jenkins/agent/workspace/<branch_name>/node_modules/node-sass/vendor/linux_musl-x64-72/binding.node
Caching binary to /root/.npm/node-sass/4.14.1/linux_musl-x64-72_binding.node

> husky@4.3.0 install /home/jenkins/agent/workspace/<branch_name>/node_modules/husky
> node husky install

husky > Setting up git hooks
CI detected, skipping Git hooks installation.
husky > Done

... and the .git/hooks/precommit hook file is never created.

Troubleshooting research notes:

  • In the husky v4 documentation it says "By default, Husky won't install on CI servers." I can't find any documentation on how to override that default behavior.

  • I can't even find the "CI detected, skipping Git hooks installation" string in the code when searching the repo code.

  • I've found an issue in the husky repo from 2017 where the developer explains why husky doesn't install in CI but did not explain how to override that, instead indicating they'd be interested in hearing about use cases for running husky in CI (implying that at least in 2017 there was no way to override).

  • In this blog post from June 2019 the author implies that husky simply cannot be run in CI and that it uses is-ci to detect whether it's running on a CI server. is-ci is mentioned in the husky documentation here where they suggest using it to detect if husky is running in CI and use HUSKY=0 to disable it. However, the behavior I'm experiencing is that husky is already not running in CI. I've tried setting HUSKY=1 in the Jenkins job but that has no effect.

2 Answers

For passersby, this issue is still relevant with husky 4+. In CI systems you can run:

export HUSKY_SKIP_INSTALL=1

For ex:

Docker

ENV HUSKY_SKIP_INSTALL=1

GitLab CI

build:
  script:
    - export HUSKY_SKIP_INSTALL=1
    - npm install

GitHub Workflows

jobs:
  build:
    steps:
      env:
        HUSKY_SKIP_INSTALL: 1

Jenkins

Create a global property.

Sadly this has changed with v7. You could instead build a custom prepare script which by passes this, a la:

//package.json
{
 ...
 "scripts": {
   ...,
   "prepare": "node ./prepare.js"
  }
}
// prepare.js
const isCi = process.env.CI !== undefined
if (isCi) {
  require('husky').install()
}

source: https://typicode.github.io/husky/#/?id=with-a-custom-script

Related