AI Hints
CI/CD with GitHub Actions · Lesson 61 of 138

Linting

Style checks in CI.

⏱ 2 min read▶ Video at 1:09:38🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

A quality gate is a check that a change must pass before it's merged. Tests are one gate. Linting is another. A linter reads your code without running it, and finds mistakes like unused imports, undefined names, and messy style.

Linting
From the video: Linting
  • Quality gates
  • Linting reads code
  • Finds mistakes without running

What a linter finds

Let's add some sloppy code. An unused import, a variable that's never used, and a call to a function that doesn't exist. Flake8 finds all three, with the file, line, and column, and a short code for each problem. Undefined name is a real bug. It would crash at runtime.

Terminal
printf 'import os\n\n\ndef report():\n    unused = 1\n    return totl([1])\n' > report.py
flake8 report.py
report.py:1:1: F401 'os' imported but unused
report.py:5:5: F841 local variable 'unused' is assigned to but never used
report.py:6:12: F821 undefined name 'totl'

Linting in CI

Run the linter in CI, so every pull request is checked the same way, whoever wrote it. Agree on the rules as a team, and keep them in a config file, like setup dot c f g, or pyproject dot toml. And make it fast. Linting takes seconds, so it's usually the first gate.

keyvalue
flake8 / ruffPython
eslintJavaScript
Config fileshared rules
First gateit's fast

Summary

Linters find mistakes without running the code, and CI runs them on every change. Next, checking the code's formatting.

Key takeaways
  • flake8
  • Undefined names = bugs
  • Lint first, it's fast
Next up: Formatting Checks — one style for everyone