2021-10-11 12:53:15 -07:00
# This workflow will build a docker container, publish it to Google Container Registry, and deploy it to GKE when there is a push to the $default-branch branch.
2019-11-13 12:29:30 -08:00
#
# To configure this workflow:
#
# 1. Ensure that your repository contains the necessary configuration for your Google Kubernetes Engine cluster, including deployment.yml, kustomization.yml, service.yml, etc.
#
2020-08-10 16:42:41 -07:00
# 2. Set up secrets in your workspace: GKE_PROJECT with the name of the project and GKE_SA_KEY with the Base64 encoded JSON service account key (https://github.com/GoogleCloudPlatform/github-actions/tree/docs/service-account-key/setup-gcloud#inputs).
2019-11-13 12:29:30 -08:00
#
2020-08-10 16:43:03 -07:00
# 3. Change the values for the GKE_ZONE, GKE_CLUSTER, IMAGE, and DEPLOYMENT_NAME environment variables (below).
2020-08-03 10:47:03 -07:00
#
2020-12-14 18:49:58 -06:00
# For more support on how to run the workflow, please visit https://github.com/google-github-actions/setup-gcloud/tree/master/example-workflows/gke
2019-11-13 12:29:30 -08:00
name : Build and Deploy to GKE
on :
2021-10-11 12:53:15 -07:00
push :
branches :
- $default-branch
2019-11-13 12:29:30 -08:00
env :
2020-08-03 10:47:03 -07:00
PROJECT_ID : ${{ secrets.GKE_PROJECT }}
GKE_CLUSTER: cluster-1 # TODO : update to cluster name
GKE_ZONE: us-central1-c # TODO : update to cluster zone
DEPLOYMENT_NAME: gke-test # TODO : update to deployment name
IMAGE : static-site
2019-11-13 12:29:30 -08:00
jobs :
setup-build-publish-deploy :
name : Setup, Build, Publish, and Deploy
runs-on : ubuntu-latest
2021-02-13 13:39:55 -06:00
environment : production
2019-11-13 12:29:30 -08:00
2020-08-03 10:47:03 -07:00
steps :
2019-11-13 12:29:30 -08:00
- name : Checkout
2019-12-24 14:22:22 -08:00
uses : actions/checkout@v2
2019-11-13 12:29:30 -08:00
# Setup gcloud CLI
2020-12-14 18:38:42 -06:00
- uses : google-github-actions/setup-gcloud@v0.2.0
2019-11-13 12:29:30 -08:00
with :
2020-08-03 10:47:03 -07:00
service_account_key : ${{ secrets.GKE_SA_KEY }}
project_id : ${{ secrets.GKE_PROJECT }}
# Configure Docker to use the gcloud command-line tool as a credential
# helper for authentication
- run : |-
gcloud --quiet auth configure-docker
# Get the GKE credentials so we can deploy to the cluster
2020-12-14 18:49:58 -06:00
- uses : google-github-actions/get-gke-credentials@v0.2.1
with :
2020-12-14 18:59:18 -06:00
cluster_name : ${{ env.GKE_CLUSTER }}
2020-12-14 18:49:58 -06:00
location : ${{ env.GKE_ZONE }}
credentials : ${{ secrets.GKE_SA_KEY }}
2019-11-13 12:29:30 -08:00
# Build the Docker image
- name : Build
2020-08-03 10:47:03 -07:00
run : |-
docker build \
--tag "gcr.io/$PROJECT_ID/$IMAGE:$GITHUB_SHA" \
2019-11-13 12:29:30 -08:00
--build-arg GITHUB_SHA="$GITHUB_SHA" \
2020-08-03 10:47:03 -07:00
--build-arg GITHUB_REF="$GITHUB_REF" \
.
2019-11-13 12:29:30 -08:00
# Push the Docker image to Google Container Registry
- name : Publish
2020-08-03 10:47:03 -07:00
run : |-
docker push "gcr.io/$PROJECT_ID/$IMAGE:$GITHUB_SHA"
2019-11-13 12:29:30 -08:00
# Set up kustomize
- name : Set up Kustomize
2020-08-03 10:47:03 -07:00
run : |-
curl -sfLo kustomize https://github.com/kubernetes-sigs/kustomize/releases/download/v3.1.0/kustomize_3.1.0_linux_amd64
2019-11-13 12:29:30 -08:00
chmod u+x ./kustomize
# Deploy the Docker image to the GKE cluster
- name : Deploy
2020-08-03 10:47:03 -07:00
run : |-
./kustomize edit set image gcr.io/PROJECT_ID/IMAGE:TAG=gcr.io/$PROJECT_ID/$IMAGE:$GITHUB_SHA
2019-11-13 12:29:30 -08:00
./kustomize build . | kubectl apply -f -
2020-01-10 17:11:21 +01:00
kubectl rollout status deployment/$DEPLOYMENT_NAME
2019-11-20 21:29:34 -05:00
kubectl get services -o wide