|
| 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, 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). |
| 8 | +# |
| 9 | +# 3. Change the values for the GKE_ZONE, GKE_CLUSTER, IMAGE, REGISTRY_HOSTNAME and DEPLOYMENT_NAME environment variables (below). |
| 10 | + |
| 11 | +name: Build and Deploy to GKE |
| 12 | + |
| 13 | +on: |
| 14 | + release: |
| 15 | + types: [created] |
| 16 | + |
| 17 | +# Environment variables available to all jobs and steps in this workflow |
| 18 | +env: |
| 19 | + GKE_PROJECT: ${{ secrets.GKE_PROJECT }} |
| 20 | + GKE_EMAIL: ${{ secrets.GKE_EMAIL }} |
| 21 | + GITHUB_SHA: ${{ github.sha }} |
| 22 | + GKE_ZONE: us-west1-a |
| 23 | + GKE_CLUSTER: example-gke-cluster |
| 24 | + IMAGE: gke-test |
| 25 | + REGISTRY_HOSTNAME: gcr.io |
| 26 | + DEPLOYMENT_NAME: gke-test |
| 27 | + |
| 28 | +jobs: |
| 29 | + setup-build-publish-deploy: |
| 30 | + name: Setup, Build, Publish, and Deploy |
| 31 | + runs-on: ubuntu-latest |
| 32 | + steps: |
| 33 | + |
| 34 | + - name: Checkout |
| 35 | + uses: actions/checkout@v2 |
| 36 | + |
| 37 | + # Setup gcloud CLI |
| 38 | + - uses: GoogleCloudPlatform/github-actions/setup-gcloud@master |
| 39 | + with: |
| 40 | + version: '270.0.0' |
| 41 | + service_account_email: ${{ secrets.GKE_EMAIL }} |
| 42 | + service_account_key: ${{ secrets.GKE_KEY }} |
| 43 | + |
| 44 | + # Configure docker to use the gcloud command-line tool as a credential helper |
| 45 | + - run: | |
| 46 | + # Set up docker to authenticate |
| 47 | + # via gcloud command-line tool. |
| 48 | + gcloud auth configure-docker |
| 49 | + |
| 50 | + # Build the Docker image |
| 51 | + - name: Build |
| 52 | + run: | |
| 53 | + docker build -t "$REGISTRY_HOSTNAME"/"$GKE_PROJECT"/"$IMAGE":"$GITHUB_SHA" \ |
| 54 | + --build-arg GITHUB_SHA="$GITHUB_SHA" \ |
| 55 | + --build-arg GITHUB_REF="$GITHUB_REF" . |
| 56 | +
|
| 57 | + # Push the Docker image to Google Container Registry |
| 58 | + - name: Publish |
| 59 | + run: | |
| 60 | + docker push $REGISTRY_HOSTNAME/$GKE_PROJECT/$IMAGE:$GITHUB_SHA |
| 61 | + |
| 62 | + # Set up kustomize |
| 63 | + - name: Set up Kustomize |
| 64 | + run: | |
| 65 | + curl -o kustomize --location https://github.com/kubernetes-sigs/kustomize/releases/download/v3.1.0/kustomize_3.1.0_linux_amd64 |
| 66 | + chmod u+x ./kustomize |
| 67 | +
|
| 68 | + # Deploy the Docker image to the GKE cluster |
| 69 | + - name: Deploy |
| 70 | + run: | |
| 71 | + gcloud container clusters get-credentials $GKE_CLUSTER --zone $GKE_ZONE --project $GKE_PROJECT |
| 72 | + ./kustomize edit set image $REGISTRY_HOSTNAME/$GKE_PROJECT/$IMAGE:${GITHUB_SHA} |
| 73 | + ./kustomize build . | kubectl apply -f - |
| 74 | + kubectl rollout status deployment/$DEPLOYMENT_NAME |
| 75 | + kubectl get services -o wide |
0 commit comments