Use `after_script` to define an array of commands that run after each job,
- Single line commands.
including failed jobs.
- Long commands [split over multiple lines](script.md#split-long-commands).
-[YAML anchors](#yaml-anchors-for-scripts).
If a job times out or is cancelled, the `after_script` commands do not execute.
**Example of `after_script`:**
An [issue](https://gitlab.com/gitlab-org/gitlab/-/issues/15603) exists to support
executing `after_script` commands for timed-out or cancelled jobs.
```yaml
job:
script:
-echo "An example script section."
after_script:
-echo "Execute this command after the `script` section completes."
```
**Additional details**:
Scripts you specify in `after_script` execute in a new shell, separate from any
Scripts you specify in `after_script` execute in a new shell, separate from any
`before_script` or `script`scripts. As a result, they:
`before_script` or `script`commands. As a result, they:
- Have a current working directory set back to the default.
- Have a current working directory set back to the default.
- Have no access to changes done by scripts defined in `before_script` or `script`, including:
- Don't have access to changes done by commands defined in the `before_script` or `script`,
including:
- Command aliases and variables exported in `script` scripts.
- Command aliases and variables exported in `script` scripts.
- Changes outside of the working tree (depending on the runner executor), like
- Changes outside of the working tree (depending on the runner executor), like
software installed by a `before_script` or `script` script.
software installed by a `before_script` or `script` script.
- Have a separate timeout, which is hard coded to 5 minutes. See the
- Have a separate timeout, which is [hard-coded to 5 minutes](https://gitlab.com/gitlab-org/gitlab-runner/-/issues/2716).
[related issue](https://gitlab.com/gitlab-org/gitlab-runner/-/issues/2716) for details.
- Don't affect the job's exit code. If the `script` section succeeds and the
- Don't affect the job's exit code. If the `script` section succeeds and the
`after_script` times out or fails, the job exits with code `0` (`Job Succeeded`).
`after_script` times out or fails, the job exits with code `0` (`Job Succeeded`).
```yaml
If a job times out or is cancelled, the `after_script` commands do not execute.
default:
[An issue exists](https://gitlab.com/gitlab-org/gitlab/-/issues/15603) to add support for executing `after_script` commands for timed-out or cancelled jobs.
after_script:
-echo "Execute this script in all jobs that don't already have an after_script section."
job1:
script:
-echo "This script executes first. When it completes, the global after_script executes."
job:
script:
-echo "This script executes first. When it completes, the job's `after_script` executes."
after_script:
-echo "Execute this script instead of the global after_script."
```
You can use [YAML anchors with `after_script`](#yaml-anchors-for-scripts).
#### Script syntax
You can use syntax in [`script`](#script) sections to:
**Related topics**:
-[Split long commands](script.md#split-long-commands) into multiline commands.
-[Use `after_script` with `default`](script.md#set-a-default-before_script-or-after_script-for-all-jobs)
-[Use color codes](script.md#add-color-codes-to-script-output) to make job logs easier to review.
to define a default array of commands that should run after all jobs.
- You can [ignore non-zero exit codes](script.md#ignore-non-zero-exit-codes).
-[Use color codes with `after_script`](script.md#add-color-codes-to-script-output)