Continuously Deploying a Flask App from a GitHub Repository to Google Cloud Run
Continuous deployment (CD) ensures that your application is always up-to-date with the latest features and fixes by automatically deploying code changes. Leveraging GitHub for source control and Google Cloud Platform (GCP) for deployment, you can streamline and automate this process for your Flask application. This guide will walk you through setting up continuous deployment for a Flask app using Google Cloud Run by linking your GitHub account directly with Google Cloud Run.
Prerequisites
- Google Cloud Account: Ensure you have an active Google Cloud account.
- GitHub Account: A GitHub account with a repository for your Flask app.
- Flask App: A simple Flask application ready to be deployed.
Step 1: Prepare Your Flask Application
Ensure your Flask application is structured correctly. Below is an example directory structure:
my-flask-app/
├── app.py
├── requirements.txt
└── Dockerfile
- app.py: Your main Flask application file.
- requirements.txt: Lists the Python dependencies.
- Dockerfile: Defines the environment for your Flask app.
from flask import Flask, render_template, url_for
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def principal():
return render_template("linecharts.html")
@app.route('/linecharts')
def linecharts():
return render_template("linecharts.html")
@app.route('/areacharts')
def areacharts():
return render_template("areacharts.html")
@app.route('/columncharts')
def columncharts():
return render_template("columncharts.html")
@app.route('/barcharts')
def barcharts():
return render_template("barcharts.html")
@app.route('/combocharts')
def combocharts():
return render_template("combocharts.html")
@app.route('/rangecharts')
def rangecharts():
return render_template("rangecharts.html")
@app.route('/timelinecharts')
def timelinecharts():
return render_template("timelinecharts.html")
@app.route('/funnelcharts')
def funnelcharts():
return render_template("funnelcharts.html")
@app.route('/candlestickcharts')
def candlestickcharts():
return render_template("candlestickcharts.html")
@app.route('/boxcharts')
def boxcharts():
return render_template("boxcharts.html")
@app.route('/piecharts')
def piecharts():
return render_template("piecharts.html")
@app.route('/radarcharts')
def radarcharts():
return render_template("radarcharts.html")
@app.route('/polarcharts')
def polarcharts():
return render_template("polarcharts.html")
@app.route('/radialcharts')
def radialcharts():
return render_template("radialcharts.html")
@app.route('/bubblecharts')
def bubblecharts():
return render_template("bubblecharts.html")
@app.route('/scattercharts')
def scattercharts():
return render_template("scattercharts.html")
@app.route('/heatmapcharts')
def heatmapcharts():
return render_template("heatmapcharts.html")
@app.route('/treemapcharts')
def treemapcharts():
return render_template("treemapcharts.html")
if __name__ == '__main__':
app.run(debug=False,port=8080,host="0.0.0.0")
requirements.txt:
aiosqlite==0.19.0
bidict==0.21.2
Bottleneck==1.3.7
Brotli==1.0.9
certifi==2024.2.2
cffi==1.16.0
charset-normalizer==2.0.4
click==8.1.7
colorama==0.4.6
cryptography==42.0.5
Flask==2.2.2
Flask-SocketIO==5.3.6
h11==0.14.0
idna==3.4
importlib-metadata==7.0.1
itsdangerous==2.0.1
Jinja2==3.1.3
MarkupSafe==2.1.3
numexpr==2.8.4
numpy==1.26.4
pandas==2.0.3
pip==23.3.1
pycparser==2.21
pyOpenSSL==24.0.0
PySocks==1.7.1
python-dateutil==2.8.2
python-engineio==4.9.0
python-socketio==5.11.2
pytz==2023.3.post1
requests==2.31.0
setuptools==68.2.2
simple-websocket==1.0.0
six==1.16.0
typing_extensions==4.9.0
tzdata==2023.3
urllib3==1.26.18
websockets==12.0
Werkzeug==2.3.8
wheel==0.41.2
win-inet-pton==1.1.0
wsproto==1.2.0
zipp==3.17.0
Dockerfile:
FROM python:3.11
# Set the working directory
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . ./
# Install the required dependencies
RUN pip install -r requirements.txt
# Make port 8080 available to the world outside this container
EXPOSE 8080
# Run app.py when the container launches
CMD python app.py
Step 2: Push Your Code to GitHub
Create a new repository on GitHub and push your Flask application code to it. You can do this via the command line:
Step 3: Set Up Google Cloud Project
- Create a New Project: Go to the Google Cloud Console and create a new project.
- Enable APIs: Enable the Cloud Run API.
- Set Up Billing: Ensure billing is enabled for your project.
Step 4: Link Your GitHub Repository with Google Cloud Run
- Navigate to Cloud Run: In the Google Cloud Console, navigate to Cloud Run.
- Create a New Service: Click on “Create Service”.
3. When prompted to select a source, choose “Continuously deploy from a source repository” and then click “Set up with Cloud Build”.
4. Authorize GitHub: Click on “Connect New Repository” and authorize Google Cloud to access your GitHub account.
5. Select Repository: Select the repository that contains your Flask app.
6. Configure Build Settings: Configure the build settings to specify the Dockerfile. Ensure the Dockerfile path is set correctly.
Step 5: Configure Build Settings
- Build Configuration: Select “Dockerfile” as the build type.
- Dockerfile Location: Ensure the path to the Dockerfile is correct (typically the root of the repository).
Step 6: Deploy the Service
- Service Settings: Configure the service settings, such as service name and region.
- Deployment Options: Set the options for the deployment, such as the CPU and memory allocation.
3. Allow Unauthenticated Invocations: Check the box to allow unauthenticated invocations if you want your app to be publicly accessible. Set container Port.
4. Create Service: Click “Create” to deploy the service.
Step 7: Verify the Deployment
Once the deployment is complete, you can find the service URL in the Google Cloud Console under Cloud Run. You can access this URL to see your Flask app in action.
Conclusion
By following these steps, you have successfully set up continuous deployment for your Flask application to Google Cloud Run by directly linking your GitHub repository. This setup ensures that your application is always up-to-date with the latest code changes, providing a reliable and scalable solution for your web application needs. Continuous deployment improves the efficiency of your development process and minimizes the risk of errors during manual deployments.
Notes
If you would like to clone and test the full projetct from GitHub
or view the project live, just click the link: