「技術分享」 Automatically deploy Flask app to Google App Engine with Git

Vicky
大橡科技 OakMega
2 min readMar 12, 2019

This tutorial will show you how to deploy a Flask app to GCP App Engine. Get rid of typing gcloud app deploy in the terminal and hanging on for long time deployment.

Prerequisites

  • A project in GCP
  • Flask app
  • Git

Let’s get started

Step 1: App Engine Configurations

The Flask app directory structure was simple. It should be like this.


└── flask_example
├── main.py
├── requirements.txt
├── app.yaml
└── deploy.yaml

Create an app.yaml file. Make sure you have gunicron in requirements.txt

service: default
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app
runtime_config:
python_version: 3

Step 2: Deploying a Flask app

With the codes ready. Create a deploy.yaml file for GCP deployment.

steps:
- name: "gcr.io/cloud-builders/gcloud"
args: ["app", "deploy", "--project=<your-project-name>"]
timeout: "1600s"

Step 3: Creating a build trigger

Our goal is to deploy our Flask app from local Git to App Engine when latest commit to branch master has been pushed.

GCP Cloud Build Trigger supports Cloud Source Repo, Github, and Bitbucket. We are using Cloud Source Repository in this example.

Navigate to Cloud Build > Triggers and Click Add trigger button.

Select Cloud Source Repository

Trigger Type: Branch

Branch: master

Select Cloud Build configuration file which we created on step 2.

Now simply push your master branch and it will deploy on App Engine default service 😊

git push origin master

--

--