RabbitMQ: 6. GitLab CI
本文件是在 CI/CD 流水线中集成 GitHub Actions 与 GitLab CI指南的一部分。您可以在这里查看完整指南:通过 GitHub Actions 或 GitLab CI 流水线启动真实的 RabbitMQ 服务,运行测试并自动销毁服务。
您可以将以下配置保存为 .gitlab-ci.yml。每次流水线运行都会为测试启动一个全新的真实 RabbitMQ 实例。
test:
image: ubuntu:24.04
timeout: 30m
variables:
STACK: "CI" # 第 2 步创建的 stack
INSTANCE: "200" # 如有需要可修改(见第 4 步)
REGION: "europe"
SERVICE_STORE: "rabbitmq"
# STACKHERO_TOKEN 来源于第 1 步创建的 CI/CD 变量。
script:
- set -euo pipefail
- apt-get update && apt-get install -y --no-install-recommends ca-certificates jq curl
- 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')
- password=$(echo "$config" | jq -r '.configuration.password')
# Call the management API (health check, falling back to overview).
- curl -fsS -u "admin:$password" "https://$host/api/health/checks/alarms" | grep -q '"status":"ok"' || curl -fsS -u "admin:$password" "https://$host/api/overview" | grep -q '"rabbitmq_version"'
- echo "✅ RabbitMQ 已可从 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,该部分即使作业失败也会执行,因此您的 RabbitMQ 服务会被及时移除,避免产生不必要的费用。
在 GitLab 中,
after_script会在 全新 shell 中运行。为此,脚本在添加服务后立即将服务 ID 写入service.env,删除前再重新加载。这样即使作业中途失败,也能确保服务被删除。