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.shat 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/checkoutbefore sourcing it.
Related guides
How to Share a Script Across Jobs in GitHub ActionsReuse the same script in multiple GitHub Actions jobs by committing it once and checking out the repo in each…
How to Run a Shell Script From the Repo in GitHub ActionsRun a committed shell script in GitHub Actions by checking out the repo, making the file executable, and invo…