|
| 1 | +# 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 "main" branch. |
| 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. Create and configure a Workload Identity Provider for GitHub (https://github.com/google-github-actions/auth#setting-up-workload-identity-federation) |
| 8 | +# |
| 9 | +# 3. Change the values for the GAR_LOCATION, GKE_ZONE, GKE_CLUSTER, IMAGE, REPOSITORY and DEPLOYMENT_NAME environment variables (below). |
| 10 | +# |
| 11 | +# For more support on how to run the workflow, please visit https://github.com/google-github-actions/setup-gcloud/tree/master/example-workflows/gke-kustomize |
| 12 | + |
| 13 | +name: Build and Deploy to GKE |
| 14 | + |
| 15 | +on: |
| 16 | + push: |
| 17 | + branches: [ "main" ] |
| 18 | + |
| 19 | +env: |
| 20 | + PROJECT_ID: ${{ secrets.GKE_PROJECT }} |
| 21 | + GAR_LOCATION: us-central1 # TODO: update region of the Artifact Registry |
| 22 | + GKE_CLUSTER: cluster-1 # TODO: update to cluster name |
| 23 | + GKE_ZONE: us-central1-c # TODO: update to cluster zone |
| 24 | + DEPLOYMENT_NAME: gke-test # TODO: update to deployment name |
| 25 | + REPOSITORY: samples # TODO: update to Artifact Registry docker repository |
| 26 | + IMAGE: static-site |
| 27 | + |
| 28 | +jobs: |
| 29 | + setup-build-publish-deploy: |
| 30 | + name: Setup, Build, Publish, and Deploy |
| 31 | + runs-on: ubuntu-latest |
| 32 | + environment: production |
| 33 | + |
| 34 | + permissions: |
| 35 | + contents: 'read' |
| 36 | + id-token: 'write' |
| 37 | + |
| 38 | + steps: |
| 39 | + - name: Checkout |
| 40 | + uses: actions/checkout@v3 |
| 41 | + |
| 42 | + # Configure Workload Identity Federation and generate an access token. |
| 43 | + - id: 'auth' |
| 44 | + name: 'Authenticate to Google Cloud' |
| 45 | + uses: 'google-github-actions/auth@v0' |
| 46 | + with: |
| 47 | + token_format: 'access_token' |
| 48 | + workload_identity_provider: 'projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider' |
| 49 | + service_account: 'my-service-account@my-project.iam.gserviceaccount.com' |
| 50 | + |
| 51 | + # Alternative option - authentication via credentials json |
| 52 | + # - id: 'auth' |
| 53 | + # uses: 'google-github-actions/auth@v0' |
| 54 | + # with: |
| 55 | + # credentials_json: '${{ secrets.GCP_CREDENTIALS }}' |
| 56 | + |
| 57 | + - name: Docker configuration |
| 58 | + run: |- |
| 59 | + echo ${{steps.auth.outputs.access_token}} | docker login -u oauth2accesstoken --password-stdin https://$GAR_LOCATION-docker.pkg.dev |
| 60 | + # Get the GKE credentials so we can deploy to the cluster |
| 61 | + - name: Set up GKE credentials |
| 62 | + uses: google-github-actions/get-gke-credentials@v0 |
| 63 | + with: |
| 64 | + cluster_name: ${{ env.GKE_CLUSTER }} |
| 65 | + location: ${{ env.GKE_ZONE }} |
| 66 | + |
| 67 | + # Build the Docker image |
| 68 | + - name: Build |
| 69 | + run: |- |
| 70 | + docker build \ |
| 71 | + --tag "$GAR_LOCATION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY/$IMAGE:$GITHUB_SHA" \ |
| 72 | + --build-arg GITHUB_SHA="$GITHUB_SHA" \ |
| 73 | + --build-arg GITHUB_REF="$GITHUB_REF" \ |
| 74 | + . |
| 75 | + # Push the Docker image to Google Artifact Registry |
| 76 | + - name: Publish |
| 77 | + run: |- |
| 78 | + docker push "$GAR_LOCATION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY/$IMAGE:$GITHUB_SHA" |
| 79 | + # Set up kustomize |
| 80 | + - name: Set up Kustomize |
| 81 | + run: |- |
| 82 | + curl -sfLo kustomize https://github.com/kubernetes-sigs/kustomize/releases/download/v3.1.0/kustomize_3.1.0_linux_amd64 |
| 83 | + chmod u+x ./kustomize |
| 84 | + # Deploy the Docker image to the GKE cluster |
| 85 | + - name: Deploy |
| 86 | + run: |- |
| 87 | + # replacing the image name in the k8s template |
| 88 | + ./kustomize edit set image LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE:TAG=$GAR_LOCATION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY/$IMAGE:$GITHUB_SHA |
| 89 | + ./kustomize build . | kubectl apply -f - |
| 90 | + kubectl rollout status deployment/$DEPLOYMENT_NAME |
| 91 | + kubectl get services -o wide |
0 commit comments