Skip to content

Instantly share code, notes, and snippets.

@ahmed-abdelazim
Created November 14, 2024 07:11
Show Gist options
  • Save ahmed-abdelazim/8113c09d39225c665fe66392e128275b to your computer and use it in GitHub Desktop.
Save ahmed-abdelazim/8113c09d39225c665fe66392e128275b to your computer and use it in GitHub Desktop.
Nest project auto deployment with Github actions

Here's a complete tutorial that will guide you through setting up an automated deployment pipeline using GitHub Actions, Docker Compose, and Docker Hub. This will deploy any code pushed to the main branch to a Docker Compose server. Let’s break down the process step-by-step.

Prerequisites

  1. Docker & Docker Compose installed on your server.
  2. Docker Hub account or access to another Docker registry.
  3. GitHub repository containing your application code.
  4. SSH access to your Docker server.

Step 1: Create a Dockerfile

In the root of your project repository, create a Dockerfile. This file will contain instructions to build a Docker image for your application. Below is an example for a simple Node.js app:

# Use the official Node.js image as a base image
FROM node:14

# Set the working directory in the container
WORKDIR /app

# Copy package.json and package-lock.json to install dependencies
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code to the working directory
COPY . .

# Expose the port the app runs on
EXPOSE 3000

# Start the application
CMD ["npm", "start"]

This Dockerfile installs the dependencies, copies the application code, and exposes port 3000 for your app.

Step 2: Create a docker-compose.yml File

In the same directory as your Dockerfile, create a docker-compose.yml file. This file will define the services and images to deploy. Here’s an example configuration:

version: '3.8'

services:
  app:
    image: myusername/myapp:latest  # Replace with your Docker image name
    ports:
      - "3000:3000"  # Map host port 3000 to container port 3000
    restart: always

Replace myusername/myapp with your Docker Hub repository name (or another registry if you're using it).

Step 3: Push Code to GitHub

Push your Dockerfile and docker-compose.yml file to your GitHub repository if you haven’t already.

Step 4: Set Up GitHub Secrets

To allow GitHub Actions to access your Docker registry and server, add the following secrets in your GitHub repository:

  1. Go to Settings > Secrets and variables > Actions.
  2. Click New repository secret for each secret.

Add the following secrets:

  • DOCKER_USERNAME: Your Docker registry username.
  • DOCKER_PASSWORD: Your Docker registry password.
  • SSH_HOST: The IP or hostname of your Docker Compose server.
  • SSH_USERNAME: SSH username for your server.
  • SSH_PRIVATE_KEY: Your private SSH key for logging into the server.
  • DOCKER_IMAGE: Your Docker image name (e.g., myusername/myapp).

Step 5: Create a GitHub Actions Workflow

Inside your repository, create a new directory called .github/workflows. Inside this directory, create a file called deploy.yml. This file will contain the configuration for GitHub Actions to build and deploy your Docker container.

Here’s an example deploy.yml file:

name: Deploy to Docker Compose Server

on:
  push:
    branches:
      - main  # Trigger the workflow on push to the main branch

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout Code
      uses: actions/checkout@v2

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

    - name: Build and Push Docker Image
      run: |
        docker build -t ${{ secrets.DOCKER_IMAGE }}:latest .
        docker push ${{ secrets.DOCKER_IMAGE }}:latest

    - name: Deploy to Docker Compose Server
      uses: appleboy/[email protected]
      with:
        host: ${{ secrets.SSH_HOST }}
        username: ${{ secrets.SSH_USERNAME }}
        key: ${{ secrets.SSH_PRIVATE_KEY }}
        script: |
          cd /path/to/your/docker-compose  # Replace with your actual path
          docker-compose pull
          docker-compose up -d

Explanation of Each Step

  1. Checkout Code: This step pulls the latest code from the repository.
  2. Log in to Docker Hub: This logs in to Docker Hub using your credentials, allowing GitHub Actions to push the image.
  3. Build and Push Docker Image: This builds the Docker image and pushes it to the Docker Hub repository.
  4. Deploy to Docker Compose Server:
    • SSH into your Docker Compose server.
    • Navigate to the directory where docker-compose.yml is located (replace /path/to/your/docker-compose).
    • Pull the latest image and restart the Docker Compose services.

Step 6: Configure Your Server’s SSH Access

Ensure that your server's SSH configuration allows GitHub Actions to connect using the provided private key (SSH_PRIVATE_KEY).

  1. Add the corresponding public key (of SSH_PRIVATE_KEY) to the server’s ~/.ssh/authorized_keys file for the user specified in SSH_USERNAME.
  2. Confirm SSH access by running a test connection from your local machine.

Step 7: Push Your Code and Trigger the Deployment

With everything configured:

  1. Push any changes to the main branch of your repository.
  2. The GitHub Actions workflow will be triggered, building, pushing, and deploying your Docker image to the server.

Additional Notes

  • Docker Image Tagging: This example uses the latest tag, but you can use different tags if you prefer versioned deployments.
  • Deployment Branch: Modify the branches setting in the on.push section if you want to deploy from a branch other than main.

After completing these steps, you’ll have a continuous deployment setup that automatically builds and deploys your app whenever you push changes to your GitHub repository!

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