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
--gen2and--trigger-topic. - Set
--entry-pointto 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 ordersGotchas
- 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 createfirst.
Related guides
How to Deploy a 2nd Gen Cloud Function With GitHub ActionsDeploy a 2nd gen Cloud Function from GitHub Actions with the deploy-cloud-functions action, setting runtime,…
How to Deploy a Dataflow Job With GitHub ActionsLaunch a Google Cloud Dataflow job from GitHub Actions with gcloud dataflow flex-template run, passing a temp…