Auto Deploying a Strapi Project to AWS EC2 with GitHub Actions

Automate the deployment of your Strapi project to AWS EC2 using GitHub Actions. This guide will help you set up continuous deployment for efficient and streamlined project management.

 · 2 min read

Table of Contents

  1. Prerequisites
  2. Setting Up AWS EC2
  3. Configuring GitHub Repository
  4. Creating GitHub Actions Workflow
  5. Setting Up Environment Variables
  6. Running the Workflow
  7. Conclusion

1. Prerequisites

Before we begin, ensure you have the following: - An AWS account - An EC2 instance set up with SSH access - A Strapi project hosted on GitHub - Basic knowledge of GitHub Actions

2. Setting Up AWS EC2

  1. Launch EC2 Instance: Follow this guide to set up an EC2 instance.
  2. Install Dependencies: SSH into your EC2 instance and install Node.js, npm, and PM2:

    sudo apt update
    sudo apt install -y nodejs npm
    sudo npm install -g pm2
    
  3. Clone Your Strapi Project:

    git clone https://github.com/your-repo/your-strapi-project.git
    cd your-strapi-project
    npm install
    pm2 start npm -- start
    

3. Configuring GitHub Repository

  1. Add Secrets: Go to your GitHub repository, navigate to Settings > Secrets and variables > Actions > New repository secret, and add the following secrets:
    • AWS_ACCESS_KEY_ID
    • AWS_SECRET_ACCESS_KEY
    • EC2_HOST
    • EC2_USER (usually ubuntu for Ubuntu instances)
    • EC2_KEY (the content of your .pem file)

4. Creating GitHub Actions Workflow

  1. Create Workflow File: In your GitHub repository, create a .github/workflows/deploy.yml file with the following content:
    name: Deploy to AWS EC2
    
    on:
      push:
        branches:
          - main
    
    jobs:
      deploy:
        runs-on: ubuntu-latest
    
        steps:
        - name: Checkout code
          uses: actions/checkout@v2
    
        - name: Set up Node.js
          uses: actions/setup-node@v2
          with:
            node-version: '14'
    
        - name: Install dependencies
          run: npm install
    
        - name: Deploy to EC2
          uses: appleboy/ssh-action@v0.1.3
          with:
            host: ${{ secrets.EC2_HOST }}
            username: ${{ secrets.EC2_USER }}
            key: ${{ secrets.EC2_KEY }}
            script: |
              cd your-strapi-project
              git pull origin main
              npm install
              pm2 restart all
    

5. Setting Up Environment Variables

  1. Environment Variables on EC2: Ensure your environment variables are set up on your EC2 instance, either through a .env file or directly in the system environment.

6. Running the Workflow

  1. Push to Main Branch: Push your changes to the main branch of your GitHub repository. This will trigger the GitHub Actions workflow to deploy your Strapi project to AWS EC2.

7. Conclusion

Automating your Strapi project deployment to AWS EC2 with GitHub Actions simplifies your workflow and ensures your project is always up-to-date with the latest changes. Follow this guide to set up continuous deployment efficiently.

If you found this tutorial helpful, share it with your network and subscribe to our blog for more web development and Strapi tutorials!


No comments yet.

Add a comment
Ctrl+Enter to add comment