Commit 11220743 authored by Marcel Amirault's avatar Marcel Amirault Committed by Suzanne Selhorn

Revamp script, before_script and after_script reference docs

parent 9e44919c
...@@ -684,142 +684,130 @@ For more information, see [Available settings for `services`](../services/index. ...@@ -684,142 +684,130 @@ For more information, see [Available settings for `services`](../services/index.
### `script` ### `script`
Use `script` to specify a shell script for the runner to execute. Use `script` to specify commands for the runner to execute.
All jobs except [trigger jobs](#trigger) require a `script` keyword. All jobs except [trigger jobs](#trigger) require a `script` keyword.
For example: **Keyword type**: Job keyword. You can use it only as part of a job.
```yaml **Possible inputs**: An array including:
job:
script: "bundle exec rspec"
```
You can use [YAML anchors with `script`](#yaml-anchors-for-scripts). - Single line commands.
- Long commands [split over multiple lines](script.md#split-long-commands).
- [YAML anchors](#yaml-anchors-for-scripts).
The `script` keyword can also contain several commands in an array: **Example of `script`:**
```yaml ```yaml
job: job1:
script: "bundle exec rspec"
job2:
script: script:
- uname -a - uname -a
- bundle exec rspec - bundle exec rspec
``` ```
Sometimes, `script` commands must be wrapped in single or double quotes. **Additional details**:
For example, commands that contain a colon (`:`) must be wrapped in single quotes (`'`).
The YAML parser needs to interpret the text as a string rather than
a "key: value" pair.
For example, this script uses a colon: You might need to use single quotes (`'`) or double quotes (`"`) when using
[special characters in `script`](script.md#use-special-characters-with-script).
```yaml **Related topics**:
job:
script:
- curl --request POST --header 'Content-Type: application/json' "https://gitlab/api/v4/projects"
```
To be considered valid YAML, you must wrap the entire command in single quotes. If - You can [ignore non-zero exit codes](script.md#ignore-non-zero-exit-codes).
the command already uses single quotes, you should change them to double quotes (`"`) - [Use color codes with `script`](script.md#add-color-codes-to-script-output)
if possible: to make job logs easier to review.
- [Create custom collapsible sections](../jobs/index.md#custom-collapsible-sections)
to simplify job log output.
```yaml #### `before_script`
job:
script:
- 'curl --request POST --header "Content-Type: application/json" "https://gitlab/api/v4/projects"'
```
You can verify the syntax is valid with the [CI Lint](../lint.md) tool. Use `before_script` to define an array of commands that should run before each job's
`script` commands, but after [artifacts](#artifacts) are restored.
Be careful when using these characters as well: **Keyword type**: Job keyword. You can use it only as part of a job or in the
[`default:` section](#custom-default-keyword-values).
- `{`, `}`, `[`, `]`, `,`, `&`, `*`, `#`, `?`, `|`, `-`, `<`, `>`, `=`, `!`, `%`, `@`, `` ` ``. **Possible inputs**: An array including:
If any of the script commands return an exit code other than zero, the job - Single line commands.
fails and further commands are not executed. Store the exit code in a variable to - Long commands [split over multiple lines](script.md#split-long-commands).
avoid this behavior: - [YAML anchors](#yaml-anchors-for-scripts).
**Example of `before_script`:**
```yaml ```yaml
job: job:
before_script:
- echo "Execute this command before any `script:` commands."
script: script:
- false || exit_code=$? - echo "This command executes after the job's `before_script` commands."
- if [ $exit_code -ne 0 ]; then echo "Previous command failed"; fi;
``` ```
#### `before_script` **Additional details**:
Use `before_script` to define an array of commands that should run before each job,
but after [artifacts](#artifacts) are restored.
Scripts you specify in `before_script` are concatenated with any scripts you specify Scripts you specify in `before_script` are concatenated with any scripts you specify
in the main [`script`](#script). The combine scripts execute together in a single shell. in the main [`script`](#script). The combined scripts execute together in a single shell.
You can overwrite a globally-defined `before_script` if you define it in a job: **Related topics**:
```yaml - [Use `before_script` with `default`](script.md#set-a-default-before_script-or-after_script-for-all-jobs)
default: to define a default array of commands that should run before the `script` commands in all jobs.
before_script: - You can [ignore non-zero exit codes](script.md#ignore-non-zero-exit-codes).
- echo "Execute this script in all jobs that don't already have a before_script section." - [Use color codes with `before_script`](script.md#add-color-codes-to-script-output)
to make job logs easier to review.
- [Create custom collapsible sections](../jobs/index.md#custom-collapsible-sections)
to simplify job log output.
job1: #### `after_script`
script:
- echo "This script executes after the global before_script."
job: Use `after_script` to define an array of commands that run after each job, including failed jobs.
before_script:
- echo "Execute this script instead of the global before_script."
script:
- echo "This script executes after the job's `before_script`"
```
You can use [YAML anchors with `before_script`](#yaml-anchors-for-scripts). **Keyword type**: Job keyword. You can use it only as part of a job or in the
[`default:` section](#custom-default-keyword-values).
#### `after_script` **Possible inputs**: An array including:
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)
to make job logs easier to review.
- [Create custom collapsible sections](../jobs/index.md#custom-collapsible-sections) - [Create custom collapsible sections](../jobs/index.md#custom-collapsible-sections)
to simplify job log output. to simplify job log output.
......
...@@ -14,6 +14,85 @@ You can use special syntax in [`script`](index.md#script) sections to: ...@@ -14,6 +14,85 @@ You can use special syntax in [`script`](index.md#script) sections to:
- [Create custom collapsible sections](../jobs/index.md#custom-collapsible-sections) - [Create custom collapsible sections](../jobs/index.md#custom-collapsible-sections)
to simplify job log output. to simplify job log output.
## Use special characters with `script:`
Sometimes, `script` commands must be wrapped in single or double quotes.
For example, commands that contain a colon (`:`) must be wrapped in single quotes (`'`).
The YAML parser needs to interpret the text as a string rather than
a "key: value" pair.
For example, this script uses a colon:
```yaml
job:
script:
- curl --request POST --header 'Content-Type: application/json' "https://gitlab/api/v4/projects"
```
To be considered valid YAML, you must wrap the entire command in single quotes. If
the command already uses single quotes, you should change them to double quotes (`"`)
if possible:
```yaml
job:
script:
- 'curl --request POST --header "Content-Type: application/json" "https://gitlab/api/v4/projects"'
```
You can verify the syntax is valid with the [CI Lint](../lint.md) tool.
Be careful when using these characters as well:
- `{`, `}`, `[`, `]`, `,`, `&`, `*`, `#`, `?`, `|`, `-`, `<`, `>`, `=`, `!`, `%`, `@`, `` ` ``.
## Ignore non-zero exit codes
When script commands return an exit code other than zero, the job fails and further
commands do not execute.
Store the exit code in a variable to avoid this behavior:
```yaml
job:
script:
- false || exit_code=$?
- if [ $exit_code -ne 0 ]; then echo "Previous command failed"; fi;
```
## Set a default `before_script` or `after_script` for all jobs
You can use [`before_script`](index.md#before_script) and [`after_script`](index.md#after_script)
with [`default`](index.md#custom-default-keyword-values):
- Use `before_script` with `default` to define a default array of commands that
should run before the `script` commands in all jobs.
- Use `after_script` with default to define a default array of commands
that should run after the job completes.
You can overwrite a default by defining a different one in a job. To ignore the default
use `before_script: []` or `after_script: []`:
```yaml
default:
before_script:
- echo "Execute this `before_script` in all jobs by default."
after_script:
- echo "Execute this `after_script` in all jobs by default."
job1:
script:
- echo "These script commands execute after the default `before_script`,"
- echo "and before the default `after_script`."
job2:
before_script:
- echo "Execute this script instead of the default `before_script`."
script:
- echo "This script executes after the job's `before_script`,"
- echo "but the job does not use the default `after_script`."
after_script: []
```
## Split long commands ## Split long commands
You can split long commands into multiline commands to improve readability with You can split long commands into multiline commands to improve readability with
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment