Skip to content

Commit

Permalink
cloudfns/wikiwebhook: set GCP_PROJECT env var explicitly
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
dmitshur committed Mar 18, 2021
1 parent ef3d188 commit 884a8de
Show file tree
Hide file tree
Showing 4 changed files with 438 additions and 56 deletions.
2 changes: 1 addition & 1 deletion cloudfns/wikiwebhook/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ gcloud functions deploy GitHubWikiChangeWebHook \
--project=symbolic-datum-552 \
--runtime go113 \
--trigger-http \
--set-env-vars="PUBSUB_TOPIC=github.webhooks.golang.go.wiki,GITHUB_WEBHOOK_SECRET=$(gcloud --project=symbolic-datum-552 secrets versions access latest --secret=github-webhook-secret)"
--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)"
```
9 changes: 1 addition & 8 deletions cloudfns/wikiwebhook/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,4 @@ module golang.org/x/build/cloudfns/wikiwebhook

go 1.13

require (
cloud.google.com/go v0.34.0
github.com/googleapis/gax-go v2.0.2+incompatible // indirect
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 // indirect
google.golang.org/api v0.1.0 // indirect
google.golang.org/genproto v0.0.0-20190111180523-db91494dd46c // indirect
google.golang.org/grpc v1.18.0 // indirect
)
require cloud.google.com/go/pubsub v1.10.1

0 comments on commit 884a8de

Please sign in to comment.