• Gavin In The Cloud
  • Posts
  • Automating Cloud Run Deployment for a Flask Application Using Cloud Build

Automating Cloud Run Deployment for a Flask Application Using Cloud Build

Effortless Flask Application Deployment on Google Cloud Run with Cloud Build

Automating Cloud Run Deployment for a Flask Application Using Cloud Build

Introduction: As cloud engineers, automation is fundamental to our workflow. In this blog post, we will guide you through the process of automating the deployment of a Flask application on Google Cloud Run using Cloud Build. We will cover the step-by-step implementation of provisioning the necessary resources, setting up the CI/CD pipeline, and achieving a seamless and efficient deployment process.

Prerequisites: Before we dive into the technical details, ensure that you have the following prerequisites in place:

  1. An GCP account with appropriate permissions to create resources like Google Cloud Build, Cloud Run services.

  2. A GitHub account with a repository set up to manage your Flask application and Cloud Build configurations.

Repo Structure: To maintain an organized project structure, we'll follow this directory structure within our GitHub repository: GitHub-repo

You can clone the sample repository for reference: GitHub-repo

Let's dive deep into your code to understand it in more detail:

  1. Dockerfile:

  • This file defines the Docker image for your Flask application.

  • It sets the Python version, creates a directory for your app, installs Python dependencies from requirements.txt, copies your source code into the container, exposes a port, defines a volume for persistent data, and runs your Flask app using Gunicorn.

FROM python:2.7

# Creating Application Source Code Directory
RUN mkdir -p /usr/src/app

# Setting Home Directory for containers
WORKDIR /usr/src/app

# Installing python dependencies
COPY requirements.txt /usr/src/app/
RUN pip install --no-cache-dir -r requirements.txt

# Copying src code to Container
COPY . /usr/src/app

# Application Environment variables
#ENV APP_ENV development
ENV PORT 8080

# Exposing Ports
EXPOSE $PORT

# Setting Persistent data
VOLUME ["/app-data"]

# Running Python Application
CMD gunicorn -b :$PORT -c gunicorn.conf.py main:app

2.cloudbuild.yaml:

  • This Cloud Build configuration file defines the steps for building and deploying your Docker image to Google Cloud Run.

  • It uses Docker to build the image and push it to Google Container Registry (GCR).

  • Then, it deploys the image to Cloud Run with specific settings like service name, image location, region, and allowing unauthenticated access.

steps:
# This step will build and push new container image to "google container registry(GCR)"
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/$PROJECT_ID/my-python-app', '.']
- name: 'gcr.io/cloud-builders/docker'
  args: ['push', 'gcr.io/$PROJECT_ID/my-python-app']

# This step will deploy the new container image to cloud run
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['run','deploy', 'my-python-app-service', '--image=gcr.io/$PROJECT_ID/my-python-app', '--region=us-central1', '--allow-unauthenticated']

3.gunicorn.conf.py:

  • This file is a Gunicorn configuration that customizes the way your Flask app is run.

  • It specifies the worker class as 'gthread,' which enables threaded concurrency.

  • It sets the number of workers to 2, which determines the number of CPU cores used for processing.

  • It also configures the number of threads for concurrency.

4.main.py:

  • This is the main Python script for your Flask application.

  • It defines a simple Flask app with a single route that returns a "Hello World" message when accessed.

  • It also includes an error handler for HTTP 500 errors and logs any server errors that occur.

5.main.pyc: 

This is a Python compiled file (bytecode) generated by Python's interpreter. It contains the compiled code of your main.py file. While it's not typically versioned in source control, it's automatically generated when you run your Python script. The Python interpreter uses this file to load your code more quickly in subsequent runs, as it doesn't need to recompile the source code

6.requirements.txt:

  • This file lists the Python packages and their versions required for your application. In this case, it specifies Flask, Gunicorn, and Futures.

Implementation Steps: Now, let's outline the steps to automate the deployment of your Flask application using Cloud Build and Google Cloud Run:

  1. Set Up a Google Cloud Project:

    • Create or use an existing Google Cloud Platform (GCP) project where you'll deploy your Flask application.

  2. Enable Necessary APIs:

    • In your GCP project, enable the Cloud Build, Container Registry, and Cloud Run APIs if they are not already enabled.

  3. Create a GitHub Repository:

    • Create a new GitHub repository or use an existing one to host your Flask application code.

  4. Create a Service Account Key:

    • Generate a service account key in JSON format in GCP with appropriate permissions for Cloud Build and Cloud Run.

  5. Add Secrets to GitHub Repository:

    • In your GitHub repository, go to "Settings" > "Secrets" and add the following secrets:

      • GCLOUD_PROJECT_ID: Your GCP project ID.

      • GOOGLE_CREDENTIALS: The content of the service account key JSON file you generated.

  6. Create a Cloud Build Trigger:

    • In GCP, set up a Cloud Build trigger linked to your GitHub repository. Configure it to use the cloudbuild.yaml file for builds.

  7. Push Code to GitHub:

    • Push your Flask application code (including Dockerfile, cloudbuild.yaml, gunicorn.conf.py, main.py, and requirements.txt) to your GitHub repository.

  8. Automated Deployment:

    • Whenever you push changes to your GitHub repository's main branch, the Cloud Build trigger will automatically build a Docker image, push it to GCR, and deploy it to Cloud Run.

  9. Access Your Application:

    • After the deployment is successful, you can access your Flask application on Google Cloud Run using the provided URL.

By following these implementation steps, your Flask application will be deployed and updated automatically whenever you push changes to your GitHub repository's main branch. This provides a streamlined CI/CD pipeline for your application on Google Cloud Run.


Conclusion: In this journey through automating Flask application deployment using Google Cloud Run and Cloud Build, we've streamlined the process, ensuring your Python apps run seamlessly, scale effortlessly, and remain effortlessly manageable. With this setup, you're poised to take full advantage of the cloud's power, enabling efficient, consistent, and automated deployments. Happy automating!