Sub topic 12
Caller context, environments and what is not inherited
This chapter assumes you can write a workflow_call workflow, pass inputs and secrets, and call it with jobs.<job_id>.uses. It covers which values a called workflow sees from the caller, and which it does not.
The called workflow runs in the caller's context
A called workflow does not run as its own repository's event. It runs as part of the caller's run. The github context therefore describes the caller:
github.repositoryis the calling repository.github.event_nameis the caller's event, such aspushorpull_request. It is neverworkflow_call.github.sha,github.refandgithub.eventcome from the caller's event.actions/checkoutwith no arguments checks out the calling repository, not the repository that holds the shared workflow.
# .github/workflows/context-demo.yml
name: Context demo
on:
workflow_call:
env:
LOCAL_FLAG: set-in-called-workflow
jobs:
show:
runs-on: ubuntu-latest
steps:
- run: |
echo "repo: ${{ github.repository }}"
echo "event: ${{ github.event_name }}"
echo "ref: ${{ github.ref }}"
echo "local: $LOCAL_FLAG"
echo "caller env: ${CALLER_FLAG:-<empty>}"If a caller on pull_request in my-org/app calls this workflow, the output shows my-org/app and pull_request. It also shows caller env: <empty>, even when the caller sets CALLER_FLAG in its own top-level env.
What is and is not inherited
| Item | Reaches the called workflow? |
|---|---|
| `github` context (repository, event, ref, SHA) | Yes, it is the caller's |
| Workflow-level `env` in the caller | No. Pass the value as an input. |
| `env` defined inside the called workflow | Yes, it works inside that workflow only |
| Secrets | Only if passed by name or with `secrets: inherit` |
| `vars` (configuration variables) | Yes, resolved from the caller's repository and organization |
| `GITHUB_TOKEN` permissions | Only as passed down from the caller. A called workflow can reduce them but not raise them. |
| Calling-job settings such as `runs-on` | Not applicable. The called workflow chooses its own runners. |
Environments
A job that calls a reusable workflow cannot set environment:. The calling job accepts only a small set of keys: name, uses, with, secrets, strategy, needs, if, permissions and concurrency.
Declare the environment on a job inside the called workflow instead:
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- run: echo "Deploying ${{ github.sha }}"GitHub looks up production in the calling repository. Its protection rules, such as required reviewers, apply, and its environment secrets are available to that job. To let callers choose the environment, add a string input and use environment: ${{ inputs.environment }}.
Environment secrets cannot be passed through the calling job's secrets: block, because the calling job has no environment. Define the environment inside the called workflow when the job needs them.
Common mistakes
- Using
github.repositoryto mean the shared-workflow repository. It is always the caller. To check out the shared repository, useactions/checkoutwithrepository: my-org/shared-workflowsand an explicitref. - Setting
envat the top of the caller and expecting the called steps to read it. Convert it to an input. - Testing for
github.event_name == 'workflow_call'. This is never true. - Adding
runs-onorstepsto a calling job. The workflow fails validation.
Exercise
A caller sets env: DEPLOY_REGION: eu-west-1 at the top of its workflow and calls a shared workflow whose step runs echo $DEPLOY_REGION. The step prints an empty line. Why, and what is the fix? Also, where should you declare environment: production so that its required reviewers apply?
Show answer
Workflow-level env in the caller is not propagated to a called workflow. Fix it by adding a deploy-region string input to the called workflow, passing with: deploy-region: eu-west-1 from the caller, and reading ${{ inputs.deploy-region }} in the step. Declare environment: production on a job inside the called workflow, because a calling job cannot set environment. GitHub resolves the environment in the calling repository.