DEV Community

Latchu@DevOps
Latchu@DevOps

Posted on

πŸš€ Build, Push, and Deploy a Python App image to Cloud Run Using Google Cloud Build Triggers

In this guide, we will build a Python Flask application, containerize it using Docker, push the image to Google Artifact Registry, and deploy it automatically to Cloud Run using Google Cloud Build Triggers.


1️⃣ Create a Private Repository in GitHub

Create a private GitHub repository and add the following files.

πŸ“„ app.py

from flask import Flask
import os

app = Flask(__name__)

@app.route("/")
def wish():
    message = "Happy birthday {name}"
    return message.format(name=os.getenv("NAME", "Latchu"))

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8080)
Enter fullscreen mode Exit fullscreen mode

πŸ“„ Dockerfile

FROM python:3.8.0-slim
WORKDIR /app
ADD . /app
RUN pip install --trusted-host pypi.python.org Flask
ENV NAME Mark
CMD ["python", "app.py"]
Enter fullscreen mode Exit fullscreen mode

2️⃣ Create an Artifact Registry Repository

Navigate to:

Artifact Registry β†’ Repositories β†’ Create Repository

Configure the repository as follows:

  • Name: pyapprepo-artifact
  • Format: Docker
  • Mode: Standard
  • Location type: Region
  • Region: asia-south2 (Delhi)
  • Cleanup policies: Dry run
  • Vulnerability scanning: Enabled

Click Create to create the repository.

1


3️⃣ Connect GitHub Repository to Cloud Build

Navigate to:

Cloud Build β†’ Triggers β†’ Connect repository

2

Steps:

  • Select Source Code Management Provider
  • Choose Region: asia-south2 (Delhi)
  • Select GitHub (Cloud Build GitHub App) β†’ Continue
  • Authenticate with GitHub
  • Select your account and repository
  • Click Connect β†’ Done

Your repository should now be connected successfully.

3


4️⃣ Create cloudbuild.yaml

Add the following cloudbuild.yaml file to your repository.

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: ['build','-t','asia-south2-docker.pkg.dev/latchu/pyapprepo-artifact/pyappimage:latest','.']

- name: 'gcr.io/cloud-builders/docker'
  args: ['push','asia-south2-docker.pkg.dev/latchu/pyapprepo-artifact/pyappimage:latest']

- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
  entrypoint: gcloud
  args:
  - 'run'
  - 'deploy'
  - 'pyflaskapp'
  - '--image'
  - 'asia-south2-docker.pkg.dev/latchu/pyapprepo-artifact/pyappimage:latest'
  - '--region'
  - 'asia-south2'
  - '--allow-unauthenticated'

options:
  defaultLogsBucketBehavior: REGIONAL_USER_OWNED_BUCKET
Enter fullscreen mode Exit fullscreen mode

5️⃣ Create a Cloud Build Trigger

Navigate to:

Cloud Build β†’ Triggers β†’ Create Trigger

Configure the trigger:

  • Name: mypyapptrigger
  • Region: asia-south2 (Delhi)
  • Event: Push to a branch
  • Source: Repository service β†’ Cloud Build repositories
  • Repository generation: 1st gen
  • Repository: Select your repo
  • Branch: .* (any branch)
  • Configuration: Autodetected (cloudbuild.yaml will be detected)
  • Location: Repository
  • Service account: Choose your service account
  • Leave other options as default
  • Click Create

6️⃣ Commit Code and Trigger the Pipeline

  • Make a small change in the code
  • Commit and push to GitHub
  • Cloud Build trigger will start automatically

7️⃣ Verify Deployment

4

  • Click on the running build in Cloud Build
  • View detailed logs for build, push, and deployment
  • Click the Cloud Run service URL
  • You should see the deployed Flask application running πŸŽ‰

5

6


βœ… Summary

This setup enables a complete CI/CD pipeline using:

  • GitHub (source code)
  • Cloud Build Triggers (automation)
  • Artifact Registry (Docker images)
  • Cloud Run (serverless deployment)

Every push to the repository automatically builds, pushes, and deploys your application.

Top comments (0)