RethinkDB: 6. GitLab CI
本文件是在 CI/CD 流水线中集成 GitHub Actions 与 GitLab CI指南的一部分。您可以在这里查看完整指南:通过 GitHub Actions 或 GitLab CI 流水线启动真实的 RethinkDB 服务,运行测试并自动销毁服务。
欢迎查阅 Stackhero 文档
Stackhero 提供即开即用的 RethinkDB cloud 服务,专为高可靠性和易用性设计。您将获得:
- 一键式无缝更新
- 在您专属的私有基础设施上实现高性能与强安全性
快速启动:您只需几分钟即可试用 Stackhero 的 RethinkDB cloud hosting。Stackhero 负责所有部署与管理,让您专注于开发自己的应用。
您可以将以下配置保存为 .gitlab-ci.yml。每次流水线运行都会为测试启动一个全新的真实 RethinkDB 实例。
test:
image: ubuntu:24.04
timeout: 30m
variables:
STACK: "CI" # 第 2 步创建的 stack
INSTANCE: "10G" # 如有需要可修改(见第 4 步)
REGION: "europe"
SERVICE_STORE: "rethinkdb"
# STACKHERO_TOKEN 来源于第 1 步创建的 CI/CD 变量。
script:
- set -euo pipefail
- apt-get update && apt-get install -y --no-install-recommends ca-certificates jq curl netcat-openbsd
- curl -fsSL https://www.stackhero.io/install.sh | sh
- SERVICE_ID=$(stackhero --format=script service-add --stack="$STACK" --service-store="$SERVICE_STORE" --instance="$INSTANCE" --region="$REGION")
- echo "SERVICE_ID=$SERVICE_ID" > service.env
- timeout 15m stackhero service-wait-for --service="$SERVICE_ID"
- config=$(stackhero service-configuration-get --service="$SERVICE_ID" --format=json)
- host=$(echo "$config" | jq -r '.configuration.domain')
# Open a TCP connection to the driver port.
- nc -z -w 5 "$host" 28015
- echo "✅ RethinkDB 已可从 CI 访问。"
# 您也可以在此处用上方凭证运行自定义测试套件 ...
after_script:
- test -f service.env && . ./service.env || true
- >
if [ -n "${SERVICE_ID:-}" ]; then
stackhero service-delete --service="$SERVICE_ID" --confirm
fi
在 GitLab 中,删除操作放在 after_script,该部分即使作业失败也会执行,因此您的 RethinkDB 服务会被及时移除,避免产生不必要的费用。
在 GitLab 中,
after_script会在 全新 shell 中运行。为此,脚本在添加服务后立即将服务 ID 写入service.env,删除前再重新加载。这样即使作业中途失败,也能确保服务被删除。