WebDeployer automates the deployment of static websites to AWS EC2 instances using Docker and GitHub Actions. With a focus on simplicity and efficiency, WebDeployer leverages self-hosted runners to provide a secure and robust CI/CD pipeline for web applications.
- GitHub Repository: Ensure you have a GitHub repository where you want to deploy the website.
- EC2 Instance: An EC2 instance with Docker installed.
- Self-Hosted Runner: Set up on the EC2 instance.
- Log in to the AWS Management Console.
- Navigate to the EC2 dashboard and click "Launch Instance".
- Choose an Amazon Machine Image (AMI) (e.g., Ubuntu).
- Select an instance type (e.g., t2.micro for free tier).
- Configure instance details, including setting up security groups to allow SSH (port 22) and HTTP (port 80) access.
- Launch the instance and note the instance ID, public DNS, and private IP address.
ssh -i /path/to/your-key.pem ec2-user@your-ec2-instance-public-dns
sudo apt update -y
sudo apt install docker -y
sudo service docker start
sudo usermod -aG docker ec2-user
- Go to your GitHub repository.
- Click on the "Settings" tab.
- In the sidebar, click on "Actions" and then "Runners".
- Click the "New self-hosted runner" button.
- Follow the instructions to download and configure the runner application on your EC2 instance.
In your repository, create a directory: .github/workflows
.
Create a file named deploy.yml
in this directory.
name: Deploy
on: [push]
jobs:
deploy:
runs-on: self-hosted
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Check Current Directory
run: pwd
- name: Verify Docker Installation
run: |
sudo docker --version
sudo systemctl status docker
- name: Check Running Docker Services
run: sudo docker ps -a
- name: Stop Docker Container
run: sudo docker stop csn-nginx || true
- name: Delete the Docker Container
run: sudo docker rm csn-nginx || true
- name: Build Nginx Docker Image
run: sudo docker build -t csn-nginx .
- name: Run the Docker Container
run: sudo docker run -d --name csn-nginx -p 8080:80 csn-nginx
- name: Check Running Docker Services After Deployment
run: sudo docker ps -a
- name: Check Docker Container Logs
run: sudo docker logs csn-nginx
- name: Test the Web Service
run: |
echo "Waiting for the service to start..."
sleep 10 # Wait for 10 seconds to give the service time to start
curl -v http://localhost:8080 || (echo "Curl failed"; sudo docker logs csn-nginx)
Push changes to the main branch of your repository. This will trigger the GitHub Actions workflow.
Go to the "Actions" tab in your GitHub repository to monitor the progress of your workflow.
This setup leverages GitHub Actions to deploy a website to an EC2 instance using a self-hosted runner. The workflow checks out your code, builds a Docker image, and runs a Docker container to serve your website via Nginx. By using a self-hosted runner, the deployment is executed directly on your EC2 instance, ensuring a secure and efficient workflow.
By following these steps, you can securely and efficiently deploy your website to an EC2 instance using GitHub Actions with a self-hosted runner.