2020-02-20 12:44:22 -05:00
# This workflow will build a docker container, publish it to Google Container Registry, and deploy it to GKE when a release is created
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.
#
2019-11-21 08:47:27 -05:00
# 2. 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 (https://github.com/GoogleCloudPlatform/github-actions/tree/docs/service-account-key/setup-gcloud#inputs).
2019-11-13 12:29:30 -08:00
#
2020-01-10 17:11:21 +01:00
# 3. Change the values for the GKE_ZONE, GKE_CLUSTER, IMAGE, REGISTRY_HOSTNAME and DEPLOYMENT_NAME environment variables (below).
2019-11-13 12:29:30 -08:00
name : Build and Deploy to GKE
on :
2020-02-20 10:01:16 -05:00
release :
types : [ created]
2019-11-13 12:29:30 -08:00
# 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
IMAGE : gke-test
2020-01-10 17:11:21 +01:00
REGISTRY_HOSTNAME : gcr.io
DEPLOYMENT_NAME : gke-test
2019-11-13 12:29:30 -08:00
jobs :
setup-build-publish-deploy :
name : Setup, Build, Publish, and Deploy
runs-on : ubuntu-latest
steps :
- 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
- uses : GoogleCloudPlatform/github-actions/setup-gcloud@master
with :
version : '270.0.0'
service_account_email : ${{ secrets.GKE_EMAIL }}
service_account_key : ${{ secrets.GKE_KEY }}
# 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 : |
2020-01-10 17:11:21 +01:00
docker build -t "$REGISTRY_HOSTNAME"/"$GKE_PROJECT"/"$IMAGE":"$GITHUB_SHA" \
2019-11-13 12:29:30 -08:00
--build-arg GITHUB_SHA="$GITHUB_SHA" \
--build-arg GITHUB_REF="$GITHUB_REF" .
# Push the Docker image to Google Container Registry
- name : Publish
run : |
2020-01-10 17:11:21 +01:00
docker push $REGISTRY_HOSTNAME/$GKE_PROJECT/$IMAGE:$GITHUB_SHA
2019-11-13 12:29:30 -08:00
# Set up kustomize
- name : Set up Kustomize
run : |
curl -o kustomize --location https://github.com/kubernetes-sigs/kustomize/releases/download/v3.1.0/kustomize_3.1.0_linux_amd64
chmod u+x ./kustomize
# Deploy the Docker image to the GKE cluster
- name : Deploy
run : |
gcloud container clusters get-credentials $GKE_CLUSTER --zone $GKE_ZONE --project $GKE_PROJECT
2020-01-10 17:11:21 +01:00
./kustomize edit set image $REGISTRY_HOSTNAME/$GKE_PROJECT/$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