jobs.<job_id>.vars

2 minute read

When a job calls a reusable workflow using uses:, you can optionally set jobs.<job_id>.vars to inherit to make the caller job’s variables available to the reusable workflow.

In the following example:

  • caller-job invokes a reusable workflow with uses:.

  • vars: inherit exposes all vars in the caller job’s scope to the reusable workflow (including any environment-scoped vars that apply to the caller job).

  • The caller also passes env-prod as an input so the reusable workflow can select an environment for its own jobs.

  • Inside the reusable workflow, a job may set its environment from the input; if that environment defines vars with the same names, those values take precedence.

Caller workflow
jobs: caller-job: uses: .cloudbees/workflows/a-reusable-workflow.yaml (1) vars: inherit (2) inputs: var-1: ${{ vars.var-A }} (3) env-prod: production (4)
1 Calls a reusable workflow from the current repository.
2 Inherits all caller vars into the reusable workflow’s context.
3 Optionally passes a specific value via inputs (useful if you want an explicit parameter alongside inheritance).
4 Provides the environment name for the reusable workflow to use.
Reusable workflow
on: workflow_call: inputs: var-1: type: string required: false env-prod: type: string required: false jobs: job-rw-1: steps: - name: Read inherited vars run: | if [ -n "${{ vars.var-A }}" ]; then echo "var-A available via inheritance"; fi if [ -n "${{ inputs.var-1 }}" ]; then echo "var-1 also provided via inputs"; fi job-rw-2: environment: ${{ inputs.env-prod }} (1) steps: - name: Environment-scoped vars may override run: | # If the selected environment defines var-A, it overrides the inherited value. if [ -n "${{ vars.var-A }}" ]; then echo "var-A resolved (environment may override)"; fi
1 Sets the job’s environment from the input; environment-scoped vars are resolved for this job and can override inherited values with the same names.
  • vars: inherit is compatible with passing inputs via with: when calling a reusable workflow; with: parameters are separate from vars and do not conflict.

  • If a job in the reusable workflow sets its environment using an expression (for example, environment: ${{ inputs.env-prod }}), the environment name is resolved first, then environment-scoped vars are evaluated for that job.

  • Use vars: inherit only with reusable workflows you trust, since all caller vars (including those applicable from the caller’s scope) become available to the called workflow.