AI Hints
Home›CI/CD with GitHub Actions Tutorial›Delivery Quiz and Reusable Workflows
CI/CD with GitHub Actions · Lesson 101 of 138

Reusable Workflows

Write once, call anywhere.

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

When many repositories use the same pipeline, copying it everywhere gets painful. Fix it in one, and you have to fix it in twenty. A reusable workflow is written once, and other workflows call it, like a function.

Reusable Workflows
From the video: Reusable Workflows
  • Copies get out of date
  • Write it once
  • Call it like a function

The reusable workflow

The trigger is workflow call, which means other workflows can call it. It takes an input, the Python version, with a default. It can accept a secret. And the job uses the input, with inputs dot python version.

.github/workflows/python-tests.ymlyaml
name: Python tests
on:
  workflow_call:
    inputs:
      python-version:
        type: string
        default: "3.12"
    secrets:
      CODECOV_TOKEN:
        required: false
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with: { python-version: "${{ inputs.python-version }}" }
      - run: pip install -q -r requirements.txt && pytest -q

Calling it

The caller doesn't have steps. Its job has uses, with the path to the workflow, or a path in another repository, with an at and a version. With passes the inputs, and secrets inherit passes the caller's secrets along.

Same repository

  • uses: ./.github/workflows/
  • python-tests.yml
  • with: inputs

Another repository

  • uses: org/ci/.github/
  • workflows/python-tests.yml@v1
  • secrets: inherit

Summary

Reusable workflows use workflow call, and callers use them at the job level, with uses. Next, inputs and secrets in more detail.

Key takeaways
  • on: workflow_call
  • jobs.X.uses
  • with + secrets
Next up: Inputs and Secrets for Reusable Workflows — passing values in