Using husky Pre-Commit Hooks to Ensure Playwright Version Consistency
Aron Schüler Published
Ensuring Playwright Version Consistency with Pre-Commit Hooks
In web development, maintaining your package versions is one of the weekly tasks for many dev teams. One common issue that often arises for us is when upgrading npm packages: ensuring the Playwright version specified in your package.json
matches the version used in your CI/CD pipeline configuration (.gitlab-ci.yml
). Inconsistencies can lead to pipeline failures and wasted time+resources. If you are using playwright, I bet you have seen this before:
╔══════════════════════════════════════════════════════════════════════╗
║ Looks like Playwright Test or Playwright was just updated to 1.26.0. ║
║ Please update docker image as well. ║
║ - current: mcr.microsoft.com/playwright:v1.23.0-focal ║
║ - required: mcr.microsoft.com/playwright:v1.26.0-focal ║
║ ║
║ <3 Playwright Team ║
╚══════════════════════════════════════════════════════════════════════╝
To address this, we need to change the .gitlab-ci.yml docker image tag to match our updated package.json
version. But then, this will happen over and over again.
Better would be: set up Husky with a pre-commit hook that checks if the Playwright versions in package.json and .gitlab-ci.yml match before any commit is made, preventing these errors!
To do so, follow these few steps:
Step-by-Step Guide
-
Step 1: Install Husky
- First, ensure you have Husky installed in your project:
-
Step 2: Enable Git Hooks in Your Project
- Initialize Husky to enable Git hooks:
-
Step 3: Create a Husky Pre-Commit Hook
- Add a pre-commit hook by running:
-
Step 4: Write the Version Check Script
-
Create a directory named
scripts
in your project root if it doesn’t exist: -
In the
scripts
directory, create a script namedcheck_playwright_versions.sh
: -
Make the script executable:
-
-
Step 5: Verify Your Setup
- Test your pre-commit hook by making a change and committing it:
- If the Playwright versions don’t match, the commit will be blocked, and you’ll see an error message.
Conclusion
By using Husky to set up a pre-commit hook that ensures Playwright versions match between package.json
and .gitlab-ci.yml
, you can prevent pipeline failures caused by version mismatches. Should save you a bit of time returning to those chore: upgrade packages
pull requests only to see that your playwright stage failed.
Regularly update and review your hooks and scripts to adapt to changes in your project configuration. If you have any questions or experiences to share, feel free to leave a comment below. Happy coding!