|
| 1 | +# This workflow will build a docker container, publish it to Google Container Registry, and deploy it to GKE when a release is created |
| 2 | +# |
| 3 | +# To configure this workflow: |
| 4 | +# |
| 5 | +# 1. Ensure that your repository contains the necessary configuration for your Google Kubernetes Engine cluster, including deployment.yml, kustomization.yml, service.yml, etc. |
| 6 | +# |
| 7 | +# 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). |
| 8 | +# |
| 9 | +# 3. Change the values for the GKE_ZONE, GKE_CLUSTER, IMAGE, and DEPLOYMENT_NAME environment variables (below). |
| 10 | +# |
| 11 | +# For more support on how to run the workflow, please visit https://github.com/GoogleCloudPlatform/github-actions/tree/master/example-workflows/gke |
| 12 | + |
| 13 | +name: Build and Deploy to GKE |
| 14 | + |
| 15 | +on: |
| 16 | + release: |
| 17 | + types: [created] |
| 18 | + |
| 19 | +env: |
| 20 | + PROJECT_ID: ${{ secrets.GKE_PROJECT }} |
| 21 | + GKE_CLUSTER: gj-cluster-1 # TODO: update to cluster name |
| 22 | + GKE_ZONE: us-central1-c # TODO: update to cluster zone |
| 23 | + DEPLOYMENT_NAME: git-gke-test # TODO: update to deployment name |
| 24 | + IMAGE: static-site |
| 25 | + |
| 26 | +jobs: |
| 27 | + setup-build-publish-deploy: |
| 28 | + name: Setup, Build, Publish, and Deploy |
| 29 | + runs-on: ubuntu-latest |
| 30 | + |
| 31 | + steps: |
| 32 | + - name: Checkout |
| 33 | + uses: actions/checkout@v2 |
| 34 | + |
| 35 | + # Setup gcloud CLI |
| 36 | + - uses: GoogleCloudPlatform/github-actions/[email protected] |
| 37 | + with: |
| 38 | + service_account_key: ${{ secrets.GKE_SA_KEY }} |
| 39 | + project_id: ${{ secrets.GKE_PROJECT }} |
| 40 | + |
| 41 | + # Configure Docker to use the gcloud command-line tool as a credential |
| 42 | + # helper for authentication |
| 43 | + - run: |- |
| 44 | + gcloud --quiet auth configure-docker |
| 45 | +
|
| 46 | + # Get the GKE credentials so we can deploy to the cluster |
| 47 | + - run: |- |
| 48 | + gcloud container clusters get-credentials "$GKE_CLUSTER" --zone "$GKE_ZONE" |
| 49 | +
|
| 50 | + # Build the Docker image |
| 51 | + - name: Build |
| 52 | + run: |- |
| 53 | + docker build \ |
| 54 | + --tag "gcr.io/$PROJECT_ID/$IMAGE:$GITHUB_SHA" \ |
| 55 | + --build-arg GITHUB_SHA="$GITHUB_SHA" \ |
| 56 | + --build-arg GITHUB_REF="$GITHUB_REF" \ |
| 57 | + . |
| 58 | +
|
| 59 | + # Push the Docker image to Google Container Registry |
| 60 | + - name: Publish |
| 61 | + run: |- |
| 62 | + docker push "gcr.io/$PROJECT_ID/$IMAGE:$GITHUB_SHA" |
| 63 | +
|
| 64 | + # Set up kustomize |
| 65 | + - name: Set up Kustomize |
| 66 | + run: |- |
| 67 | + curl -sfLo kustomize https://github.com/kubernetes-sigs/kustomize/releases/download/v3.1.0/kustomize_3.1.0_linux_amd64 |
| 68 | + chmod u+x ./kustomize |
| 69 | +
|
| 70 | + # Deploy the Docker image to the GKE cluster |
| 71 | + - name: Deploy |
| 72 | + run: |- |
| 73 | + ./kustomize edit set image gcr.io/PROJECT_ID/IMAGE:TAG=gcr.io/$PROJECT_ID/$IMAGE:$GITHUB_SHA |
| 74 | + ./kustomize build . | kubectl apply -f - |
| 75 | + kubectl rollout status deployment/$DEPLOYMENT_NAME |
| 76 | + kubectl get services -o wide |
0 commit comments