Skip to content

Instantly share code, notes, and snippets.

@SahilDhingraa
Last active August 9, 2024 18:51
Show Gist options
  • Save SahilDhingraa/e700370183faca6bf2ee3001bb60a9bb to your computer and use it in GitHub Desktop.
Save SahilDhingraa/e700370183faca6bf2ee3001bb60a9bb to your computer and use it in GitHub Desktop.
This GitHub Actions workflow automates the process of building a Docker image and deploying it to an EC2 instance.

GitHub Actions CI/CD Workflow for Docker and EC2 Deployment

This GitHub Actions workflow automates the process of building a Docker image and deploying it to an EC2 instance. It consists of two main jobs: build-docker and run-on-ec2.

Workflow YAML

name: OneHealth CI/CD

on:
  push:
    branches: ["main"]
  pull_request:
    branches: ["main"]

jobs:
  build-docker:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Log in to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.USERNAME }}
          password: ${{ secrets.PASSWORD }}

      - name: Build the Docker image
        run: docker build -t <docker-repository> .

      - name: Push the Docker image
        run: docker push <docker-repository>

  run-on-ec2:
    needs: build-docker
    runs-on: ubuntu-latest
    env:
      EC2_SSH_PRIVATE_KEY: ${{ secrets.SSH_SECRET }}
      EC2_URL: ${{ secrets.SSH_HOST }}
      EC2_USERNAME: ${{ secrets.SSH_USERNAME }}
    steps:
      - name: Setup SSH for EC2
        uses: omarhosny206/setup-ssh-for-ec2@v1.0.0
        with:
          EC2_SSH_PRIVATE_KEY: $EC2_SSH_PRIVATE_KEY
          EC2_URL: $EC2_URL
      - name: run the docker container on EC2
        run: |
          ssh -o StrictHostKeyChecking=no $EC2_USERNAME@$EC2_URL "
          sudo docker pull <docker-repository>
          sudo docker stop atom || true
          sudo docker rm atom || true
          sudo docker run --name atom -d -p 80:5000 <docker-repository>
          "

Workflow Explanation

  1. The build-docker job:

    • Checks out the code
    • Sets up Docker
    • Logs into Docker Hub
    • Builds the Docker image
    • Pushes the image to Docker Hub
  2. The run-on-ec2 job:

    • Connects to an EC2 instance
    • Pulls the newly built image
    • Stops and removes any existing container
    • Runs the new image in a container

Setup Instructions

To use this workflow, you need to add the following secrets to your GitHub repository:

  1. DOCKER_USERNAME: Your Docker Hub username
  2. DOCKER_PASSWORD: Your Docker Hub password or access token
  3. SSH_SECRET: The private SSH key for your EC2 instance
  4. SSH_HOST: The public DNS or IP address of your EC2 instance
  5. SSH_USERNAME: The username to log into your EC2 instance

To add these secrets:

  1. Go to your GitHub repository
  2. Click on 'Settings' > 'Secrets and variables' > 'Actions'
  3. Click 'New repository secret' for each secret you need to add

Remember to replace <docker-repo-name> with your actual Docker repository name in the workflow file.

Note

Ensure that your EC2 instance has Docker installed and that the user specified by SSH_USERNAME has the necessary permissions to run Docker commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment