Course contentsShow
← Web Development
Lesson 1445 of 2,98226. Code Quality ToolsFree lesson

Running Stylelint in CI/CD

Adding Stylelint checks to pre-commit hooks and continuous integration pipelines.

Running Stylelint in CI/CD

You'll learn how to automate Stylelint checks before code reaches production, catching style issues early in your development pipeline.

Why Automate Style Checks?

Manual linting is like proofreading your own writing—you'll miss things. Integrating Stylelint into your CI/CD pipeline and pre-commit hooks acts as an automatic editor, ensuring every piece of CSS meets your standards before it's merged or deployed.

Pre-Commit Hooks

Pre-commit hooks run checks before code is committed to version control. Using tools like Husky, you can configure a hook to run Stylelint automatically:

// package.json
{
  "scripts": {
    "lint:css": "stylelint '**/*.css'"
  },
  "husky": {
    "hooks": {
      "pre-commit": "npm run lint:css"
    }
  }
}

If Stylelint finds errors, the commit is blocked until you fix them. This prevents problematic styles from ever entering your repository.

Continuous Integration Pipelines

In CI environments (GitHub Actions, GitLab CI, Jenkins), add Stylelint as a required check:

# .github/workflows/lint.yml
name: Lint CSS
on: [push, pull_request]
jobs:
  stylelint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: npm install
      - run: npm run lint:css

This runs on every push or pull request. If Stylelint fails, the build fails, and the code can't be merged. It's your team's safety net.

Exit Codes and Failure Handling

Stylelint returns exit code 0 for success and non-zero for errors. CI systems recognize these codes, automatically failing builds when issues are detected. You can also configure maximum warnings before failure.

Key Takeaway: Automating Stylelint in pre-commit hooks and CI/CD pipelines enforces consistent CSS quality across your entire team, preventing style issues from reaching production without manual oversight.