Skip to content
Latchkey

How to Deploy a Pub/Sub-Triggered Cloud Function With GitHub Actions

gcloud functions deploy --trigger-topic binds a function to a Pub/Sub topic so each published message invokes it.

Authenticate via WIF, then deploy a 2nd gen function with --trigger-topic <topic>. Messages on that topic invoke the function via Eventarc.

Steps

  • Authenticate via WIF and set up gcloud.
  • Deploy with --gen2 and --trigger-topic.
  • Set --entry-point to the message handler.

Workflow

.github/workflows/deploy.yml
permissions:
  id-token: write
  contents: read
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: google-github-actions/auth@v2
        with:
          workload_identity_provider: ${{ vars.WIF_PROVIDER }}
          service_account: ${{ vars.DEPLOY_SA }}
      - uses: google-github-actions/setup-gcloud@v2
      - run: |
          gcloud functions deploy on-order \
            --gen2 --runtime nodejs20 --region us-central1 \
            --source ./functions --entry-point handleOrder \
            --trigger-topic orders

Gotchas

  • The Pub/Sub and Eventarc service agents need their default roles, or the trigger fails to provision.
  • The topic must already exist; create it with gcloud pubsub topics create first.

Related guides

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