多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# GitLab ChatOps > 原文:[https://docs.gitlab.com/ee/ci/chatops/README.html](https://docs.gitlab.com/ee/ci/chatops/README.html) * [How GitLab ChatOps works](#how-gitlab-chatops-works) * [Best practices for ChatOps CI jobs](#best-practices-for-chatops-ci-jobs) * [Controlling the ChatOps reply](#controlling-the-chatops-reply) * [GitLab ChatOps examples](#gitlab-chatops-examples) * [GitLab ChatOps icon](#gitlab-chatops-icon) # GitLab ChatOps[](#gitlab-chatops "Permalink") 版本历史 * 在[GitLab Ultimate](https://about.gitlab.com/pricing/) 10.6 中[引入](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/4466) . * 在 11.9 中[移至](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/24780) [GitLab Core](https://about.gitlab.com/pricing/) . GitLab ChatOps 提供了一种通过 Slack 等聊天服务与 CI / CD 作业进行交互的方法. 许多组织的讨论,协作和故障排除都是在聊天服务中进行的. 拥有一种方法来运行 CI / CD 作业并将输出回传到渠道,可以极大地增强团队的工作流程. ## How GitLab ChatOps works[](#how-gitlab-chatops-works "Permalink") GitLab ChatOps 是基于[GitLab CI / CD](../README.html)和[Slack Slash Commands](../../user/project/integrations/slack_slash_commands.html)构建的. ChatOps 使用以下参数为[斜杠命令](../../integration/slash_commands.html)提供`run`操作: * 要执行的`<job name>` . * The `<job arguments>`. ChatOps 将以下[CI / CD 变量](../variables/README.html#predefined-environment-variables)传递给作业: * `CHAT_INPUT`包含任何其他参数. * `CHAT_CHANNEL`设置为触发该动作的通道的名称. 当执行时,ChatOps 查找指定的作业名称,并尝试将其与[`.gitlab-ci.yml`](../yaml/README.html)的相应作业匹配. 如果在`master`上找到匹配的作业,则计划仅包含该作业的管道. 作业完成后: * 如果作业在*不到 30 分钟内完成* ,则 ChatOps 会将作业的输出发送到 Slack. * 如果作业在*30 分钟内完成* ,则该作业必须使用[Slack API](https://api.slack.com/)将数据发送到通道. 要使用`run`命令,您必须具有[Developer 访问权限或更高权限](../../user/permissions.html#project-members-permissions) . 如果不能从聊天中触发某项工作,则可以将其设置为下列`except: [chat]` . ## Best practices for ChatOps CI jobs[](#best-practices-for-chatops-ci-jobs "Permalink") 由于 ChatOps 是基于 GitLab CI / CD 构建的,因此该作业具有可用的所有相同功能. 创建 ChatOps 作业时,请考虑以下最佳做法: * GitLab 强烈建议您`only: [chat]`设置`only: [chat]`以便该作业不会作为标准 CI 管道的一部分运行. * 如果作业设置为以下`when: manual` ,则 ChatOps 将创建管道,但是作业等待启动. * ChatOps 为访问控制提供了有限的支持. 如果触发斜杠命令的用户在项目中具有" [开发人员"访问权限或更高权限](../../user/permissions.html#project-members-permissions) ,则作业将运行. 作业本身可以使用现有的[CI / CD 变量(](../variables/README.html#predefined-environment-variables)例如`GITLAB_USER_ID`来执行其他权限验证,但是可以[覆盖](../variables/README.html#priority-of-environment-variables)这些变量. ### Controlling the ChatOps reply[](#controlling-the-chatops-reply "Permalink") 单个命令的作业输出将作为答复发送到通道. 例如,以下任务的聊天回复是频道中的`Hello World` : ``` hello-world: stage: chatops only: [chat] script: - echo "Hello World" ``` 包含多个命令(或`before_script` )的作业将在聊天回复中返回其他内容. 在这些情况下,命令和它们的输出都包括在内,并且命令以 ANSI 颜色代码包装. To selectively reply with the output of one command, its output must be bounded by the `chat_reply` section. For example, the following job lists the files in the current directory: ``` ls: stage: chatops only: [chat] 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" ``` ## GitLab ChatOps examples[](#gitlab-chatops-examples "Permalink") GitLab.com 团队创建了一个[常见 ChatOps 脚本](https://gitlab.com/gitlab-com/chatops)的存储库,它们用于与我们的 GitLab 生产实例进行交互. 其他 GitLab 实例的管理员可能会发现它们很有用. 它们可以作为您可以编写以与自己的应用程序进行交互的 ChatOps 脚本的灵感. ## GitLab ChatOps icon[](#gitlab-chatops-icon "Permalink") [官方的 GitLab ChatOps 图标](img/gitlab-chatops-icon.png)可以下载. 您可以在此处找到并下载官方的 GitLab ChatOps 图标. [![GitLab ChatOps bot icon](https://img.kancloud.cn/7b/60/7b60a293a2acf2d8fe90578f65e41547_164x144.png)](img/gitlab-chatops-icon-small.png)