Sub topic 13
Testing the action in its own repository
This chapter assumes you can read a GitHub Actions workflow, know how uses and with work, and have already written a composite action with inputs and outputs. You will test an action by running it in a workflow inside the repository that holds it.
Why test in the same repository
A composite action only runs inside a workflow, so a workflow is the natural test harness. When the action lives in the same repository, you can call it with a relative path. That path always points at the commit being tested, so a pull request tests its own changes before anything is released.
Two rules apply to local actions:
- The repository must be checked out first. A local path such as
./.github/actions/greetdoes not exist on the runner untilactions/checkouthas run. - The path is relative to the workspace root and must start with
./.
A test workflow
The workflow below calls the greet action with an input, then checks the output. A failing test command makes the step fail, which fails the job.
name: Test greet action
on:
pull_request:
push:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- id: hello
uses: ./.github/actions/greet
with:
who-to-greet: Octocat
- name: Check the output
env:
GREETING: ${{ steps.hello.outputs.greeting }}
run: |
test "$GREETING" = "Hello, Octocat"The output is passed through env instead of being written into the script. This is the same injection-safe habit you use inside the action.
Testing more than one case
Use a matrix to run the action with several inputs. Each entry becomes its own job, so a failure names the exact case.
name: Test greet action matrix
on: [pull_request]
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
name: [Octocat, World]
steps:
- uses: actions/checkout@v4
- id: hello
uses: ./.github/actions/greet
with:
who-to-greet: ${{ matrix.name }}
- env:
GREETING: ${{ steps.hello.outputs.greeting }}
EXPECTED: Hello, ${{ matrix.name }}
run: test "$GREETING" = "$EXPECTED"Setting fail-fast: false lets every case finish, so you see all failures at once.
What to cover
- Defaults. Call the action with no
withblock and check that the default input value is used. - Optional steps. Run once with
run-tests: "false"and once with"true"to exercise eachifbranch. - Each operating system that you claim to support. The
shellyou chose may not exist everywhere. - Failure paths. Add
continue-on-error: trueand anidto the step, then assert thatsteps.<id>.outcomeequalsfailure.
Exercise
Your test workflow fails with "Can't find 'action.yml'" for uses: ./.github/actions/greet, although the file exists in the repository. What is the most likely cause, and how do you fix it?
Show answer
The repository was not checked out, so the path does not exist on the runner. Add - uses: actions/checkout@v4 as a step before the step that uses the local action.
Exercise
How can a test confirm that an action step fails when given bad input, without failing the whole job?
Show answer
Give the step an id and set continue-on-error: true. In a later step, compare steps.<id>.outcome with failure, and fail only if it differs. Pass the value through env and check it with test.