An example workflow that uses GitHub Actions to deploy a simple hello app to an existing Google Kubernetes Engine cluster.
This code is intended to be an example. You will likely need to change or update values to match your setup.
For pushes to the main
branch, this workflow will:
-
Download and configure the Google Cloud SDK with the provided credentials.
-
Use a Kubernetes Deployment to push the image to the cluster.
- Note that a GKE deployment requires a unique Tag to update the pods. Using
a constant tag
latest
or a branch namemain
may result in successful workflows that don't update the cluster.
- Note that a GKE deployment requires a unique Tag to update the pods. Using
a constant tag
-
Create a new Google Cloud Project (or select an existing project) and enable the Container Registry and Kubernetes Engine APIs.
-
Create a new GKE cluster or select an existing GKE cluster.
-
Create or reuse a GitHub repository for the example workflow:
-
Move into the repository directory:
$ cd <repo>
-
Copy the example into the repository:
$ cp -r <path_to>/github-actions/example-workflows/gke/ .
-
Create a Google Cloud service account if one does not already exist.
-
Add the the following Cloud IAM roles to your service account:
Kubernetes Engine Developer
- allows deploying to GKE
Note: These permissions are overly broad to favor a quick start. They do not represent best practices around the Principle of Least Privilege. To properly restrict access, you should create a custom IAM role with the most restrictive permissions.
-
Create a JSON service account key for the service account.
Note: You won't require this if you are using self-hosted runners
-
Add the following secrets to your repository's secrets:
-
GKE_PROJECT
: Google Cloud project ID -
GKE_SA_KEY
: the content of the service account JSON file
-
-
Update
.github/workflows/gke.yml
to match the values corresponding to your VM:-
GKE_CLUSTER
- the instance name of your cluster -
GKE_ZONE
- the zone your cluster resides
You can find the names of your clusters using the command:
$ gcloud container clusters list --project $PROJECT_ID
and the zone using the command:
$ gcloud container clusters describe <CLUSTER_NAME>
-
-
Add and commit your changes:
$ git add . $ git commit -m "Set up GitHub workflow"
-
Push to the
main
branch:$ git push -u origin main
-
View the GitHub Actions Workflow by selecting the
Actions
tab at the top of your repository on GitHub. Then click on theDeploy simple nginx to GKE
element to see the details.