debian-mirror-gitlab/doc/ci/chatops/index.md

114 lines
3.8 KiB
Markdown
Raw Normal View History

2021-03-11 19:13:27 +05:30
---
2023-06-20 00:43:36 +05:30
stage: Deploy
group: Environments
2022-11-25 23:54:43 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/product/ux/technical-writing/#assignments
2021-03-11 19:13:27 +05:30
type: index, concepts, howto
---
# GitLab ChatOps **(FREE)**
2021-11-11 11:23:49 +05:30
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/4466) in GitLab Ultimate 10.6.
> - [Moved](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/24780) to GitLab Free in 11.9.
2023-05-27 22:25:52 +05:30
> - `CHAT_USER_ID` [introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/341798) in GitLab 14.4.
2021-03-11 19:13:27 +05:30
2023-05-27 22:25:52 +05:30
Use GitLab ChatOps to interact with CI/CD jobs through chat services
like Slack.
2021-03-11 19:13:27 +05:30
2023-05-27 22:25:52 +05:30
Many organizations use chat services to collaborate, troubleshoot, and plan work. With ChatOps,
you can discuss work with your team, run CI/CD jobs, and view job output, all from the same
application.
2021-03-11 19:13:27 +05:30
2023-05-27 22:25:52 +05:30
## ChatOps workflow and CI/CD configuration
2021-03-11 19:13:27 +05:30
2023-05-27 22:25:52 +05:30
ChatOps looks for the specified job in the
[`.gitlab-ci.yml`](../yaml/index.md) on the project's default
branch. If the job is found, ChatOps creates a pipeline that contains
only the specified job. If you set `when: manual`, ChatOps creates the
pipeline, but the job doesn't start automatically.
A job run with ChatOps has the same functionality as a job run from
GitLab. The job can use existing [CI/CD variables](../variables/index.md#predefined-cicd-variables) like
`GITLAB_USER_ID` to perform additional rights validation, but these
variables can be [overridden](../variables/index.md#cicd-variable-precedence).
You should set [`rules`](../yaml/index.md#rules) so the job does not
run as part of the standard CI/CD pipeline.
2021-03-11 19:13:27 +05:30
2021-09-30 23:02:18 +05:30
ChatOps passes the following [CI/CD variables](../variables/index.md#predefined-cicd-variables)
2021-03-11 19:13:27 +05:30
to the job:
2023-05-27 22:25:52 +05:30
- `CHAT_INPUT` - The arguments passed to `/project-name run`.
- `CHAT_CHANNEL` - The name of the chat channel the job is run from.
- `CHAT_USER_ID` - The chat service ID of the user who runs the job.
2021-03-11 19:13:27 +05:30
2023-05-27 22:25:52 +05:30
When the job runs:
2021-03-11 19:13:27 +05:30
2023-05-27 22:25:52 +05:30
- If the job completes in less than 30 minutes, ChatOps sends the job output to the chat channel.
- If the job completes in more than 30 minutes, you must use a method like the
2021-03-11 19:13:27 +05:30
[Slack API](https://api.slack.com/) to send data to the channel.
2023-05-27 22:25:52 +05:30
## Run a CI/CD job
You can run a CI/CD job from chat with the `/project-name run`
[slash command](../../integration/slash_commands.md).
Prerequisites:
- You must have at least the Developer role for the project.
To run a CI/CD job:
- In the chat client, enter `/project-name run <job name> <arguments>`.
2021-03-11 19:13:27 +05:30
2023-05-27 22:25:52 +05:30
ChatOps schedules a pipeline that contains only the specified job.
2021-03-11 19:13:27 +05:30
2023-05-27 22:25:52 +05:30
### Exclude a job from ChatOps
2021-03-11 19:13:27 +05:30
2023-05-27 22:25:52 +05:30
To prevent a job from being run from chat:
2021-03-11 19:13:27 +05:30
2023-05-27 22:25:52 +05:30
- In `.gitlab-ci.yml`, set the job to `except: [chat]`.
2021-03-11 19:13:27 +05:30
2023-05-27 22:25:52 +05:30
## Customize the ChatOps reply
ChatOps sends the output for a job with a single command to the
channel as a reply. For example, when the following job runs,
the chat reply is `Hello world`:
2021-03-11 19:13:27 +05:30
```yaml
2021-10-27 15:23:28 +05:30
stages:
- chatops
2021-03-11 19:13:27 +05:30
hello-world:
stage: chatops
2021-10-27 15:23:28 +05:30
rules:
2022-07-16 23:28:13 +05:30
- if: $CI_PIPELINE_SOURCE == "chat"
2021-03-11 19:13:27 +05:30
script:
- echo "Hello World"
```
2023-05-27 22:25:52 +05:30
If the job contains multiple commands, or if `before_script` is set, ChatOps sends the commands
and their output to the channel. The commands are wrapped in ANSI color codes.
2021-03-11 19:13:27 +05:30
2023-05-27 22:25:52 +05:30
To selectively reply with the output of one command, place the output
in a `chat_reply` section. For example, the following job lists the
files in the current directory:
2021-03-11 19:13:27 +05:30
```yaml
2021-10-27 15:23:28 +05:30
stages:
- chatops
2021-03-11 19:13:27 +05:30
ls:
stage: chatops
2021-10-27 15:23:28 +05:30
rules:
2022-07-16 23:28:13 +05:30
- if: $CI_PIPELINE_SOURCE == "chat"
2021-03-11 19:13:27 +05:30
script:
- echo "This command will not be shown."
- echo -e "section_start:$( date +%s ):chat_reply\r\033[0K\n$( ls -la )\nsection_end:$( date +%s ):chat_reply\r\033[0K"
```
2023-05-27 22:25:52 +05:30
## Related topics
2021-03-11 19:13:27 +05:30
2023-06-20 00:43:36 +05:30
- [The official GitLab ChatOps icon](img/gitlab-chatops-icon.png)
- [A repository of common ChatOps scripts](https://gitlab.com/gitlab-com/chatops)
that GitLab uses to interact with GitLab.com