Notify Slack Action 使用教程
1. 项目介绍
Notify Slack Action
是一个 GitHub Action,用于在 GitHub Actions 工作流中发送 Slack 通知。它可以通知工作流的失败、警告或成功状态。该项目支持自定义通知标题、消息和页脚,并且可以根据工作流状态提及用户或用户组。
2. 项目快速启动
2.1 安装依赖
首先,克隆项目并安装依赖:
git clone https://github.com/ravsamhq/notify-slack-action.git
cd notify-slack-action
npm install
2.2 配置 GitHub Actions
在你的 GitHub Actions 工作流文件中,添加以下步骤以发送 Slack 通知:
name: Notify Slack on Failure
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Run tests
run: npm test
- name: Notify Slack
if: always()
uses: ravsamhq/notify-slack-action@v2
with:
status: ${{ job.status }}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
2.3 配置 Slack Webhook
确保在 GitHub 仓库的 Secrets 中添加 SLACK_WEBHOOK_URL
,以便 GitHub Actions 可以发送通知到 Slack。
3. 应用案例和最佳实践
3.1 自定义通知标题和消息
你可以自定义通知的标题和消息格式:
steps:
- name: Notify Slack
if: always()
uses: ravsamhq/notify-slack-action@v2
with:
status: ${{ job.status }}
notification_title: "[workflow] has [status_message]"
message_format: "[emoji] *[workflow]* [status_message] in <[repo_url]|[repo]>"
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
3.2 提及用户和用户组
在工作流失败时提及特定用户或用户组:
steps:
- name: Notify Slack
if: always()
uses: ravsamhq/notify-slack-action@v2
with:
status: ${{ job.status }}
notify_when: "failure"
mention_users: "U0160UUNH8S,U0080UUAA9N"
mention_users_when: "failure,warnings"
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
4. 典型生态项目
4.1 GitHub Actions
Notify Slack Action
是 GitHub Actions 生态系统的一部分,GitHub Actions 是一个持续集成和持续交付 (CI/CD) 平台,允许你自动化你的软件开发工作流程。
4.2 Slack
Slack 是一个团队协作工具,Notify Slack Action
通过 Slack Webhook 将 GitHub Actions 的工作流状态通知发送到 Slack 频道,从而实现团队协作和问题跟踪。
通过以上步骤,你可以轻松地将 Notify Slack Action
集成到你的 GitHub Actions 工作流中,实现自动化的 Slack 通知。