Mosquitto: 3. The lifecycle script

This documentation is part of the GitHub Actions & GitLab CI guide. View the full guide here: Spin up a real Mosquitto service from your GitHub Actions or GitLab CI pipeline, run your tests against it, and tear it down automatically.

👋 Welcome to the Stackhero documentation!

Stackhero gives you a fully managed Mosquitto MQTT cloud environment designed for reliability and flexibility:

  • Unlimited message throughput and data transfers, so your workflows never hit artificial limits.
  • Unlimited device authentication through your own external API, making onboarding and access management seamless.
  • Advanced ACLs for fine-grained control over topics, users, and actions.
  • A custom domain name with built-in HTTPS for secure, branded endpoints (for example: https://mqtt.your-company.com).
  • Zero-hassle updates: apply improvements or security patches with a single click.
  • Consistent performance and strong security, with every instance running on a private, dedicated infrastructure.

Accelerate your IoT projects and reduce operational overhead. You can have a secure, production-ready Mosquitto MQTT cloud hosting instance up and running in just a few minutes.

Here is a complete example script that manages the full service lifecycle. It demonstrates how little setup is required, just a few commands. You can copy the ready-made 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="mosquitto"             # The Mosquitto service store
instance="200"        # 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 it with sudo)
apt-get update && apt-get install -y --no-install-recommends ca-certificates jq curl mosquitto-clients

# 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 Mosquitto 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')
user=$(echo "$config" | jq -r '.configuration.authenticationUsers[0].login')
password=$(echo "$config" | jq -r '.configuration.authenticationUsers[0].password')

# 8. Smoke test: Publish a test message to a topic.
mosquitto_pub -h "$host" -p 8883 -u "$user" -P "$password" --capath /etc/ssl/certs -t "stackhero/ci" -m "hello"

echo "✅ Mosquitto is reachable from CI."

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

timeout 15m puts a limit on the wait. If the service does not start, the run fails within minutes and the service is deleted, instead of waiting for the time limit of your CI.