2021-10-22 14:47:00 +05:30
# 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.
#
2021-11-19 20:46:53 +03:00
# 2. Create and configure a Workload Identity Provider for GitHub (https://github.com/google-github-actions/auth#setting-up-workload-identity-federation)
2019-11-13 12:29:30 -08:00
#
2021-11-19 20:46:53 +03:00
# 3. Change the values for the GAR_LOCATION, GKE_ZONE, GKE_CLUSTER, IMAGE, REPOSITORY and DEPLOYMENT_NAME environment variables (below).
2020-08-03 10:47:03 -07:00
#
2021-11-19 20:46:53 +03: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-kustomize
2019-11-13 12:29:30 -08:00
name : Build and Deploy to GKE
on :
2021-10-22 14:47:00 +05:30
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 }}
2021-11-19 20:46:53 +03:00
GAR_LOCATION: us-central1 # TODO : update region of the Artifact Registry
2020-08-03 10:47:03 -07:00
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
2021-11-19 20:46:53 +03:00
REPOSITORY: samples # TODO : update to Artifact Registry docker repository
2020-08-03 10:47:03 -07:00
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
2022-01-30 09:07:53 +03:00
permissions :
contents : 'read'
id-token : 'write'
2020-08-03 10:47:03 -07:00
steps :
2019-11-13 12:29:30 -08:00
- name : Checkout
2022-03-28 13:10:48 -04:00
uses : actions/checkout@v3
2019-11-13 12:29:30 -08:00
2021-11-19 20:46:53 +03:00
# Configure Workload Identity Federation and generate an access token.
- id : 'auth'
name : 'Authenticate to Google Cloud'
2022-01-30 09:07:53 +03:00
uses : 'google-github-actions/auth@v0'
2019-11-13 12:29:30 -08:00
with :
2021-11-19 20:46:53 +03:00
token_format : 'access_token'
workload_identity_provider : 'projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider'
service_account : 'my-service-account@my-project.iam.gserviceaccount.com'
2020-08-03 10:47:03 -07:00
2022-01-30 09:07:53 +03:00
# Alternative option - authentication via credentials json
# - id: 'auth'
# uses: 'google-github-actions/auth@v0'
# with:
# credentials_json: '${{ secrets.GCP_CREDENTIALS }}'
2021-11-19 20:46:53 +03:00
- name : Docker configuration
run : |-
echo ${{steps.auth.outputs.access_token}} | docker login -u oauth2accesstoken --password-stdin https://$GAR_LOCATION-docker.pkg.dev
2020-08-03 10:47:03 -07:00
# Get the GKE credentials so we can deploy to the cluster
2021-11-19 20:46:53 +03:00
- name : Set up GKE credentials
2022-01-30 09:07:53 +03:00
uses : google-github-actions/get-gke-credentials@v0
2020-12-14 18:49:58 -06:00
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 }}
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 \
2021-11-19 20:46:53 +03:00
--tag "$GAR_LOCATION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY/$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" \
.
2021-11-19 20:46:53 +03:00
# Push the Docker image to Google Artifact Registry
2019-11-13 12:29:30 -08:00
- name : Publish
2020-08-03 10:47:03 -07:00
run : |-
2021-11-19 20:46:53 +03:00
docker push "$GAR_LOCATION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY/$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 : |-
2021-11-19 20:46:53 +03:00
# replacing the image name in the k8s template
./kustomize edit set image LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE:TAG=$GAR_LOCATION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY/$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