Skip to content

Commit

Permalink
Implement Github notification bot.
Browse files Browse the repository at this point in the history
Using https://github.com/GoogleCloudPlatform/cloud-build-notifiers as
a base, created a Github bot that will add the details of the Google
Cloud build to each pull request.

Closes #259
  • Loading branch information
markmandel committed May 18, 2021
1 parent 57dd4b6 commit fa5b629
Show file tree
Hide file tree
Showing 12 changed files with 821 additions and 0 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions build/ci/github-bot/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM golang AS build-env
COPY . /go-src
WORKDIR /go-src
RUN go build -o /go-app .

FROM gcr.io/distroless/base
COPY --from=build-env /go-app /
ENTRYPOINT ["/go-app", "--alsologtostderr", "--v=0"]
34 changes: 34 additions & 0 deletions build/ci/github-bot/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2021 Google LLC All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

STORAGE := gs://quilkin-ci-config

current_dir := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))

all: build deploy

# build: creates the project Cloud Build custom build step and hosts it on gcr.io
build:
gcloud builds submit . --config=cloudbuild.yaml $(ARGS)

# deploy pushes a new version to Cloud Run
deploy:
-gsutil mb $(STORAGE)
gsutil cp $(current_dir)/github.yaml $(STORAGE)
gcloud run deploy github-bot \
--image="us-docker.pkg.dev/quilkin/ci/github-bot" \
--platform managed \
--no-allow-unauthenticated \
--region=us-west3 \
--update-env-vars="CONFIG_PATH=$(STORAGE)/github.yaml"
30 changes: 30 additions & 0 deletions build/ci/github-bot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Github Notification Bot

This is the bot that created comments on GitHub pull requests whenever a
[Google Cloud Build](https://cloud.google.com/build) build passes or fails.

## Setup

### Image

Run `make build` to submit the cloud build to create the image that will be hosted on
[Cloud Run](https://cloud.google.com/run).

### Secrets

It is expected that there will be a secret named `gh-token` with a
[Github auth token](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) stored
in it, and the ${PROJECT_NUMBER}-compute@developer.gserviceaccount.com service account has the
role `roles/secretmanager.secretAccessor` for the `gh-token` secret.

### Deployment

Run `make deploy` to copy the config to the appropriate bucket, and deploy the notifier image to Cloud Run.

### Connect to Cloud Build

Follow https://cloud.google.com/build/docs/subscribe-build-notifications to create the
[Google Cloud PubSub topics](https://cloud.google.com/pubsub) for Google Cloud Build.

Follow https://cloud.google.com/run/docs/triggering/pubsub-push to setup the Notifier service to be triggered from
PubSub with the `cloud-builds` pubsub topic.
26 changes: 26 additions & 0 deletions build/ci/github-bot/cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2021 Google LLC All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

steps:
- name: gcr.io/cloud-builders/docker
args:
- build
- --tag=${_REGISTRY}/github-bot:latest
- '.'
images:
- ${_REGISTRY}/github-bot
options:
dynamic_substitutions: true
substitutions:
_REGISTRY: "us-docker.pkg.dev/${PROJECT_ID}/ci"
25 changes: 25 additions & 0 deletions build/ci/github-bot/github.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: cloud-build-notifiers/v1
kind: GithubNotifier
metadata:
name: github-notifier
spec:
notification:
filter: build.status == Build.Status.SUCCESS
delivery:
secrets:
- name: gh-token
value: projects/quilkin/secrets/gh-token/versions/latest
26 changes: 26 additions & 0 deletions build/ci/github-bot/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2021 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

module github-bot

go 1.16

require (
github.com/GoogleCloudPlatform/cloud-build-notifiers/lib/notifiers v0.0.0-20210219212036-163c92a64b27
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/google/go-github/v35 v35.2.0
golang.org/x/oauth2 v0.0.0-20210201163806-010130855d6c
golang.org/x/tools v0.1.1 // indirect
google.golang.org/genproto v0.0.0-20210513213006-bf773b8c8384
)
Loading

0 comments on commit fa5b629

Please sign in to comment.