Skip to content
Latchkey

How to Source a Shared Shell Library in GitHub Actions

Source a committed shell library at the top of a run step so its functions are available, since sourced state does not carry to the next step.

Keep helpers in a file like scripts/lib.sh, then source it inside the run block that uses them. Because each step is a fresh shell, source the library in every step that needs it.

Steps

  • Commit reusable functions to scripts/lib.sh.
  • source ./scripts/lib.sh at the start of the run block.
  • Call the functions within that same step.

Workflow

.github/workflows/ci.yml
steps:
  - uses: actions/checkout@v4
  - run: |
      set -euo pipefail
      source ./scripts/lib.sh
      log_info "Starting build"
      run_build
      log_info "Done"

Gotchas

  • Functions sourced in one step are gone in the next; re-source the library each time.
  • The library must be present, so run actions/checkout before sourcing it.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →