Mercure-Hub: 3. The lifecycle script

This documentation is part of the CI pipelines with GitHub Actions and GitLab CI guide. You can view the complete guide here: Launch a real Mercure Hub service from your GitHub Actions or GitLab CI pipeline, run your tests against it, and automatically tear it down.

👋 Welcome to the Stackhero documentation!

Stackhero offers a fully managed Mercure-Hub cloud service designed to simplify and ensure reliable real-time data delivery. You benefit from:

  • Unlimited requests and message sizes for total flexibility.
  • A custom domain with integrated HTTPS security (for example, https://real-time.your-company.com).
  • One-click updates to keep your hub up to date effortlessly.
  • High performance and enhanced security on a private, dedicated infrastructure.
  • Multiple available regions: 🇪🇺 Europe and 🇺🇸 USA for low-latency delivery.

Get started quickly: it only takes 5 minutes to launch your Mercure-Hub cloud hosting environment and start sending real-time updates to your applications.

Below is a complete example script that manages the full service lifecycle. It shows how little setup is required—just a few commands. You can copy the ready-to-use YAML for your platform from the sections below.

#!/bin/bash
set -euo pipefail

# STACKHERO_TOKEN is supplied by the CI secret, the CLI picks it up automatically.

stack="CI"                              # The stack created in step 2
serviceStore="mercure-hub"             # The Mercure Hub service store
instance="20G"        # Change this as needed (see step 4)
region="europe"                         # Region name (see stackhero regions-list)

# 1. Install the client used by the smoke test (as root, or prefix with sudo)
apt-get update && apt-get install -y --no-install-recommends ca-certificates jq curl

# 2. Install the CLI on the runner
curl -fsSL https://www.stackhero.io/install.sh | STACKHERO_INSTALL_DIR="$HOME/.local/bin" sh
export PATH="$HOME/.local/bin:$PATH"

# 3. Add Mercure Hub to the CI stack and capture its service ID
serviceId=$(stackhero --format=script service-add \
  --stack="$stack" \
  --service-store="$serviceStore" \
  --instance="$instance" \
  --region="$region")
echo "Service added: $serviceId"

# 4. Delete the service when the script ends, whether it succeeds or fails
trap 'stackhero service-delete --service="$serviceId" --confirm' EXIT

# 5. Wait until the service is running (this usually takes a couple of minutes)
timeout 15m stackhero service-wait-for --service="$serviceId"

# 6. Read the configuration (contains the generated credentials)
config=$(stackhero service-configuration-get --service="$serviceId" --format=json)

# 7. Extract the credentials you need
host=$(echo "$config" | jq -r '.configuration.domain')

# 8. Smoke test: Request the hub endpoint and check it answers.
curl -fsS -o /dev/null -w "%{http_code}" "https://$host/.well-known/mercure?topic=test" | grep -qE "^(200|400|401)$"

echo "✅ Mercure Hub is reachable from CI."

The only cleanup required is deleting the service: the stack remains for the next run. In the YAML below, this deletion is a separate step that the CI always runs, even if a test fails, so you are never billed for a service you no longer use.

timeout 15m sets a limit on the wait. If the service does not start, the run fails quickly and the service is deleted, instead of waiting for your CI's time limit.