Setup Husky rule for “ng build -c production” before pushing commits to your Git/BitBucket/Azure Devops repository

Balram Chavan
2 min readMay 17, 2024

While working with feature branch strategy, you would always strive to keep your main branch build stable. There are few options to achieve this.

  1. Feature branch specific builds
    If your company can afford the DevOps process to set up and run builds for each feature branch, this will be the right strategy for you. For every commit you push to your branch will trigger a build for that branch. You could set up a Pull Request merging rule to allow merge only if the feature branch build is green.
  2. Trigger build on Pull Request
    Another approach is to set up a rule to trigger a build when you open a pull request against main branch. The merge action will take place only if this build is successful.
  3. Run a production build locally before you push to a remote repository.

I will focus on a build pipeline for Angular project here. I wanted to implement the 3rd approach in one of my projects. Whenever, I am about to push my local changes to remote, I wanted to run production build command.

To do so, I simply added a Husky pre-hook. Here are the steps to achieve this.

  • Install Husky package by running command npm install --save-dev husky or yarn add --dev husky
  • Based on the pre-hooks you want, create a file with that name. e.g. ./husky/pre-push. If you want to run prettifier or linter commands on git commit, you can create pre-commit a file in the .husky folder.
  • Now, whenever you run a git push command, the production builds command ng build -c production shall be triggered. You can specify more parameters or different command if needed.

--

--