AI Hints
Home›CI/CD with GitHub Actions Tutorial›Templates and Shared Workflows
CI/CD with GitHub Actions · Lesson 116 of 138

Practice: A Shared Workflow

One workflow, many callers.

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

Practice time. Write a reusable quality workflow, that lints and tests with a coverage minimum. The minimum should be an input, with a default of ninety. Then call it from CI, asking for ninety five.

Practice: A Shared Workflow
From the video: Practice: A Shared Workflow
  • A reusable quality workflow
  • Coverage as an input
  • Call it with 95

The shared workflow

Workflow call makes it reusable. It declares a number input called coverage, with a default of ninety. The job installs, lints, and runs pytest. The coverage minimum comes from inputs dot coverage.

.github/workflows/quality.ymlyaml
name: Quality
on:
  workflow_call:
    inputs:
      coverage:
        type: number
        default: 90
jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: pip install -q -r requirements.txt
      - run: flake8
      - run: pytest -q --cov=app --cov-fail-under=${{ inputs.coverage }}

Calling it

Cat shows the caller. A single job, that uses the shared workflow, and passes coverage ninety five. Our helper runs the call. It shows the inputs, then runs the called job, with the minimum set to ninety five. Every check passes.

Terminal
cat .github/workflows/ci.yml
name: CI
on: [push, pull_request]
jobs:
  quality:
    uses: ./.github/workflows/quality.yml
    with:
      coverage: 95
run-steps .github/workflows/ci.yml | grep -E "^Calling|^Job|✓|✗|Required|succeeded"
Calling ./.github/workflows/quality.yml  (inputs: coverage=95)
Job: quality / check
  ✓ Run pip install -q -r requirements.txt
  ✓ Run flake8
  ✓ Run pytest -q --cov=app --cov-fail-under=95
      Required test coverage of 95% reached. Total coverage: 100.00%
  Job succeeded

Summary

One shared workflow, with inputs, can serve every repository. Change it once, and everyone gets the update. Next, a quiz on advanced workflows.

Key takeaways
  • workflow_call
  • inputs.coverage
  • One place to change
Next up: Advanced Quiz — test yourself