AI Hints
Home›CI/CD with GitHub Actions Tutorial›First Workflows Practice
CI/CD with GitHub Actions · Lesson 34 of 138

Practice: A Lint Workflow

Your turn.

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

Your turn. Write a workflow called lint, that runs on every push and pull request. It should check out the code, set up Python, install flake8, and check the style. Pause, write it, then compare with mine.

Practice: A Lint Workflow
From the video: Practice: A Lint Workflow
  • Your turn
  • A lint workflow
  • Pause and write it

One answer

Here's my answer. On, with a list, covers both events. Checkout and setup python prepare the machine. Pip installs flake8 quietly. And the last step runs it, allowing lines up to a hundred characters. Did yours look similar? Small differences are fine.

.github/workflows/lint.ymlyaml
name: Lint
on: [push, pull_request]
jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
      - run: pip install -q flake8
      - name: Check style
        run: flake8 --max-line-length 100

Trying it

Let's add a style problem, an unused import, and run the workflow. Flake8 finds it, on line one of app dot py, plus a second problem, missing blank lines after it. The job fails. Then we remove the line, and run again. Clean code, and a green job.

Terminal
sed -i '1i import os' app.py
run-steps .github/workflows/lint.yml
Workflow: Lint  (event: push, branch: main)
Job: lint
  ↷ uses actions/checkout@v4  (GitHub only)
  ↷ uses actions/setup-python@v5  (GitHub only)
  ✓ Run pip install -q flake8
  ✗ Check style
      ./app.py:1:1: F401 'os' imported but unused
      ./app.py:2:1: E302 expected 2 blank lines, found 0
      Error: Process completed with exit code 1.
  Job failed
sed -i '1d' app.py && run-steps .github/workflows/lint.yml | tail -3
  ✓ Run pip install -q flake8
  ✓ Check style
  Job succeeded

Summary

A lint workflow checks style on every change. Flake8 catches problems like unused imports straight away. Next, a workflow with several triggers.

Key takeaways
  • on: [push, pull_request]
  • pip install flake8
  • flake8
Next up: Practice: Two Triggers — one workflow, many events