kenneth-stack / WebDeployer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

WebDeployer

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.

Prerequisites

  • 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.

Step 1: Set Up Your EC2 Instance

Launch an EC2 Instance:

  1. Log in to the AWS Management Console.
  2. Navigate to the EC2 dashboard and click "Launch Instance".
  3. Choose an Amazon Machine Image (AMI) (e.g., Ubuntu).
  4. Select an instance type (e.g., t2.micro for free tier).
  5. Configure instance details, including setting up security groups to allow SSH (port 22) and HTTP (port 80) access.
  6. Launch the instance and note the instance ID, public DNS, and private IP address.

Connect to Your EC2 Instance:

ssh -i /path/to/your-key.pem ec2-user@your-ec2-instance-public-dns

Install Docker:

sudo apt update -y
sudo apt install docker -y
sudo service docker start
sudo usermod -aG docker ec2-user

Step 2: Configure the Self-Hosted Runner

Register the Runner with GitHub:

  1. Go to your GitHub repository.
  2. Click on the "Settings" tab.
  3. In the sidebar, click on "Actions" and then "Runners".
  4. Click the "New self-hosted runner" button.
  5. Follow the instructions to download and configure the runner application on your EC2 instance.

Step 3: Create a GitHub Actions Workflow

Create a Workflow File:

In your repository, create a directory: .github/workflows.

Create a file named deploy.yml in this directory.

Define the Workflow:

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)

Step 4: Trigger the Pipeline

Push Code Changes:

Push changes to the main branch of your repository. This will trigger the GitHub Actions workflow.

Monitor the Workflow:

Go to the "Actions" tab in your GitHub repository to monitor the progress of your workflow.

Summary

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.

About


Languages

Language:HTML 94.9%Language:Dockerfile 5.1%