After

When you specify dependencies using the use keyword, the task inherits the file system contents and environment variables of its dependencies.

If you want to run tasks after other tasks without inheriting the output of the dependencies, you should use the after keyword instead.

Common reasons to use after include:

  • Continuous Deployment workflows, where tasks modify external resources and need to run in a specific order
  • ETL workflows, where state is stored externally and tasks need to run in a specific order
  • Optimizing build minutes by making sure some tasks succeed before running others

Example

tasks:
  - key: one
    run: echo this is task one

  - key: two
    run: echo this is task two

  - key: three
    after: [one, two]
    run: echo "task three waits for one and two to succeed before running"

When after is a list, the task runs only if every task in the list succeeds.

Referencing tasks in after expressions

To run a task based on how other tasks ended, set after to an expression instead of a list. The expression checks the status attributes of other tasks, and can combine them with && and ||.

tasks:
  - key: one
    run: exit 1

  - key: if-one-fails
    after: ${{ one.failed }}
    run: echo task one failed!

These expressions cover the common cases:

  • ${{ example-task.finished }} runs after example-task fully executes, regardless of if it succeeded or failed. If example-task doesn't finish (for example, if it is cancelled), then this task will be skipped.
  • ${{ example-task.failed && example-task.finished }} runs only if the task executed and then failed, for example by exiting with a non-zero status.

A task is skipped if any task in its use doesn't succeed, even when its after expression is true. If you might need data from a task even when it fails, such as a coverage report, make sure to save that output as an artifact.

after expressions work differently from other expressions. They can only reference tasks, and they reference them by their bare key, such as ${{ one.failed }}, rather than through the tasks context. Other contexts, such as init, vars, and secrets, aren't available in after expressions.

tasks:
  - key: lint
    run: npm run lint

  - key: test
    run: npm test

  - key: report-test-failure
    after: ${{ lint.succeeded && test.failed }}
    run: echo lint passed but tests failed

For each task, after expressions can only check status attributes. To read a task's output values, artifacts, or other data, use the tasks context in another field, such as env.

To check the status of tasks inside a local package, see checking local package task status.

Status attributes

Each task in an after expression has the following attributes. ✅ marks the attributes that are true for each way a task can end.

outcomesucceededfailedfinishedcache-hitabortedcancelledtimed-outskipped
Executed and succeeded✅✅
Executed and failed✅✅
Cache hit✅✅✅
Cancelled✅✅✅
Timed out✅✅✅
Aborted for another reason✅✅
Skipped✅