playground: trigger playground releases from Go releases

This change includes configuration to build and deploy new releases of
the Go playground when a new tag of Go is released to Github. The
trigger is configured to filter tags to new, non-rc, non-beta releases
of Go.

This first commit simply implements releasing the current version of Go
configured for the playground. Changes to the build process to ensure
we're releasing the latest version of Go will be in a follow-up commit.

Updates golang/go#32606

Change-Id: I3b518fd3f02efcd8f510fd865f3370bea19f0e9b
Reviewed-on: https://go-review.googlesource.com/c/playground/+/183037
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
diff --git a/Makefile b/Makefile
index 4fe2ee7..b0c8651 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,8 @@
-.PHONY: docker test
+CLOUDBUILD_TRIGGER_ID := $(shell jq -r .id cloudbuild_trigger.json)
+CLOUDBUILD_TRIGGER_JSON := cloudbuild_trigger.json
+GCLOUD_ACCESS_TOKEN := $(shell gcloud auth print-access-token)
+
+.PHONY: docker test update-cloudbuild-trigger
 
 docker:
 	docker build -t golang/playground .
@@ -9,3 +13,9 @@
 	# Then run the slower tests, which happen as one of the
 	# Dockerfile RUN steps:
 	docker build -t golang/playground .
+
+update-cloudbuild-trigger:
+	# The gcloud CLI doesn't yet support updating a trigger.
+	curl -H "Authorization: Bearer $(GCLOUD_ACCESS_TOKEN)" -H "Content-Type: application/json" \
+		-d @$(CLOUDBUILD_TRIGGER_JSON) \
+		-X PATCH https://cloudbuild.googleapis.com/v1/projects/golang-org/triggers/$(CLOUDBUILD_TRIGGER_ID)
diff --git a/README.md b/README.md
index 33bdcfc..fffee73 100644
--- a/README.md
+++ b/README.md
@@ -5,30 +5,31 @@
 
 ## Building
 
-```
+```bash
 # build the image
 docker build -t playground .
 ```
 
 ## Running
 
-```
+```bash
 docker run --name=play --rm -d -p 8080:8080 playground
 # run some Go code
 cat /path/to/code.go | go run client.go | curl -s --upload-file - localhost:8080/compile
 ```
 
-# Deployment
+## Deployment
 
-Building the playground Docker container takes more than the default 10 minute time limit of cloud build, so increase its timeout first (note, `app/cloud_build_timeout` is a global configuration value):
+Building the playground Docker container takes more than the default 10 minute time limit of cloud build, so increase
+its timeout first (note, `app/cloud_build_timeout` is a global configuration value):
 
-```
+```bash
 gcloud config set app/cloud_build_timeout 1200  # 20 mins
 ```
 
 Alternatively, to avoid Cloud Build and build locally:
 
-```
+```bash
 make docker
 docker tag playground:latest gcr.io/golang-org/playground:latest
 docker push gcr.io/golang-org/playground:latest
@@ -37,11 +38,22 @@
 
 Then:
 
-```
+```bash
 gcloud --project=golang-org --account=you@google.com app deploy app.yaml
 ```
 
-# Contributing
+### Deployment Triggers
+
+Playground releases are also triggered when new tags are pushed to Github. The Cloud Build trigger configuration is
+defined in [cloudbuild_trigger.json](cloudbuild_trigger.json).
+
+Triggers can be updated by running the following Make target:
+
+```bash
+make update-cloudbuild-trigger
+```
+
+## Contributing
 
 To submit changes to this repository, see
 https://golang.org/doc/contribute.html.
diff --git a/cloudbuild_trigger.json b/cloudbuild_trigger.json
new file mode 100644
index 0000000..2fa8304
--- /dev/null
+++ b/cloudbuild_trigger.json
@@ -0,0 +1,50 @@
+{
+  "id": "5a2c9e25-a71a-4adf-a785-76c3eca2ac8a",
+  "description": "Go repository release trigger for x/playground",
+  "github": {
+    "name": "go",
+    "owner": "golang",
+    "push": {
+      "tag": "^go[0-9](\\.[0-9]+)+$"
+    }
+  },
+  "build": {
+    "steps": [
+      {
+        "name": "gcr.io/cloud-builders/git",
+        "args": [
+          "clone",
+          "--depth",
+          "1",
+          "https://go.googlesource.com/playground"
+        ]
+      },
+      {
+        "dir": "playground",
+        "name": "gcr.io/cloud-builders/docker",
+        "args": [
+          "build",
+          "-t",
+          "gcr.io/$PROJECT_ID/playground",
+          "."
+        ],
+        "timeout": "1800s"
+      },
+      {
+        "dir": "playground",
+        "name": "gcr.io/cloud-builders/gcloud",
+        "args": [
+          "app",
+          "deploy",
+          "app.yaml",
+          "--project=$PROJECT_ID",
+          "--image-url=gcr.io/$PROJECT_ID/playground:latest"
+        ],
+        "timeout": "1200s"
+      }
+    ],
+    "images": [
+      "gcr.io/$PROJECT_ID/playground"
+    ]
+  }
+}