Sub topic 8
Shipping scripts with the action
Inline run blocks work for short commands. Once a step grows past a few lines, a separate script is easier to read, lint, and test. A composite action can ship scripts in its own directory and call them from its steps. This chapter assumes you can write an action.yml with inputs, outputs, and shell on every run step, as covered earlier.
Why a script path is not obvious
A composite action's steps run inside the caller's job. The job's working directory is the caller's workspace, usually the checked-out caller repository. A relative path such as ./scripts/check.sh therefore points into the caller's repository, not into the action's directory.
The action's files are downloaded to a separate location on the runner. You don't know that location in advance, so the workflow runtime exposes it through the github.action_path context value. It is the directory that contains the action.yml of the action that is currently running.
Layout of an action with scripts
Keep the scripts next to action.yml. A typical layout for a local action looks like this:
.github/actions/check/
├── action.yml
└── scripts/
└── check.shFor an action in its own repository, action.yml sits at the repository root and scripts/ sits beside it. The github.action_path value works the same way in both cases.
Calling a bundled script
The step below runs a script from the action directory:
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"Three details matter here.
- The
${{ }}expression is expanded before the shell starts. The shell sees a plain absolute path, such as/home/runner/work/_actions/my-org/my-action/v1/scripts/check.sh. - The path is quoted. A runner path normally has no spaces, but quoting is a habit that protects you on other runners.
- The script is run with
bash script.shrather than./script.sh. This avoids a dependency on the executable bit, which a checkout or download can drop.
The executable bit
If you want to run the script directly, the file needs the executable permission recorded in Git. Set it once and commit it:
git update-index --chmod=+x scripts/check.sh
git add scripts/check.sh
git commit -m "Make check.sh executable"If you skip this, ./scripts/check.sh can fail with Permission denied. Calling the interpreter explicitly, as in bash "$path", sidesteps the problem entirely. Many authors do both.
Passing data into the script
A script has three ordinary ways to receive data from the action.
- Command-line arguments.
- Environment variables set on the step with
env. - Files, such as one the script reads from the workspace.
Environment variables are the safest default for values that come from inputs. An input is controlled by the caller, so interpolating it directly into the command line can let a crafted value inject shell syntax. The injection chapter covers this in detail. Here, the pattern is simply to map each input to an env entry and read it in the script.
name: Check files
description: Run a bundled script against a directory
inputs:
target-dir:
description: Directory to check, relative to the workspace
required: false
default: "."
max-size-kb:
description: Largest allowed file size in kilobytes
required: false
default: "512"
outputs:
large-files:
description: Number of files over the size limit
value: ${{ steps.scan.outputs.large-files }}
runs:
using: composite
steps:
- id: scan
shell: bash
env:
TARGET_DIR: ${{ inputs.target-dir }}
MAX_SIZE_KB: ${{ inputs.max-size-kb }}
run: bash "${{ github.action_path }}/scripts/scan.sh"The matching script reads those variables and writes an output. A script is not part of the YAML, so it can use $GITHUB_OUTPUT directly, just like an inline step:
#!/usr/bin/env bash
set -euo pipefail
target_dir="${TARGET_DIR:-.}"
max_size_kb="${MAX_SIZE_KB:-512}"
if [[ ! -d "$target_dir" ]]; then
echo "::error::Directory not found: $target_dir"
exit 1
fi
count=$(find "$target_dir" -type f -size +"${max_size_kb}k" | wc -l | tr -d ' ')
echo "Found ${count} file(s) larger than ${max_size_kb} KB in ${target_dir}"
echo "large-files=${count}" >> "$GITHUB_OUTPUT"Two things to notice. The script validates its own input and fails with an ::error:: annotation, which appears in the run summary. And the output is written to $GITHUB_OUTPUT, so the step with id: scan exposes large-files, which the action then maps to its own output.
Which directory does the script work in?
Because the step runs in the caller's workspace, the script's current directory is the caller's workspace. This is usually what you want. The script scans the caller's files, and the script itself lives elsewhere.
Relative paths passed through inputs, such as target-dir above, resolve against the workspace. If the script also needs to read a helper file that ships with the action, such as a config template, build that path from github.action_path:
template="${ACTION_PATH}/templates/default.json"To make ACTION_PATH available, pass it with env:
- shell: bash
env:
ACTION_PATH: ${{ github.action_path }}
run: bash "$ACTION_PATH/scripts/render.sh"This is useful when a script calls other scripts, since it can compute its own location without the github context. Inside a Bash script you can also derive it from the script file itself:
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$script_dir/lib/common.sh"Edge cases and common errors
- Using a relative path.
run: bash ./scripts/check.shworks when you test the action inside its own repository with a checkout, because the workspace happens to containscripts/. It then fails in every other repository. Always usegithub.action_path. - Using
github.workspacefor bundled files. The workspace is the caller's repository, not the action's. It only contains your scripts if the action is run in its own repository. - Using
github.action_pathin a non-step field. Thegithubcontext is available in steprun,env, andwithvalues. Do not rely on it elsewhere inaction.yml. - Forgetting the shell. A script-calling step is still a
runstep, so it needsshell. - Windows runners. Bash is available on Windows runners through Git Bash, so
shell: bashworks, butgithub.action_paththen contains backslashes in the expression output. Quoting the path and callingbashhandles this in most cases. If you need to support Windows, test on it. - Missing
set -euo pipefail. Without it, a failed command in the middle of a script can be ignored, and the step reports success. - Line endings. A script saved with Windows (CRLF) line endings fails with errors such as
\r: command not found. Add a.gitattributesrule such as*.sh text eol=lf.
Mixing languages
The bundled file does not have to be Bash. A step can call any interpreter that exists on the runner:
runs:
using: composite
steps:
- shell: bash
env:
REPORT_PATH: ${{ inputs.report-path }}
run: python3 "${{ github.action_path }}/scripts/summarize.py"Hosted runners include Python and Node.js, but self-hosted runners may not. If the action depends on a tool, either install it in an earlier step (for example with actions/setup-python) or document the requirement. Installing a language is also the point at which a JavaScript action starts to look like a better fit, since it bundles its own code and uses the runner's Node runtime.
A short checklist
- Put scripts in a subdirectory next to
action.yml. - Reference them with
${{ github.action_path }}in a quoted path. - Pass inputs to scripts through
env, not through string interpolation in the command. - Start every Bash script with
set -euo pipefail. - Write outputs to
$GITHUB_OUTPUTand map them in theoutputs:section. - Record the executable bit in Git or call the interpreter explicitly.
Exercise
A composite action in my-org/lint-action contains scripts/lint.sh. Its step uses run: bash ./scripts/lint.sh. The action passes in its own repository, but fails with "No such file or directory" when used from my-org/web-app. What is wrong, and what is the corrected run line?
Show answer
The relative path resolves against the caller's workspace, which in web-app has no scripts/lint.sh. It only worked in the action's repository because the checkout put the same file in the workspace. Use the action's directory instead: run: bash "${{ github.action_path }}/scripts/lint.sh".
Exercise
Your action accepts an input branch-name and a step runs run: bash "${{ github.action_path }}/scripts/publish.sh" ${{ inputs.branch-name }}. Rewrite the step so the input cannot inject shell syntax, and say how the script reads the value.
Show answer
Map the input to an environment variable and keep the command fixed: add env: BRANCH_NAME: ${{ inputs.branch-name }} to the step and use run: bash "${{ github.action_path }}/scripts/publish.sh". The step also needs shell: bash. Inside the script, read "$BRANCH_NAME", quoted, so the value is treated as data and never parsed as shell code.