Modernizing Workflows with Pre-commit Hooks in Drupal

Published: (March 8, 2026 at 12:18 PM EDT)
2 min read
Source: Dev.to

Source: Dev.to

Problem

When managing a Drupal distribution for a massive intergovernmental corporation with developers across three continents, code review cannot be the first line of defense against poor syntax. By the time a senior architect reviews a Pull Request, it is already too late to argue about indentations or missing docblocks.

In a custom upstream (CHG0099785), CI pipelines were continuously failing due to trivial PHPCS (PHP CodeSniffer) violations, creating massive bottlenecks in the merge cycle. A single tab instead of two spaces caused the CI pipeline to spend 5–10 minutes spinning up containers, running tests, and ultimately failing on the syntax check.

Multiplying 10 minutes of wait time by 20 developers committing three times a day resulted in hundreds of hours per week spent waiting for Jenkins/GitLab to report missing semicolons.

Solution

Shift the validation logic from the remote CI server to the developer’s local machine using automated Git pre‑commit hooks.

  • Integrated husky and lint‑staged into the project’s root package.json.
  • The hook intercepts the git commit command and targets only the files currently staged in Git.
  • Checking the entire legacy Drupal core takes minutes; checking the two files the developer just edited takes ~400 ms.

Implementation

Pre‑commit Hook Workflow

  1. Run phpcbf (PHP Code Beautifier and Fixer) on staged files.

    • If a developer uses double quotes instead of single quotes, phpcbf instantly corrects the file, stages the corrected version, and allows the commit to proceed silently.
  2. Run phpcs to enforce coding standards after automatic fixes.

Configuration Highlights

// package.json (excerpt)
{
  "scripts": {
    "prepare": "husky install"
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "*.php": [
      "phpcbf",
      "phpcs"
    ]
  }
}

Downstream Deployment

An edge case appeared when downstream vendor agencies lacked required binaries (e.g., Node.js for Husky). To handle this, the composer.json scaffolding scripts were adjusted to disable or bypass strict pre‑commit hooks during downstream deployment builds, ensuring the hooks apply only to first‑party UI/UX developers while remaining invisible to the production pipeline.

Results

  • CI failure rates due to syntax issues dropped by 98 %.
  • Senior architects now spend their time reviewing business logic and security vectors rather than commenting on line‑length limits.

References

  • Enterprise CMS case studies:
  • LinkedIn profile:
0 views
Back to Blog

Related posts

Read more »