In this guide, we’ll walk through automating the deployment of updates to a Google Cloud Platform (GCP) instance using GitHub Actions. This workflow simplifies the process of authenticating with GCP, updating the codebase, and restarting the application inside a Docker container.
Objective
- The goal is to create a seamless deployment pipeline for a web application running on a GCP Compute Engine instance. This workflow:
- Authenticates with GCP using a service account key.
- Configures the gcloud CLI for the target project and zone.
- Pulls the latest changes from the code repository.
- Builds and deploys the application using Docker.
GitHub Actions Workflow Configuration
Below is the GitHub Actions workflow file for the pipeline:
name: Authenticate with GCP and Update Instance
on:
push:
branches:
- main
jobs:
gcloud-auth:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Google Cloud Authentication
uses: 'google-github-actions/auth@v2'
with:
credentials_json: '${{ secrets.GCP_SERVICE_ACCOUNT_KEY }}'
- name: Install gcloud CLI
run: |
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates gnupg
echo "deb https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - sudo apt-get update && sudo apt-get install -y google-cloud-cli
- name: Configure gcloud CLI
run: |
gcloud config set project ${{ secrets.GCP_SERVICE_ACCOUNT_PROJECT }}
gcloud config set compute/zone us-central1-a
- name: Verify Authentication
run: gcloud compute instances list
- name: Update Instance with New Changes
run: |
gcloud compute ssh ${{ secrets.GCP_SERVICE_ACCOUNT_INSTANCE }} \
--zone us-central1-a \
--command "
sudo su -c 'ls -l / && \
cd /automation/ && \
ls -l && \
git pull && \
docker build -t client-end . && \
docker stop client-end-container || true && \
docker rm client-end-container || true && \
docker run -d -p 3000:3000 --name client-end-container client-end'"
Steps Breakdown
- Checkout the Code The workflow begins by checking out the repository's code using the actions/checkout action. This ensures that the latest changes from the repository are available.
- name: Checkout code
uses: actions/checkout@v3
- Authenticate with GCP This step uses the google-github-actions/auth action to authenticate with GCP. The credentials are securely stored in GitHub Secrets as GCP_SERVICE_ACCOUNT_KEY.
- name: Set up Google Cloud Authentication
uses: 'google-github-actions/auth@v2'
with:
credentials_json: '${{ secrets.GCP_SERVICE_ACCOUNT_KEY }}'
- Install gcloud CLI The gcloud CLI is installed on the runner. This tool is necessary for managing GCP resources and executing commands on the Compute Engine instance.
- name: Install gcloud CLI
run: |
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates gnupg
echo "deb https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo apt-get update && sudo apt-get install -y google-cloud-cli
- Configure gcloud CLI The gcloud CLI is configured to use the target GCP project and compute zone.
- name: Configure gcloud CLI
run: |
gcloud config set project ${{ secrets.GCP_SERVICE_ACCOUNT_PROJECT }}
gcloud config set compute/zone us-central1-a
- Verify Authentication The workflow verifies that the authentication and configuration are correct by listing the Compute Engine instances in the project.
- name: Verify Authentication
run: gcloud compute instances list
- Update the Instance Finally, the workflow connects to the Compute Engine instance via SSH, pulls the latest changes from the repository, builds the Docker image, and deploys it.
- name: Update Instance with New Changes
run: |
gcloud compute ssh ${{ secrets.GCP_SERVICE_ACCOUNT_INSTANCE }} \
--zone us-central1-a \
--command "
sudo su -c 'ls -l / && \
cd /automation/ && \
ls -l && \
git pull && \
docker build -t client-end . && \
docker stop client-end-container || true && \
docker rm client-end-container || true && \
docker run -d -p 3000:3000 --name client-end-container client-end'
"
Conclusion
This GitHub Actions workflow ensures that the application is automatically updated and redeployed on the GCP Compute Engine instance whenever changes are pushed to the main branch. By integrating GitHub Actions with GCP, this approach provides a reliable, repeatable, and scalable solution for managing deployments.
Top comments (0)