Sub topic 14
Sharing deployment logic with reusable workflows
This chapter assumes you can read a workflow file and know how jobs, environments, and needs work. You should also have seen a deploy job like the one from the previous chapters.
Why reuse a workflow
Most teams deploy the same way to staging and production. Only the environment name, and sometimes a few settings, change. If you copy the deploy job into two files, a fix in one file is easy to forget in the other.
A reusable workflow is a workflow file that other workflows can call, much as code calls a function. The file declares inputs, and the caller passes values to them.
The called workflow
A workflow becomes reusable when its trigger is workflow_call. Inputs go under that trigger.
# .github/workflows/deploy-reusable.yml
name: Reusable deploy
on:
workflow_call:
inputs:
environment:
required: true
type: string
jobs:
deploy:
runs-on: ubuntu-latest
environment: ${{ inputs.environment }}
steps:
- run: echo "Deploying to ${{ inputs.environment }}"The environment key takes the input value. Each call therefore uses the secrets, variables, and approval rules of the environment it names. A production call waits for reviewers, and a staging call does not.
Each input needs a type: string, boolean, or number. Mark an input required: true when the workflow cannot run without it.
The calling workflow
A caller uses a whole workflow as a job. It sets uses on the job instead of runs-on and steps.
# .github/workflows/release.yml
name: Release
on:
push:
branches: [main]
jobs:
staging:
uses: ./.github/workflows/deploy-reusable.yml
with:
environment: staging
production:
needs: staging
uses: ./.github/workflows/deploy-reusable.yml
with:
environment: productionThe path ./.github/workflows/... points to a file in the same repository. To use a workflow from another repository, write owner/repo/.github/workflows/file.yml@ref. The ref is a branch, tag, or commit SHA. Pinning to a tag or SHA keeps a change in the shared repository from altering your deployments unexpectedly.
Because production lists needs: staging, it starts only after the staging call succeeds.
Secrets and permissions
A called workflow does not automatically receive the caller's secrets. You have two options:
- Declare secrets under
on.workflow_call.secretsand pass them withsecrets:in the caller. - Write
secrets: inheritin the caller to pass all of its secrets. This works within the same organization or enterprise.
Secrets stored in an environment need no passing. The called job reads them because it names that environment.
Permissions follow a similar rule. The called workflow can narrow the GITHUB_TOKEN permissions it receives, but it cannot raise them above what the caller grants. If the deploy needs id-token: write for OIDC, the caller must allow it, either at the workflow level or on the calling job.
Common errors
- Workflow not found. The path is wrong, or the
@refdoes not exist. Local calls must not include a ref. - Calling file is invalid. The called file lacks
workflow_callin itsonblock. - Missing secret. The caller did not pass it, and it is not stored in the environment.
- OIDC fails inside the called workflow. The caller did not grant
id-token: write. - Extra keys on the calling job. A job that uses
usescannot also havestepsorruns-on.
Exercise
You call a reusable deploy workflow with environment: production. The environment requires a reviewer. At what point does the run pause, and why?
Show answer
The run pauses when the deploy job inside the called workflow is ready to start. That job sets environment: ${{ inputs.environment }}, which resolves to production, so GitHub applies that environment's protection rules and waits for approval before running the job's steps.