Valkey: 6. GitLab CI
本文件是在 CI/CD 流水线中集成 GitHub Actions 与 GitLab CI指南的一部分。您可以在这里查看完整指南:通过 GitHub Actions 或 GitLab CI 流水线启动真实的 Valkey 服务,运行测试并自动销毁服务。
👋 欢迎来到 Stackhero 文档!
Stackhero 提供即开即用的 Valkey cloud 解决方案,带来多项优势,包括:
- 内置
Valkey AdminWeb 管理界面。- 无限制的消息大小与传输。
- 只需一键即可轻松完成更新。
- 基于专属私有基础设施,实现卓越性能与强大安全性。
节省时间,简化您的工作流程:只需 5 分钟即可体验 Stackhero 的 Valkey cloud hosting 解决方案!
您可以将以下配置保存为 .gitlab-ci.yml。每次流水线运行都会为测试启动一个全新的真实 Valkey 实例。
test:
image: ubuntu:24.04
timeout: 30m
variables:
STACK: "CI" # 第 2 步创建的 stack
INSTANCE: "1G" # 如有需要可修改(见第 4 步)
REGION: "europe"
SERVICE_STORE: "valkey"
# STACKHERO_TOKEN 来源于第 1 步创建的 CI/CD 变量。
script:
- set -euo pipefail
- apt-get update && apt-get install -y --no-install-recommends ca-certificates jq curl redis-tools
- 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.credentials.password')
# Send a PING and expect a PONG.
- redis-cli -h "$host" -p 6380 -a "$password" --tls --no-auth-warning PING | grep -q PONG
- echo "✅ Valkey 已可从 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,该部分即使作业失败也会执行,因此您的 Valkey 服务会被及时移除,避免产生不必要的费用。
在 GitLab 中,
after_script会在 全新 shell 中运行。为此,脚本在添加服务后立即将服务 ID 写入service.env,删除前再重新加载。这样即使作业中途失败,也能确保服务被删除。