跳到内容
脚本与 CI/CD

脚本与 CI/CD

CLI 在脚本中的工作方式与在终端中相同。通过环境变量给它 API 密钥,把它的回答当作 JSON 读取,并让它的退出码告诉脚本发生了什么。本页介绍其中的每一项,以及适用于 GitHub Actions、GitLab CI 和 Docker 的完整流水线。

脚本如何使用 CLI

部署可以先询问 Status200 是否有未解决的事件,如果有就停止:

flowchart TB J["CI 作业开始"] --> S["它的机密设置<br/>STATUS200_API_KEY<br/>和 STATUS200_URL"] S --> Q["status200 incident count<br/>统计未解决的事件"] Q -->|"0"| D["部署"] Q -->|"1 或更多"| X["停止:有未解决<br/>的事件"] Q -->|"退出码 2"| A["停止:检查<br/>API 密钥"]

有事件未解决时会等待的部署

为脚本提供 API 密钥

从 CI 系统的机密中设置 STATUS200_API_KEY 和 STATUS200_URL。这样 CLI 既不需要 login,也不需要配置文件。对单条命令而言,标志仍然优先于它们,而它们优先于已保存的上下文。

Bash
export STATUS200_API_KEY="$CI_STATUS200_API_KEY"
export STATUS200_URL="https://status200.ru"
status200 incident count
提示

为每条流水线提供专属的 API 密钥,只赋予它需要的访问权限:只读取的检查只需要只读取的密钥。流水线停用时,在 Status200 中删除该密钥。

如果一台机器为多个项目运行作业,请为每个项目保存一个上下文,并在每条命令中指定它,这样作业永远不会使用另一个作业的当前上下文:

Bash
status200 --context production incident count
status200 --context staging monitor count

退出码

代码含义示例
0成功一次列出,即使什么都没找到
1失败Status200 拒绝了请求,例如 API error (422);无效的 JSON;缺少或未知的选项;未保存的上下文
2没有可用的 API 密钥找不到凭据;无效的 API 密钥;低于其套餐的密钥(402);401
3未找到Status200 返回 404:记录不存在、位于其他项目,或密钥无法读取它

即使什么都没有保存,whoami 和 context current 也以 0 结束:它们报告已设置的内容,但不检查它。脚本通过退出码区分各种情况:

Bash
status200 incident get "$INCIDENT_ID" -o json > incident.json
case $? in
  0) echo "Saved the incident to incident.json" ;;
  2) echo "Check STATUS200_API_KEY and STATUS200_URL" >&2; exit 1 ;;
  3) echo "There is no incident $INCIDENT_ID" >&2; exit 1 ;;
  *) echo "Status200 refused the request" >&2; exit 1 ;;
esac

错误消息输出到标准错误,因此 incident.json 只包含回答。

用 jq 读取回答

在脚本中写上 -o json。在管道中 CLI 本来就会打印 JSON,但有了这个标志,脚本在任何地方运行都能正常工作:

Bash
# The title of each incident
status200 incident list -o json | jq -r '.[].title'

# The ID of a new monitor
MONITOR_ID=$(status200 monitor create --data '{"name":"Checkout API","monitorType":"Manual"}' -o json | jq -r '._id')

# When each incident was created: dates come as objects
status200 incident list -o json | jq -r '.[] | "\(.createdAt.value)  \(.title)"'

# The newest 100 incidents as CSV
status200 incident list --limit 100 -o json | jq -r '.[] | [._id, .title, .createdAt.value] | @csv'

统计只打印一个数字,可直接用于比较:

Bash
OPEN=$(status200 incident count --query '{"currentIncidentState":{"isResolvedState":false}}')
echo "$OPEN incidents are open"

从文件创建记录

把记录作为 JSON 文件放在代码旁边,并从那里创建它们:

monitors/checkout-api.jsonJSON
{
  "name": "Checkout API",
  "monitorType": "Manual"
}
Bash
status200 monitor create --file monitors/checkout-api.json

从一个 JSON 数组创建多条记录,每条创建一次:

Bash
jq -c '.[]' monitors.json | while IFS= read -r monitor; do
  status200 monitor create --data "$monitor" -o json | jq -r '._id'
done
注意

每次运行都会创建新记录。要让监视器和状态页面与文件保持同步,只更改变化的部分并删除已不存在的部分,请使用 Terraform 提供程序。

流水线示例

GitHub Actions

.github/workflows/deploy.ymlYAML
name: Deploy
on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: lts/*
      - name: Install the Status200 CLI
        run: npm install -g @status200/cli
      - name: Stop while an incident is open
        env:
          STATUS200_API_KEY: ${{ secrets.STATUS200_API_KEY }}
          STATUS200_URL: https://status200.ru
        run: |
          OPEN=$(status200 incident count --query '{"currentIncidentState":{"isResolvedState":false}}')
          if [ "$OPEN" -gt 0 ]; then
            echo "$OPEN incidents are open: not deploying."
            exit 1
          fi
      - name: Deploy
        run: ./deploy.sh

GitLab CI

.gitlab-ci.ymlYAML
check-incidents:
  stage: test
  image: node:lts
  variables:
    STATUS200_URL: https://status200.ru
  script:
    - npm install -g @status200/cli
    - OPEN=$(status200 incident count --query '{"currentIncidentState":{"isResolvedState":false}}')
    - echo "$OPEN incidents are open"
    - test "$OPEN" -eq 0

Docker

DockerfileDockerfile
FROM node:lts-slim
RUN npm install -g @status200/cli
ENTRYPOINT ["status200"]

在 GitHub Actions 中,在仓库的 Settings → Secrets and variables → Actions 下添加 STATUS200_API_KEY;在 GitLab 中,把它添加为掩码 CI/CD 变量。使用 Docker 镜像时,每次运行都传入这些变量:

Bash
docker build -t status200-cli .
docker run --rm -e STATUS200_API_KEY -e STATUS200_URL status200-cli incident count

后续步骤