Efficient Deployment with GitHub Actions & EC2: A CI/CD Journey
In today's fast-paced software development landscape, continuous integration and continuous deployment (CI/CD) have become indispensable practices. They enable teams to automate the building, testing, and deployment of applications, resulting in faster release cycles, higher-quality code, and improved collaboration among developers.
GitHub Actions, a powerful workflow automation tool provided by GitHub, empowers developers to implement CI/CD pipelines seamlessly within their repositories. In this article, we'll delve into a practical example of setting up a CI/CD pipeline using GitHub Actions for a Flask application deployed on an EC2 instance.
Understanding the Workflow:
Our project revolves around a Flask application hosted on an EC2 instance. The CI/CD pipeline is triggered whenever changes are pushed to the main branch of the GitHub repository. The workflow consists of two YAML files: ci.yml
and cd.yml
.
CI Workflow (ci.yml):
Upon a push to the main branch, the CI workflow (ci.yml) is triggered.
This workflow utilizes GitHub-hosted runners and comprises three main stages:
Running test cases that are written in test_app.py
Login to DockerHub: Authentication to DockerHub to access the container registry.
Build Docker Image: Building the Docker image for the Flask application.
Push Docker Image: Pushing the built Docker image to DockerHub for storage and accessibility.
CD Workflow (cd.yml):
After the successful completion of the CI workflow, the CD workflow (cd.yml) is triggered.
This workflow runs on self-hosted runners configured on an EC2 instance.
The CD workflow involves the following stages:
Pull Docker Image: Pull the latest Docker image from DockerHub.
Delete Old Container: Removing the existing container instance running on the EC2 instance.
Run Docker Container: Spinning up a new Docker container with the updated image.
Implementing the Workflow:
To implement this CI/CD pipeline, follow these steps:
Setting Up GitHub Actions:
For a more detailed setup guide for this project, please visit the repository linked below:
Create two YAML files,
ci.yml
andcd.yml
, in the.github/workflows
directory of your GitHub repository.Define the workflow steps within each YAML file according to the stages mentioned above.
Configuring Self-Hosted Runners on EC2:
Set up an EC2 instance with Docker installed.
Install and configure the GitHub Actions runner on the EC2 instance.
Ensure the runner is registered with your GitHub repository and configured to execute the
cd.yml
workflow.example of running the cd.yml in a self-hosted runner in EC2
Deploying the Flask Application:
Ensure your Flask application is containerized using Docker.
Push the Dockerfile and application code to your GitHub repository.
Configure the Dockerfile to expose the Flask application on port 5000.
Accessing the Deployed Application:
Once the CI/CD pipeline is set up and triggered, navigate to
http://<ec2_public_ip>:5000
access the deployed Flask application.Any updates pushed to the GitHub repository will automatically trigger the pipeline, ensuring seamless deployment of changes to the production environment.
Conclusion:
By leveraging GitHub Actions for CI/CD, we've established an efficient pipeline for automating the deployment of a Flask application. This streamlined approach enhances development agility, improves code quality, and accelerates time-to-market. With continuous integration and deployment becoming increasingly essential in modern software development, adopting tools like GitHub Actions empowers teams to deliver value to end-users rapidly and reliably.
Subscribe to my newsletter
Read articles from Hemanth Gangula directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Hemanth Gangula
Hemanth Gangula
๐ Passionate about cloud and DevOps, I'm a technical writer at Hasnode, dedicated to crafting insightful blogs on cutting-edge topics in cloud computing and DevOps methodologies. Actively seeking opportunities in the DevOps domain, I bring a blend of expertise in AWS, Docker, CI/CD pipelines, and Kubernetes, coupled with a knack for automation and innovation. With a strong foundation in shell scripting and GitHub collaboration, I aspire to contribute effectively to forward-thinking teams, revolutionizing development pipelines with my skills and drive for excellence. #DevOps #AWS #Docker #CI/CD #Kubernetes #CloudComputing #TechnicalWriter