test
This project uses GitHub Actions to automatically build and deploy the Flask application to an AWS EC2 instance. Deployment is triggered on every push to the main branch.
Before you can use this CI/CD setup, ensure you have the following:
- AWS Account: An active AWS account with an EC2 instance configured.
- Ensure your EC2 instance has Docker installed and the user specified in
EC2_USERNAMEcan run Docker commands (e.g., is part of thedockergroup). - The EC2 instance's security group must allow inbound traffic on port 80 (or the host port you map in
docker-compose.ymlor the GitHub Actions workflow).
- Ensure your EC2 instance has Docker installed and the user specified in
- Docker Hub Account: A Docker Hub account to store the built Docker images.
- Docker (Local): Docker installed on your local machine if you wish to build and test the application locally.
The CI/CD workflow relies on several secrets that must be configured in your GitHub repository settings. Go to Settings > Secrets and variables > Actions and add the following repository secrets:
DOCKERHUB_USERNAME: Your Docker Hub username.DOCKERHUB_TOKEN: Your Docker Hub access token or password. This token needs permissions to push images to your Docker Hub repository.EC2_HOST: The public hostname or IP address of your EC2 instance.EC2_USERNAME: The username for SSHing into your EC2 instance (e.g.,ubuntu,ec2-user).EC2_PRIVATE_KEY: The private SSH key (contents of the.pemfile) for authenticating to your EC2 instance.
You need to customize the GitHub Actions workflow file (.github/workflows/deploy.yml) with your specific details. Look for the following placeholders and replace them:
-
Docker Image Name:
- In the
build-and-pushjob, underBuild and push Docker imagestep, changetags: your-dockerhub-username/your-repo-name:latestto your Docker Hub username and desired image name (e.g.,johndoe/my-flask-app:latest). - In the
deploy-to-ec2job, within thescriptsection, changedocker pull your-dockerhub-username/your-repo-name:latestto match the image name you set above. - Also in the
deploy-to-ec2job script, changedocker run ... your-dockerhub-username/your-repo-name:latestto match the image name.
- In the
-
EC2 Container Name:
- In the
deploy-to-ec2job, within thescriptsection, replace all instances ofyour-app-containerwith the name you want to use for the Docker container running on your EC2 instance (e.g.,my-flask-app-container). This affectsdocker stop,docker rm, anddocker run --name.
- In the
You can build and run the application locally using Docker before pushing changes or for development purposes.
Using Docker:
-
Build the image:
docker build -t my-flask-app .(Replace
my-flask-appwith your preferred image name) -
Run the container:
docker run -p 5000:5000 my-flask-app
This will run the app, and it will be accessible at
http://localhost:5000. The Flask app inside the container runs on port 5000, and we map it to port 5000 on the host.
Using Docker Compose:
Docker Compose uses the docker-compose.yml file which is configured to map host port 80 to container port 5000.
-
Build and run the services:
docker-compose up --build
The app will be accessible at
http://localhost:80. -
Run in detached mode (in the background):
docker-compose up -d
-
Stop the services:
docker-compose down
When changes are pushed to the main branch of your GitHub repository:
- The GitHub Actions workflow defined in
.github/workflows/deploy.ymlis automatically triggered. - Build and Push: The
build-and-pushjob executes.- It checks out the latest code.
- It logs into Docker Hub using the configured secrets.
- It builds a Docker image from the
Dockerfile. - The newly built image is tagged (e.g.,
your-dockerhub-username/your-repo-name:latest) and pushed to Docker Hub.
- Deploy to EC2: The
deploy-to-ec2job executes (only after thebuild-and-pushjob succeeds).- It securely connects to your AWS EC2 instance via SSH using the configured secrets.
- On the EC2 instance, it performs the following:
- Logs into Docker Hub (using secrets passed to the script).
- Pulls the latest Docker image that was just pushed from Docker Hub.
- Stops the currently running Docker container (if one exists with the specified name).
- Removes the old container.
- Starts a new Docker container from the newly pulled image, mapping port 80 on the EC2 instance to port 5000 in the container.
- Your application is now updated and accessible via the EC2 instance's public IP address or hostname (on port 80).