Home DevOpsGitHub Actions Reusable Workflows: Share CI Steps Safely

GitHub Actions Reusable Workflows: Share CI Steps Safely

by Knowledge Base Linux
GitHub Actions reusable workflows shared CI template

GitHub Actions reusable workflows solve the copy-paste problem that every growing CI setup hits: the same test, build, and deploy steps duplicated across ten repositories, drifting apart every time someone fixes a bug in one copy and forgets the other nine. A reusable workflow is a workflow file that other workflows call instead of duplicating, with declared inputs, outputs, and secrets. Fix the reusable workflow once, and every caller gets the fix. This guide builds a reusable deploy workflow, calls it from two different repositories, passes inputs and secrets safely, and covers the limits that catch first-time users.

The mental model is simple: the reusable workflow is a function, the caller is the call site, and workflow_call is the function signature. The reusable workflow declares what it accepts, the caller supplies values, and the reusable workflow returns outputs the caller can use in later jobs. That analogy also explains the main limitation: a reusable workflow cannot read the caller repository files directly, and the caller cannot reach into the reusable workflow internals. Everything crosses the boundary through declared inputs, outputs, and secrets, which is exactly what makes reuse safe to reason about. If your CI is still a single workflow per repository, our guide to GitHub Actions caching for faster builds is a good companion optimisation once reuse is in place.

GitHub Actions reusable workflows shared module in pipelines

Prerequisites

You need a GitHub account, a repository where the reusable workflow will live, and at least one caller repository, plus permission to add workflow files and secrets to both. You should be comfortable writing a basic GitHub Actions workflow with jobs, steps, and the checkout action. The examples use a reusable workflow that builds a Docker image and deploys it, but the pattern works for tests, linting, releases, and any repeated job sequence. Note that reusable workflows must live in the .github/workflows directory of their repository, and a caller in the same repository references them by path, while a caller in another repository references them by repository and ref.

GitHub Actions Reusable Workflows: Build the Reusable Workflow

Create a workflow file that triggers on workflow_call instead of push or pull_request. Declare inputs with types and defaults, declare secrets explicitly, and declare outputs that later jobs in the caller can read. The reusable workflow below accepts an environment name and an image tag, builds and pushes an image, and outputs the image digest. Keep the reusable workflow focused on one job: a workflow that tries to do everything becomes as hard to maintain as the duplicated code it replaced.

name: Reusable deploy
on:
  workflow_call:
    inputs:
      environment:
        required: true
        type: string
      image-tag:
        required: false
        type: string
        default: latest
    secrets:
      registry-password:
        required: true
    outputs:
      image-digest:
        value: ${{ jobs.deploy.outputs.digest }}
jobs:
  deploy:
    runs-on: ubuntu-latest
    outputs:
      digest: ${{ steps.build.outputs.digest }}
    steps:
      - uses: actions/checkout@v4
      - name: Build and push image
        id: build
        run: |
          echo "Deploying to ${{ inputs.environment }} with tag ${{ inputs.image-tag }}"

Two details deserve attention. First, inputs in a reusable workflow are accessed with the inputs context, not the github context, and they are typed: a boolean input is a real boolean, while an untyped value would arrive as a string and surprise a conditional. Second, secrets are never available as inputs, and they are not printed in logs even if a step echoes them, because GitHub masks secret values. That masking is a safety net, not a permission model: only pass a secret to a reusable workflow you trust, because the workflow code runs with that secret in its environment. The official GitHub documentation on reusing workflows lists the full input, secret, and output syntax, including environment and concurrency controls.

GitHub Actions Reusable Workflows: Call It from Another Workflow

A caller workflow is short, and that brevity is the point. It declares a job whose only step is uses pointing at the reusable workflow, passes inputs, and passes secrets by name. For a caller in the same repository, use a relative path. For a caller in a different repository, use the repository, path, and ref form, and pin the ref to a tag or commit SHA in production rather than a moving branch, so a change to the reusable workflow cannot silently change every caller pipeline.

name: Deploy production
on:
  push:
    branches: [main]
jobs:
  call-deploy:
    uses: myorg/shared-workflows/.github/workflows/deploy.yml@v1
    with:
      environment: production
      image-tag: ${{ github.sha }}
    secrets:
      registry-password: ${{ secrets.REGISTRY_PASSWORD }}

Read outputs from the called workflow through the jobs context in a later job that declares a dependency with needs. That later job can, for example, post the image digest to a deployment record or run a smoke test against the deployed service. Our guide to deploying to a VPS with GitHub Actions over SSH shows a caller-side pattern for the deployment step itself, which you can wrap in a reusable workflow once the manual version works.

  report:
    needs: call-deploy
    runs-on: ubuntu-latest
    steps:
      - run: echo "Deployed digest ${{ needs.call-deploy.outputs.image-digest }}"

Secrets handling has one shortcut and one warning. The shortcut is secrets: inherit, which passes all caller secrets to the reusable workflow without naming each one, and it is convenient for a trusted workflow in the same organisation. The warning is that inherit passes secrets the reusable workflow does not need, which violates least privilege and makes auditing harder. Prefer explicit secrets for cross-team and public reusable workflows, and reserve inherit for a small set of workflows owned by the same team.

Troubleshooting

When a caller fails with an error about a missing input or secret, compare the caller with block against the workflow_call declaration character by character: input names use hyphens in the examples above, and a caller passing image_tag instead of image-tag will fail validation rather than silently using a default. When a reusable workflow fails with a permissions error, check the permissions of the caller job, because the reusable workflow inherits the caller permissions unless it declares its own, and a job that only has read access cannot push an image no matter what the reusable workflow requests.

When outputs arrive empty, check two places: the reusable workflow job must declare outputs mapping to step outputs, and the workflow_call block must declare outputs mapping to job outputs. Missing either mapping breaks the chain silently, because GitHub does not error on an undeclared output, it just returns nothing. When a cross-repository call fails with a not-found error, check the ref: a tag that was moved or deleted, or a branch that does not contain the workflow file, produces the same error as a typo in the repository name. Finally, remember the nesting limit: a reusable workflow can call another reusable workflow, but the chain is limited to four levels, so flatten deep chains before they hit that ceiling in production.

Conclusion

GitHub Actions reusable workflows pay off the moment a second repository needs the same pipeline: declare inputs, outputs, and secrets in the reusable workflow, call it with a pinned ref, pass secrets explicitly, and read outputs through needs. The discipline is in the boundary: everything a caller needs crosses through declared values, nothing leaks through shared files or implicit state. Start with one workflow, the one you have already copied three times, and let the savings from that single extraction fund the rest of the migration.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.