build/cloudfns/wikiwebhook
Dmitri Shuralyov 884a8debac cloudfns/wikiwebhook: set GCP_PROJECT env var explicitly
Prior to CL 230637, this cloud function was targeting the Go 1.11
runtime, which implicitly¹ set the GCP_PROJECT environment variable.
That no longer happens with the newer Go runtime², so set the same
variable explicitly, otherwise publishing to the topic fails.

Remove a defer r.Body.Close() line; the http.Request body is closed
by the http.Server, http.Handler does not need to.

Also update the version of pubsub package to the latest. It isn't
needed to fix the problem, but it has already been tested during
debugging, so it's inexpensive to include.

A future enhancement is to factor out the pubsub client creation
to happen once per function instance rather than per invocation³,
and use metadata.ProjectID instead of needing an explicit env var.

¹ https://cloud.google.com/functions/docs/env-var#nodejs_8_python_37_and_go_111
² https://cloud.google.com/functions/docs/env-var#newer_runtimes
³ https://cloud.google.com/functions/docs/concepts/go-runtime#one-time_initialization

Fixes golang/go#44948.

Change-Id: I03fd66c7fb784cef24c5759b45cb7c10ed17ee6a
Reviewed-on: https://go-review.googlesource.com/c/build/+/302549
Trust: Dmitri Shuralyov <dmitshur@golang.org>
Trust: Alexander Rakoczy <alex@golang.org>
Reviewed-by: Alexander Rakoczy <alex@golang.org>
Reviewed-by: Carlos Amedee <carlos@golang.org>
2021-03-18 16:11:36 +00:00
..
README.md cloudfns/wikiwebhook: set GCP_PROJECT env var explicitly 2021-03-18 16:11:36 +00:00
go.mod cloudfns/wikiwebhook: set GCP_PROJECT env var explicitly 2021-03-18 16:11:36 +00:00
go.sum cloudfns/wikiwebhook: set GCP_PROJECT env var explicitly 2021-03-18 16:11:36 +00:00
wikiwebhook.go cloudfns/wikiwebhook: set GCP_PROJECT env var explicitly 2021-03-18 16:11:36 +00:00
wikiwebhook_test.go

README.md

Go Reference

golang.org/x/build/cloudfns/wikiwebhook

Package wikiwebhook implements an Google Cloud Function HTTP handler that expects GitHub webhook change events.

Deploying

gcloud functions deploy GitHubWikiChangeWebHook \
  --project=symbolic-datum-552 \
  --runtime go113 \
  --trigger-http \
  --set-env-vars="PUBSUB_TOPIC=github.webhooks.golang.go.wiki,GCP_PROJECT=symbolic-datum-552,GITHUB_WEBHOOK_SECRET=$(gcloud --project=symbolic-datum-552 secrets versions access latest --secret=github-webhook-secret)"