Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 242CI/CD · GitHub

How to reuse workflows in GitHub Actions

Twenty repositories each have their own copy of the same build-and-deploy workflow; a security fix means twenty pull requests, and half are forgotten. A reusable workflow lives once in a central repository; every project calls it with a few inputs and secrets. For smaller pieces (a few steps) a composite action does the same. This how-to writes a reusable workflow, calls it, passes secrets safely, and pins versions.

  • 5 Lessen
  • 3 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

242.1

The quick answer

platform/.github/workflows/build-image.yml
name: build-image
on:
  workflow_call:
    inputs:
      image:
        type: string
        required: true
      context:
        type: string
        default: "."
    secrets:
      registry-token:
        required: true
    outputs:
      digest:
        value: ${{ jobs.build.outputs.digest }}
 
jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
    outputs:
      digest: ${{ steps.push.outputs.digest }}
    steps:
      - uses: actions/checkout@v4
      - uses: docker/login-action@v3
        with:
          registry: registry.example.com
          username: ci
          password: ${{ secrets.registry-token }}
      - id: push
        uses: docker/build-push-action@v6
        with:
          context: ${{ inputs.context }}
          push: true
          tags: registry.example.com/${{ inputs.image }}:${{ github.sha }}
shop-api/.github/workflows/ci.yml
on: { push: { branches: [main] } }
jobs:
  image:
    uses: example-org/platform/.github/workflows/build-image.yml@v1.2.0
    with:
      image: shop/api
    secrets:
      registry-token: ${{ secrets.REGISTRY_TOKEN }}

242.2

Step by step

  1. Central repository — for example example-org/platform. Settings → Actions → Access: allow use from repositories in the organization (for private repos).
  2. **on: workflow_call** (first file) — defines inputs (typed), secrets and outputs. It is a normal workflow otherwise, with jobs and permissions.
  3. Call it (second file) — at job level with uses: OWNER/REPO/.github/workflows/FILE.yml@REF. Not as a step.
  4. Secrets — pass only what is needed, by name. secrets: inherit passes all of the caller's secrets — easy, but gives the shared workflow more than it needs; use it only for trusted internal workflows.
  5. Pin the version — call a tag (@v1.2.0) or a commit SHA, not @main; a change in the platform repo then reaches projects only when they update. Release the platform repo with tags and notes.
  6. Use the outputs — a later job in the caller: needs: image and ${{ needs.image.outputs.digest }}.
  7. Composite actions for steps — when you only share a few steps (setup + lint), write action.yml with runs: using: composite and call it as a step (uses: example-org/platform/actions/lint@v1).
  8. Limits — reusable workflows can be nested only a few levels deep, and environment variables of the caller are not passed (use inputs).

242.3

Check that it worked

The caller's run shows the job image / build from the platform repo; the image is pushed with the commit tag; the digest output is visible to the next job; changing the platform repo does not affect callers until they bump the tag.

242.4

Common problems

ProblemCauseFix
workflow was not foundWrong path/ref, or access from other repos not allowed.Check the path and tag; allow access in the platform repo settings.
Secret is empty in the called workflowNot passed (secrets are not inherited by default).Pass it in secrets: or use inherit on purpose.
uses at step level failsReusable workflows are called per job.Move uses: to the job; for steps write a composite action.
Env vars missingCaller env is not passed.Use inputs.
All projects broke at onceCallers use @main.Pin to tags/SHAs; release the platform repo.
Permission errorsCalled job has fewer permissions than needed.Set permissions in the called job; the caller can only lower them.

Commando's in deze les

242.5

Go further

1Where do you call a reusable workflow — at job or step level?

At job level, with uses: OWNER/REPO/.github/workflows/FILE.yml@REF.

2Why pin a reusable workflow to a tag or SHA?

Changes in the central repository then reach a project only when it updates the version on purpose.

Commando's in deze les

Alle commando's in dit hoofdstuk