TExcelTAD
Home › CI/CD Pipelines & Pipeline Security › Reusable Workflows & Composite Actions

Reusable Workflows & Composite Actions

A platform team that supports 40 services doesn't want 40 hand-maintained copies of "build, scan, push, deploy". GitHub Actions has two ways to share pipeline code:

  • a reusable workflow (on: workflow_call) shares whole jobs, including their runners, environments and approvals;
  • a composite action (runs: using: composite) shares steps that run inside the caller's job.

Release Engineering compares the two in a table and models how token permissions narrow down a chain of calls. Here we build the pair a DevOps team typically owns: a deploy workflow and a setup action.

A reusable deploy workflow

yaml
# acme/pipelines/.github/workflows/deploy.yml   (a shared repository)
name: deploy
on:
  workflow_call:
    inputs:
      environment:
        description: Target environment (staging or production)
        type: string
        required: true
      image:
        description: Image reference pinned by digest (registry/name@sha256:...)
        type: string
        required: true
    secrets:
      DEPLOY_TOKEN:
        required: true
    outputs:
      url:
        description: The deployed URL
        value: ${{ jobs.deploy.outputs.url }}

jobs:
  deploy:
    runs-on: ubuntu-24.04
    environment: ${{ inputs.environment }}       # the caller's environment rules apply here
    concurrency:
      group: deploy-${{ github.repository }}-${{ inputs.environment }}
      cancel-in-progress: false
    outputs:
      url: ${{ steps.deploy.outputs.url }}
    steps:
      - name: Refuse tags
        env:
          IMAGE: ${{ inputs.image }}
        run: |
          case "$IMAGE" in
            *@sha256:*) echo "deploying $IMAGE" ;;
            *) echo "::error::image must be pinned by digest"; exit 1 ;;
          esac
      - id: deploy
        env:
          IMAGE: ${{ inputs.image }}
          TARGET: ${{ inputs.environment }}
          DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}
        run: |
          ./deploy "$TARGET" "$IMAGE"      # placeholder for helm upgrade / kubectl / a cloud CLI
          echo "url=https://$TARGET.${GITHUB_REPOSITORY#*/}.example.com" >> "$GITHUB_OUTPUT"

Each service calls it, once per environment:

yaml
# .github/workflows/release.yml in acme/shop
jobs:
  build:
    uses: ./.github/workflows/build.yml          # a reusable workflow in the same repository
  staging:
    needs: build
    uses: acme/pipelines/.github/workflows/deploy.yml@v3
    with:
      environment: staging
      image: ${{ needs.build.outputs.image }}
    secrets: inherit                             # pass the caller's secrets through
  production:
    needs: [build, staging]
    uses: acme/pipelines/.github/workflows/deploy.yml@v3
    with:
      environment: production
      image: ${{ needs.build.outputs.image }}
    secrets: inherit

The "refuse tags" step is the point of sharing: one team enforces "deploy by digest" (and later "only signed images") for every service at once, by releasing v3.1.0 of the shared workflow.

A composite setup action

yaml
# acme/pipelines/actions/setup-tools/action.yml
name: Set up pipeline tools
description: Installs pinned versions of the scanners every pipeline uses
inputs:
  trivy-version:
    description: Trivy version
    default: '0.76.1'
runs:
  using: composite
  steps:
    - name: Install Trivy
      shell: bash                                  # required on every run step of a composite action
      env:
        VERSION: ${{ inputs.trivy-version }}
      run: |
        curl -fsSLo trivy.tgz "https://github.com/aquasecurity/trivy/releases/download/v${VERSION}/trivy_${VERSION}_Linux-64bit.tar.gz"
        grep "trivy_${VERSION}_Linux-64bit.tar.gz" "$GITHUB_ACTION_PATH/checksums.txt" | sha256sum -c -
        tar -xzf trivy.tgz -C "$RUNNER_TEMP" trivy
        echo "$RUNNER_TEMP" >> "$GITHUB_PATH"

Used as one step: - uses: acme/pipelines/actions/setup-tools@v3. $GITHUB_ACTION_PATH is the action's own folder, so it can ship files (here, the expected checksums) next to action.yml.

Caller rules: a checker

A job that calls a reusable workflow may only use a restricted set of keys: no runs-on, no steps. Mixing them is a common error, and GitHub rejects the workflow file. The model checks caller jobs the way a linter would:

python · try it yourself
CALLER_KEYS = {"name", "uses", "with", "secrets", "strategy", "needs", "if", "permissions", "concurrency"}

def check(jobs):
    for name, job in jobs.items():
        if "uses" in job:
            bad = sorted(set(job) - CALLER_KEYS)
            if bad:
                print(f"{name}: calls {job['uses']} but also sets {', '.join(bad)} (not allowed)")
                continue
            if job.get("secrets") not in (None, "inherit") and not isinstance(job.get("secrets"), dict):
                print(f"{name}: secrets must be a mapping or 'inherit'")
                continue
            print(f"{name}: ok (reusable workflow call)")
        elif "runs-on" not in job:
            print(f"{name}: a normal job needs runs-on")
        else:
            print(f"{name}: ok (normal job)")

check({
    "build":   {"uses": "./.github/workflows/build.yml"},
    "staging": {"needs": "build", "uses": "acme/pipelines/.github/workflows/deploy.yml@v3",
                "with": {"environment": "staging"}, "secrets": "inherit"},
    "broken":  {"uses": "acme/pipelines/.github/workflows/deploy.yml@v3", "runs-on": "ubuntu-24.04",
                "steps": [{"run": "echo hi"}]},
    "env-job": {"uses": "acme/pipelines/.github/workflows/deploy.yml@v3", "environment": "production"},
    "lint":    {"steps": [{"run": "make lint"}]},
})
build: ok (reusable workflow call)
staging: ok (reusable workflow call)
broken: calls acme/pipelines/.github/workflows/deploy.yml@v3 but also sets runs-on, steps (not allowed)
env-job: calls acme/pipelines/.github/workflows/deploy.yml@v3 but also sets environment (not allowed)
lint: a normal job needs runs-on

The env-job mistake is worth remembering: the called workflow declares the environment (from an input), not the caller.

Versioning shared pipelines

  • Tag releases with SemVer and keep a moving major tag (v3); callers that want stability pin a SHA with a version comment and let Dependabot or Renovate propose updates.
  • Shared workflows are a product: write a changelog and treat a renamed input as a breaking change.
  • Test them from a workflow in their own repository that calls them with sample inputs.
  • Visibility: a reusable workflow in a private repository can be called from other private repositories of the same organization only if the repository's Actions settings allow access.

Test yourself

You want every service's production deployment to require an approval and use one shared implementation. Which mechanism?

  1. A composite action
  2. A reusable workflow, whose job declares the environment
  3. A shell script in each repo
  4. A matrix

What is wrong with a calling job that has both uses: and runs-on:?

  1. Nothing
  2. A job that calls a reusable workflow can't define its own runner or steps
  3. runs-on must come first
  4. uses must be a SHA

What does secrets: inherit do?

  1. Creates new secrets
  2. Passes all the caller's secrets to the called workflow
  3. Makes secrets public
  4. Copies secrets to the shared repository