From 601bebc1b028b5deb6c8401ef435e9d89fc6ffa3 Mon Sep 17 00:00:00 2001 From: averikitsch Date: Tue, 14 Jan 2020 10:56:09 -0800 Subject: [PATCH] Fix license and update readmes --- example-workflows/README.md | 46 ++++++++ .../gke/.github/workflows/gke.yml | 25 ++--- example-workflows/gke/README.md | 102 +++++++++++------- example-workflows/gke/deployment.yml | 2 +- example-workflows/gke/kustomization.yml | 2 +- example-workflows/gke/nginx.conf | 2 +- example-workflows/gke/service.yml | 2 +- 7 files changed, 126 insertions(+), 55 deletions(-) create mode 100644 example-workflows/README.md diff --git a/example-workflows/README.md b/example-workflows/README.md new file mode 100644 index 00000000..6f0721b2 --- /dev/null +++ b/example-workflows/README.md @@ -0,0 +1,46 @@ +# Example Workflows for Github Actions with Google Cloud Platform + +These example workflows use the [setup-gcloud][action] to build and deploy applications to Google Cloud Platform. + +## Workflows + +| Workflow | Description | +| ------------------------------- | ------------------------ | +| [Google Kubernetes Engine](gke/)| Deploy a static site to an existing GKE cluster | +| [Cloud Run](cloud-run/) | Deploy a container to Cloud Run (Fully Managed)| + +## Before you begin + +1. [Create or select a Google Cloud Platform project][project]. + +1. Make sure that billing is enabled for your Google Cloud project. [Learn how to confirm billing is enabled for your project.][billing] + +1. [Create a Service Account][service-account] and download a [JSON key][key]. + + * Base64 encode your JSON key with the following command: + ``` + base64 ~/path/to/key/key.json + ``` + + * Add the encoded key to the [repository's secrets][secrets] (⚙ Settings > Secrets), named `GOOGLE_APPLICATION_CREDENTIALS`. + + * Add the service account email, ` +[action]: https://github.com/GoogleCloudPlatform/github-actions/tree/0c32120a9a2dda7fb9b392e6c3b90fa413b4642e/setup-gcloud +[project]: https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project +[billing]: https://cloud.google.com/billing/docs/how-to/modify-project +[service-account]: https://cloud.google.com/iam/docs/creating-managing-service-accounts#creating +[key]: https://cloud.google.com/iam/docs/creating-managing-service-account-keys#creating_service_account_keys +[secrets]: https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets diff --git a/example-workflows/gke/.github/workflows/gke.yml b/example-workflows/gke/.github/workflows/gke.yml index 9d674c1a..50c72bbc 100644 --- a/example-workflows/gke/.github/workflows/gke.yml +++ b/example-workflows/gke/.github/workflows/gke.yml @@ -1,4 +1,4 @@ -# Copyright 2019 Google Inc. +# Copyright 2019 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -17,7 +17,9 @@ # To configure this workflow: # # https://github.com/GoogleCloudPlatform/github-actions/tree/docs/service-account-key/setup-gcloud#inputs -# 1. Set up secrets in your workspace: GKE_PROJECT with the name of the project, GKE_EMAIL with the service account email, GKE_KEY with the Base64 encoded JSON service account key. +# 1. Set up secrets in your workspace: GKE_PROJECT with the name of the project, +# SA_EMAIL with the service account email, GOOGLE_APPLICATION_CREDENTIALS with +# the Base64 encoded JSON service account key. # # 2. Change the values for the GKE_ZONE, GKE_CLUSTER and IMAGE environment variables (below). @@ -31,10 +33,8 @@ on: # Environment variables available to all jobs and steps in this workflow env: GKE_PROJECT: ${{ secrets.GKE_PROJECT }} - GKE_EMAIL: ${{ secrets.GKE_EMAIL }} - GITHUB_SHA: ${{ github.sha }} - GKE_ZONE: us-west1-a GKE_CLUSTER: example-gke-cluster + GKE_ZONE: us-west1-a IMAGE: gke-test jobs: @@ -50,19 +50,19 @@ jobs: - uses: GoogleCloudPlatform/github-actions/setup-gcloud@master with: version: '270.0.0' - service_account_email: ${{ secrets.GKE_EMAIL }} - service_account_key: ${{ secrets.GKE_KEY }} + service_account_email: ${{ secrets.SA_EMAIL }} + service_account_key: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }} # Configure docker to use the gcloud command-line tool as a credential helper - run: | # Set up docker to authenticate # via gcloud command-line tool. gcloud auth configure-docker - + # Build the Docker image - name: Build - run: | - docker build -t gcr.io/"$GKE_PROJECT"/"$IMAGE":"$GITHUB_SHA" \ + run: | + docker build -t gcr.io/$GKE_PROJECT/$IMAGE:$GITHUB_SHA \ --build-arg GITHUB_SHA="$GITHUB_SHA" \ --build-arg GITHUB_REF="$GITHUB_REF" . @@ -70,7 +70,7 @@ jobs: - name: Publish run: | docker push gcr.io/$GKE_PROJECT/$IMAGE:$GITHUB_SHA - + # Set up kustomize - name: Set up Kustomize run: | @@ -80,7 +80,8 @@ jobs: # Deploy the Docker image to the GKE cluster - name: Deploy run: | - gcloud container clusters get-credentials $GKE_CLUSTER --zone $GKE_ZONE --project $GKE_PROJECT + gcloud container clusters get-credentials $GKE_CLUSTER \ + --zone $GKE_ZONE --project $GKE_PROJECT ./kustomize edit set image gcr.io/$GKE_PROJECT/$IMAGE:${GITHUB_SHA} ./kustomize build . | kubectl apply -f - kubectl rollout status deployment/gke-test diff --git a/example-workflows/gke/README.md b/example-workflows/gke/README.md index d8d1d264..3dd2e97a 100644 --- a/example-workflows/gke/README.md +++ b/example-workflows/gke/README.md @@ -1,51 +1,75 @@ - - # Google Kubernetes Engine - GitHub Actions An example workflow that uses [GitHub Actions](https://help.github.com/en/categories/automating-your-workflow-with-github-actions) to deploy [a static website](site/) to an existing [Google Kubernetes Engine](https://cloud.google.com/kubernetes-engine/) cluster. ## Workflow -The [example workflow](.github/workflows/gke.yml) will trigger on every push to this repo. +For pushes to the _default_ branch, `master`, the workflow will: -For pushes to the _feature_ branch, the workflow will: -1. Build the Docker image -1. Verify the Google Cloud Platform credentials are correct +1. Verify the Google Cloud Platform credentials are correct. -For pushes to the _default_ branch (`master`), in addition to the above Actions, the workflow will: -1. Tag and Push the image to Google Container Registry - * The image is available through the following tags: `latest`, the branch name, and first 8 of the commit SHA - * `gcloud` serves as a [credential helper](https://cloud.google.com/container-registry/docs/pushing-and-pulling) for Docker. This workflow registers `gcloud` as a credential helper and uses the 'docker' command within the `gcloud` action to push the image. -1. Use a Kubernetes Deployment to push an image to the Cluster - * Note that a GKE deployment requires a unique Tag to update the pods. Using a constant tag `latest` or a branch name `master` may result in successful workflows that don't update the cluster. +1. Build, tag, and push the image to Google Container Registry. -## Pre-reqs + * `gcloud` serves as a [credential helper](https://cloud.google.com/container-registry/docs/pushing-and-pulling) for Docker. This workflow registers `gcloud` as a + credential helper and uses the 'docker' command within the `gcloud` action + to push the image. -1. Google Cloud Platform project -1. GCP Service Account with write access to GCR and GKE for this project -1. GCP Service Account [credentials](https://cloud.google.com/iam/docs/creating-managing-service-account-keys) stored as a JSON key. Base64 encode the JSON key and paste the entire blob as a secret (Repository Settings --> Secrets) named `GKE_KEY`. -1. Also add Secrets for `GKE_PROJECT` and `GKE_EMAIL`. Those can be found in the raw key JSON above. -1. An existing Kubernetes Engine cluster - 1. [Create a Cluster](https://cloud.google.com/kubernetes-engine/docs/quickstart#create_cluster) -1. Edit `deployment.yml` to enter the correct GCR path to your image. Easy to find from GCR section of GCP console after first image push. -## Resources + * The image is available through the following tags: `latest`, the branch + name, and first 8 of the commit SHA. -#### Example Actions -* [`actions/setup-node`](https://github.com/actions/setup-node) -* [`actions/setup-gcloud`](https://github.com/GoogleCloudPlatform/github-actions/tree/master/setup-gcloud) -* [Actions in GitHub Marketplace](https://github.com/marketplace?type=actions) +1. Use a Kubernetes Deployment to push the image to the cluster. -#### Example Workflows -* [`actions/starter-workflows`](https://github.com/actions/starter-workflows) + * Note that a GKE deployment requires a unique Tag to update the pods. Using + a constant tag `latest` or a branch name `master` may result in successful + workflows that don't update the cluster. + +## Setup + +1. Create or reuse a Github repository for the example workflow: + + 1. [Create a repository](https://help.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-new-repository). + + 1. Move into your repository directory. + + 1. Copy the example into the repository: + ``` + cp -r /github-actions/example-workflows/gke/ ./ + ``` + +1. Add your Project Id to the [repository's secret][secrets], named `GKE_PROJECT`. + +1. [Create a GKE cluster][cluster] or select an existing GKE cluster. + +1. Update `deployment.yml` with the following values: + + * `GKE_CLUSTER`: the name of your cluster. + + * `GKE_ZONE`: the zone your cluster resides. + + You can find the names of your clusters using the command: `gcloud container clusters list`, +and the zone using the command: `gcloud container clusters describe `. + +1. [Add the the following roles to your service account][roles]: + + * `Kubernetes Engine Developer` + * `Storage Admin` + +## Run the workflow + +1. Add and commit your changes: +``` +git add . +git commit -m "Set up Github workflow" +``` + +1. Push to the `master` branch: +``` +git push origin master +``` + +1. View the workflow by selecting the `Actions` tab at the top of your repository. +Then click on the `Build and Deploy to GKE` workflow to see the details. + +[secrets]: https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets +[cluster]: https://cloud.google.com/kubernetes-engine/docs/quickstart#create_cluster +[roles]: https://cloud.google.com/iam/docs/granting-roles-to-service-accounts#granting_access_to_a_service_account_for_a_resource diff --git a/example-workflows/gke/deployment.yml b/example-workflows/gke/deployment.yml index 254f48f0..fc44d094 100644 --- a/example-workflows/gke/deployment.yml +++ b/example-workflows/gke/deployment.yml @@ -1,4 +1,4 @@ -# Copyright 2019 Google Inc. +# Copyright 2019 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/example-workflows/gke/kustomization.yml b/example-workflows/gke/kustomization.yml index 11d31338..989a38cf 100644 --- a/example-workflows/gke/kustomization.yml +++ b/example-workflows/gke/kustomization.yml @@ -1,4 +1,4 @@ -# Copyright 2019 Google Inc. +# Copyright 2019 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/example-workflows/gke/nginx.conf b/example-workflows/gke/nginx.conf index 343aeb6b..c58d6d58 100644 --- a/example-workflows/gke/nginx.conf +++ b/example-workflows/gke/nginx.conf @@ -1,4 +1,4 @@ -# Copyright 2019 Google Inc. +# Copyright 2019 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/example-workflows/gke/service.yml b/example-workflows/gke/service.yml index 87a540c3..2546aad8 100644 --- a/example-workflows/gke/service.yml +++ b/example-workflows/gke/service.yml @@ -1,4 +1,4 @@ -# Copyright 2019 Google Inc. +# Copyright 2019 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License.