Deploying to Google Cloud Run with Github Actions: A Step-by-Step Guide

Deploying to Google Cloud Run with Github Actions: A Step-by-Step Guide

What is Google Cloud Run?

Google Cloud Run is a serverless container platform that enables developers to run applications in a fully managed environment. It allows you to deploy stateless containers on a pay-as-you-go basis and auto-scales your application based on incoming traffic.

What is Github Actions?

GitHub Actions is a powerful workflow automation tool that allows developers to automate their development workflows. It integrates well with Google Cloud Run, making it easy to deploy applications from GitHub to Cloud Run.

In this article, I will be deploying a containerized web application to Google Cloud Run using GitHub Actions.

NOTE: If you want to use Gitlab CI/CD instead of GitHub Actions, see my other article here.

Let’s continue….


Before we dive into the tutorial, make sure you have the following:

  • A Google Cloud Platform account

  • A GitHub account

  • Docker installed on your local machine

Step 1: Set up your project on Google Cloud

Before we can deploy our application to Google Cloud Run, we need to create a new project on Google Cloud Platform and enable the Cloud Run API. Here’s how to do it:

  1. Go to the Google Cloud Console.

  2. Click on the project dropdown menu and select “New Project”.

  3. Give your project a name and click “Create”.

  4. Once your project is created, click on the “Activate Cloud Shell” button on the top right corner of the page.

  5. Run the following command to enable the Cloud Run API:

gcloud services enable

An alternative way to enable Cloud Run API

  1. Go to the Google Cloud Console and select your project.

  2. In the left navigation menu, click on “APIs & Services” and then “Dashboard.”

  3. Click on the “+ ENABLE APIS AND SERVICES” button.

  4. Search for “Cloud Run API” and click on it.

  5. Click the “Enable” button.Step 2: Create a Dockerfile

Next, we need to create a Dockerfile for our application. This file will contain instructions on how to build a container image for our application.

Here’s an example Dockerfile for a Node.js application:

# Use the official Node.js image
FROM node:14-alpine

# Set the working directory

# Copy the package.json and package-lock.json files
COPY package*.json ./

# Install the dependencies
RUN npm install --production

# Copy the rest of the application code
COPY . .

# Expose port 8080

# Start the application
CMD ["npm", "start"]

Save this file in the root directory of your project.

Step 3: Build and test the container locally

Before deploying our container to Google Cloud Run, let’s build and test it locally. Run the following command to build the container image:

docker build -t <your-image-name> .

Replace <your-image-name> with a name for your container image. Once the build is complete, run the container with the following command:

docker run -p 8080:8080 <your-image-name>

This will start the container and map port 8080 on your local machine to port 8080 inside the container. Open your web browser and go to http://localhost:8080 to test your application.

Step 4: Set up GitHub Actions

GitHub Actions is a powerful tool that allows you to automate your software development workflows. In this step, we will be creating a GitHub Actions workflow to build and deploy our container to Google Cloud Run.

  1. In your GitHub repository, click on the “Actions” tab.

  2. Click on the “Set up a workflow yourself” button.

  3. Replace the contents of the file with the following code:

name: "Deploy to Google Cloud Run"

      - main

    runs-on: ubuntu-latest
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Google Cloud SDK
        uses: google-github-actions/setup-gcloud@master
          project_id: <your-project-id>
          service_account_key: ${{ secrets.GCP_SA_KEY }}
          export_default_credentials: true

      - name: Configure docker for GCP
        run: gcloud auth configure-docker

      - name: Build and push Docker image
        uses: docker/build-push-action@v2
          context: .
          push: true
          build-args: |

      - name: Deploy to Cloud Run
        uses: google-github-actions/deploy-cloudrun@main
          service: <your-service-name>
          region: <your-region>
          platform: managed
          allow-unauthenticated: true
          env_vars: |

Replace <your-project-id>, <your-image-name>, <your-service-name>, and <your-region> with your own values.

You can see more here on how to use the google cloud run github actions.

4. Click on the “Start commit” button and commit the changes to the repository.

Step 5: Deploy to Google Cloud Run

Once the GitHub Actions workflow completes successfully, your container should be deployed to Google Cloud Run. To verify that your application is running, go to the Google Cloud Console, select your project, and click on “Cloud Run” in the sidebar. You should see your service listed there.

Click on the service to view its details, including the URL for your application. Open this URL in your web browser to test your deployed application.

Congratulations! You have successfully deployed a containerized web application to Google Cloud Run using GitHub Actions.

If you liked this article, please leave a clap or even a comment and don’t forget to follow me to get updated when I publish another one. Thanks!