Sub topic 8
Shipping scripts with the action
This chapter assumes you can write a composite action's action.yml, declare inputs, and give every run step a shell. Here you will move longer logic out of inline YAML and into script files that ship with the action.
Why bundle scripts
Inline run blocks become hard to read once they pass a few lines. A script file can be linted, run locally, and tested on its own. You keep it in the action's directory, so every repository that uses the action gets the same copy.
A typical layout looks like this:
.github/actions/check/
action.yml
scripts/
check.shThe path problem
A composite action runs inside the caller's job. The job's working directory is the caller's checked-out repository, not the action's directory. A step that runs bash scripts/check.sh looks for the file in the caller's repository and fails.
The github.action_path context value fixes this. It holds the path to the directory that contains the running action's files. It works whether the action lives in the same repository or is fetched from another one.
name: Run bundled check
description: Run a script that ships inside the action's own directory
runs:
using: composite
steps:
- shell: bash
run: bash "${{ github.action_path }}/scripts/check.sh"Calling the script through bash also avoids a second problem. A file's executable bit can be lost, depending on how it was committed. Running bash path/to/script.sh does not need that bit.
Passing data to the script
Do not paste input values into the script path or the command line through ${{ }}. Pass them as environment variables and read them in the script. The injection chapter covers the reasons. For now, follow the pattern.
name: Run bundled check
description: Check a target directory with a bundled script
inputs:
target:
description: Directory to check
required: false
default: "."
runs:
using: composite
steps:
- shell: bash
env:
TARGET: ${{ inputs.target }}
run: bash "${{ github.action_path }}/scripts/check.sh"The script then reads TARGET:
#!/usr/bin/env bash
set -euo pipefail
if [ ! -d "${TARGET}" ]; then
echo "Directory not found: ${TARGET}" >&2
exit 1
fi
echo "Checking ${TARGET}"The set -euo pipefail line makes the script stop on the first error, on unset variables, and on failures inside pipes. A failing script then fails the step, as you want.
Common errors
- Using a relative path.
bash scripts/check.shpoints at the caller's repository. Usegithub.action_path. - Expecting
action_pathinenvat the action level. Thegithubcontext is available in steprunandenvvalues. Use it there. - Forgetting to check out the caller. For a local action (
./.github/actions/check), the workflow must runactions/checkoutfirst. Otherwise the action files do not exist on the runner. - Relying on the executable bit. Call scripts with
bashexplicitly.
Exercise
A composite action has the file scripts/lint.sh next to its action.yml. A step runs bash scripts/lint.sh. It works when the action is called from the action's own repository but fails from other repositories. Why, and what is the fix?
Show answer
The relative path resolves against the caller's workspace, which only happens to contain scripts/lint.sh in the action's own repository. Use bash "${{ github.action_path }}/scripts/lint.sh" so the path points to the action's directory.