Skip to content
Latchkey

gcloud pubsub topics publish: Publish a Message

gcloud pubsub topics publish posts a single message, with optional attributes, to a Pub/Sub topic.

Triggering a downstream consumer at the end of a pipeline, or seeding a test, is a single publish call. Attributes carry metadata alongside the body.

What it does

gcloud pubsub topics publish sends a message body and optional key=value attributes to the named topic and prints the assigned message ID. The topic is referenced by short name in the active project or by full path.

Common usage

Terminal
gcloud pubsub topics publish deploys \
  --message='{"service":"api","status":"deployed"}'
# with attributes for filtering
gcloud pubsub topics publish deploys \
  --message='ok' --attribute=env=prod,version=1.4.2

Flags

FlagWhat it does
<TOPIC>Topic short name or full projects/.../topics/... path
--message <text>Message body to publish
--attribute KEY=VALAdd message attributes (comma-separated)
--ordering-key <key>Set an ordering key for ordered delivery
--project <id>Project that owns the topic

In CI

Authenticate keylessly with Workload Identity Federation; the runner identity needs roles/pubsub.publisher on the topic. Use attributes rather than stuffing metadata into the body so subscribers can filter without parsing.

Common errors in CI

"Resource not found (resource=topics/...)" means a wrong topic name or project. "User not authorized to perform this action" means roles/pubsub.publisher is missing. "Cloud Pub/Sub API has not been used in project ... or it is disabled" means enable pubsub.googleapis.com.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →