Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
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 }}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 }}Step by step
- Central repository — for example
example-org/platform. Settings → Actions → Access: allow use from repositories in the organization (for private repos). - **
on: workflow_call** (first file) — defines inputs (typed), secrets and outputs. It is a normal workflow otherwise, with jobs and permissions. - Call it (second file) — at job level with
uses: OWNER/REPO/.github/workflows/FILE.yml@REF. Not as a step. - Secrets — pass only what is needed, by name.
secrets: inheritpasses all of the caller's secrets — easy, but gives the shared workflow more than it needs; use it only for trusted internal workflows. - 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. - Use the outputs — a later job in the caller:
needs: imageand${{ needs.image.outputs.digest }}. - Composite actions for steps — when you only share a few steps (setup + lint), write
action.ymlwithruns: using: compositeand call it as a step (uses: example-org/platform/actions/lint@v1). - Limits — reusable workflows can be nested only a few levels deep, and environment variables of the caller are not passed (use inputs).
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.
Common problems
| Problem | Cause | Fix |
|---|---|---|
workflow was not found | Wrong 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 workflow | Not passed (secrets are not inherited by default). | Pass it in secrets: or use inherit on purpose. |
uses at step level fails | Reusable workflows are called per job. | Move uses: to the job; for steps write a composite action. |
| Env vars missing | Caller env is not passed. | Use inputs. |
| All projects broke at once | Callers use @main. | Pin to tags/SHAs; release the platform repo. |
| Permission errors | Called job has fewer permissions than needed. | Set permissions in the called job; the caller can only lower them. |
Go further
- Command pages: gh, git.
- Next how-tos: Build and push an image in CI, Test on many versions with a CI matrix, Run GitHub Actions locally with act, Use secrets safely in GitHub Actions.
- Learn it: GitHub Actions — Reusable workflows, GitHub Actions — Composite actions, GitHub Actions — Pin actions.
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.