# 在 GitHub Actions 中加入部署审批与通知

> 部署前先暂停，等有人在手机上批准，上线后再通知大家：用实时事件更新仪表盘、给你 app 的用户发推送通知，还能按计划开启排队室。

Source: https://hibira.io/zh-cn/guides/github-actions

部署前先暂停，等有人在手机上批准，上线后再通知大家：用实时事件更新仪表盘、给你 app 的用户发推送通知，还能按计划开启排队室。

Package: hibira-io/action@v1

- 审批在公开和私有仓库、任何 GitHub 方案中都能使用，审批人不需要 GitHub 账号。
- 请求被拒绝或无人回应时，step 会失败，没有批准就不会上线。
- 部署通知可以是实时事件或推送通知，无论 job 成功还是失败都会发送。
- 没有任何依赖：一个命令一个 step，app 的凭据放在仓库 secrets 中。

## 1. 配置

```shell
# Repository → Settings → Secrets and variables → Actions
# Variables: HIBIRA_APP_ID, HIBIRA_APP_KEY
# Secret:    HIBIRA_APP_SECRET
```

## 2. 部署前先请人批准

```yaml
- uses: hibira-io/action@v1
  with:
    command: approval
    app-id: ${{ vars.HIBIRA_APP_ID }}
    key: ${{ vars.HIBIRA_APP_KEY }}
    secret: ${{ secrets.HIBIRA_APP_SECRET }}
    host: https://ws.hibira.io
    title: Deploy ${{ github.ref_name }} to production?
    timeout-minutes: 30
- run: ./deploy.sh
```

## 3. 发送部署通知

```yaml
- if: always()
  uses: hibira-io/action@v1
  with:
    command: event
    app-id: ${{ vars.HIBIRA_APP_ID }}
    key: ${{ vars.HIBIRA_APP_KEY }}
    secret: ${{ secrets.HIBIRA_APP_SECRET }}
    host: https://ws.hibira.io
    channel: deploys
    event: deploy.finished
    data: '{"version":"${{ github.ref_name }}","status":"${{ job.status }}"}'
```

## 4. 按计划开启排队室

```yaml
on:
  schedule:
    - cron: '0 1 * * 5'
jobs:
  open:
    runs-on: ubuntu-latest
    steps:
      - uses: hibira-io/action@v1
        with:
          command: waiting-room
          app-id: ${{ vars.HIBIRA_APP_ID }}
          key: ${{ vars.HIBIRA_APP_KEY }}
          secret: ${{ secrets.HIBIRA_APP_SECRET }}
          host: https://ws.hibira.io
          room: friday-drop
          state: open
          admit-per-minute: 200
```

## 开始之前

- 等待回答时 runner 会一直被占用，所以 timeout-minutes 请设成接近预期的回答时间。
- 改用 command: push 会发送推送通知；需要 app 的推送 instance ID 和 secret key，可在 app 页面的「推送通知」中找到。
- 也可以用 api-token（在 Settings → AI agents 创建、有读写权限的 API token）代替 key 和 secret：这样每个命令（包括 push）都只需要 app-id。

## 常见问题

### 审批需要付费的 GitHub 方案吗？

不需要。step 是在 Hibira 的审批页面上等待回答，所以公开和私有仓库、任何 GitHub 方案都能用，审批人也不必登录 GitHub。

### 无人回应会怎样？

请求在 timeout-minutes 后过期，step 会失败；如果设置 fail-on-reject: false，则可以自己检查 status 输出再决定。

- [GitHub Marketplace](https://github.com/marketplace/actions/hibira-deploy-approvals)
- [GitHub](https://github.com/hibira-io/action)
