Meilisearch: 6. GitLab CI

本文档属于在 CI/CD 流水线中集成 GitHub Actions 与 GitLab CI指南的一部分。您可以在此处查看完整指南:通过 GitHub Actions 或 GitLab CI 流水线启动真实的 Meilisearch 服务,运行测试并自动销毁服务。

欢迎查阅 Stackhero 文档!

如果您需要一款快速、容错拼写且易于部署的搜索引擎,Stackhero 的 Meilisearch cloud 服务可让您在几分钟内快速上手。您将获得:

  • 支持自定义域名,集成HTTPS,保障安全访问。
  • 简单的一键更新,无需手动操作。
  • 稳定的性能和强大的隔离性,专属私有独立基础设施。

您只需 5 分钟即可在 Stackhero 上部署 Meilisearch cloud hosting。整个配置流程旨在为您节省时间,让部署变得简单高效。

您可以将以下配置保存为 .gitlab-ci.yml。这样,每次流水线运行都会为测试启动一个全新的真实 Meilisearch 实例。

test:
  image: ubuntu:24.04
  timeout: 30m
  variables:
    STACK: "CI"                           # 第 2 步创建的 stack
    INSTANCE: "10G"   # 如有需要可修改(见第 4 步)
    REGION: "europe"
    SERVICE_STORE: "meilisearch"
  # 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')
    # Call the Meilisearch health endpoint.
    - curl -fsS "https://$host/health" | grep -q '"status":"available"'
    - echo "✅ Meilisearch 已可从 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 中。该部分即使在作业失败时也会执行,因此您的 Meilisearch 服务会被及时删除,避免为未使用的服务付费。

在 GitLab 中,after_script 会在全新 shell中执行。为此,脚本在添加服务后立即将服务 ID 写入 service.env 文件,并在删除前重新加载。这样即使作业中途失败,也能确保服务被正确删除。