Hello all, my name is Krish Naik, and welcome to my YouTube channel. First of all, a very Happy Teachers Day to everyone out there. In this tutorial, we’re going to delve into GitHub Actions as we develop an end-to-end project with an automated workflow.
The project involves creating a simple Flask application, converting it into a Docker image, and then deploying or pushing it to Docker Hub. The entire process will be automated using CI/CD pipelines developed with GitHub Actions.
If you’d like to learn more from me, I have several affordable Udemy courses. You can find the links in the description of my YouTube video.
In this tutorial, we’re focusing on implementing a complete end-to-end project featuring both CI and CD pipelines. We'll start with the basics and gradually move on to more advanced topics.
Here's a brief overview of what we’re going to do:
First, create a folder for your project. Open Visual Studio Code or any other code editor and set up the following files:
Here's a step-by-step implementation of the desired workflow:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Hello, World!"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)
from app import app
def test_home():
response = app.test_client().get('/')
assert response.status_code == 200
assert response.data == b"Hello, World!"
FROM python:3.9-slim
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python", "app.py"]
Create a new folder named .github/workflows and inside it, create a file named cicd.yaml with the following content:
name: CI/CD for Dockerized Flask App
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.9
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flask pytest
- name: Run tests
run: pytest
build-and-publish:
needs: build-and-test
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and Push Docker image
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/flask-test-app:latest
Create your Docker Hub account and generate a personal access token. Go to your GitHub repository's settings, navigate to Secrets, and add secrets for DOCKER_USERNAME and DOCKER_PASSWORD.
Push your code to the repository, and GitHub Actions will automatically trigger the workflow. The stages include:
This tutorial provides a comprehensive guide to automating your CI/CD pipeline using GitHub Actions for a Dockerized Flask application. This workflow ensures continuous integration and continuous deployment, streamlining your development process.
If you found this tutorial helpful, be sure to check out the video for a more detailed walkthrough: