RabbitMQ: 6. GitLab CI

Esta documentación forma parte de la guía Integración de GitHub Actions y GitLab CI. Consulte la guía completa aquí: Inicie un servicio real de RabbitMQ desde su pipeline de GitHub Actions o GitLab CI, ejecute sus pruebas sobre él y elimínelo automáticamente.

Puede guardar esta configuración como .gitlab-ci.yml. Con este setup, cada ejecución del pipeline inicia una nueva instancia real de RabbitMQ para sus pruebas.

test:
  image: ubuntu:24.04
  timeout: 30m
  variables:
    STACK: "CI"                           # La stack creada en el paso 2
    INSTANCE: "200"   # Modifique si lo necesita (ver paso 4)
    REGION: "europe"
    SERVICE_STORE: "rabbitmq"
  # STACKHERO_TOKEN proviene de la variable CI/CD creada en el paso 1.
  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 es accesible desde CI."
    # Puede ejecutar su propia batería de pruebas aquí usando las credenciales anteriores ...
  after_script:
    - test -f service.env && . ./service.env || true
    - >
      if [ -n "${SERVICE_ID:-}" ]; then
        stackhero service-delete --service="$SERVICE_ID" --confirm
      fi

En GitLab, la eliminación se realiza en after_script. Esta sección se ejecuta incluso si el job falla, por lo que su servicio RabbitMQ se elimina y no se le factura por un servicio que no está utilizando.

En GitLab, after_script se ejecuta en un shell limpio. Para gestionarlo, el script escribe el ID del servicio en service.env en cuanto se añade el servicio, y lo recarga antes de la eliminación. Así, incluso si algo falla a mitad del job, su servicio se elimina igualmente.