Deploy application on GKE using Github Actions
Follow the below steps to setup Github Actions workflow.
Prerequisite:
- Create service account in GCP and generate key file.
- Convert key file into base64 format and using the output to create secret variable in github repo.
Steps:
- In your github repo create .github/workflow directory
- Under workflow directory create a file called deploy.yml and paste below code into it.
name: Build-Deploy
on:
push:
branches: [ "anup_gke" ]
env:
PROJECT_ID: <google-project-id> # ${{ secrets.GKE_PROJECT }}
GKE_CLUSTER: <GKE-Cluster-name> # TODO: update to cluster name
GKE_ZONE: us-central1-a # TODO: update to cluster zone
DEPLOYMENT_NAME: gke-test # TODO: update to deployment name
IMAGE_FLASK: <image name>
IMAGE_NGINX: <image name>
jobs:
build-deploy-gke:
name: Login, Build, Publish, and Deploy
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- id: 'auth'
name: Google Authentication
uses: 'google-github-actions/auth@v1'
with:
credentials_json: '${{ secrets.GKE_SA_KEY }}'
# Setup gcloud CLI
- uses: google-github-actions/setup-gcloud@v1
name: Setup gcloud CLI
# Configure Docker to use the gcloud command-line tool as a credential
# helper for authentication
- name: 'Configure Docker to use the gcloud command-line tool'
run: |-
gcloud --quiet auth configure-docker
# Build the Docker image for mvp-flask
- name: Build Docker Image of mvp-flask
run: |-
cd source/mvp-flask
docker build -t gcr.io/$PROJECT_ID/$IMAGE_FLASK:$GITHUB_RUN_ID .
# Build the Docker image for mvp-nginx
- name: Build Docker Image of mvp-nginx
run: |-
cd source/nginx
docker build -t gcr.io/$PROJECT_ID/$IMAGE_NGINX:$GITHUB_RUN_ID .
- name: Push docker images to GCR
run: |-
docker push gcr.io/$PROJECT_ID/$IMAGE_FLASK:$GITHUB_RUN_ID
docker push gcr.io/$PROJECT_ID/$IMAGE_NGINX:$GITHUB_RUN_ID
- name: 'Get GKE Credentails'
id: 'get-credentials'
uses: 'google-github-actions/get-gke-credentials@v1'
with:
cluster_name: ${{ env.GKE_CLUSTER }}
location: ${{ env.GKE_ZONE }}
- name: Deploy Docker Image on GKE Cluster
run: |-
kubectl set image deployment.apps/mvp-flask flask=gcr.io/<google-project-id>/$IMAGE_FLASK:$GITHUB_RUN_ID
kubectl set image deployment.apps/mvp-nginx nginx=gcr.io/<google-project-id>/$IMAGE_NGINX:$GITHUB_RUN_ID
- name: command
run: |-
kubectl get all
echo "###"
kubectl describe deployment.apps/mvp-flask
echo "###"
kubectl describe deployment.apps/mvp-nginx