DEV Community

SWAPNIL AHMMED SHISHIR
SWAPNIL AHMMED SHISHIR

Posted on

CI/CD guide to deploy your Node.js + Express + MySQL project to cPanel via FTP every time you push to GitHub.

πŸš€ GitHub Actions CI/CD for Node.js + Express + MySQL on cPanel via FTP

πŸ“ Prerequisites

βœ… A Node.js + Express project

βœ… A cPanel hosting with FTP access

βœ… A MySQL database set up in cPanel

βœ… A GitHub repository

πŸ”§ Step 1: Prepare Your Project

Make sure your project is structured like:

/my-app
  β”œβ”€β”€ public/
  β”œβ”€β”€ routes/
  β”œβ”€β”€ .env
  β”œβ”€β”€ index.js (main server)
  β”œβ”€β”€ package.json
  └── ...
Enter fullscreen mode Exit fullscreen mode

Also, ensure your .env file is ignored:

# .gitignore
.env
node_modules
Enter fullscreen mode Exit fullscreen mode

πŸ”‘ Step 2: Create FTP User in cPanel

  1. Log in to cPanel
  2. Go to FTP Accounts
  3. Create an FTP user and note down:
    • FTP Host (like ftp.yourdomain.com)
    • FTP Username
    • FTP Password
    • FTP Port: typically 21
    • FTP Path (like /public_html/subdomain/ or similar)

πŸ§ͺ Step 3: Test FTP Access Locally

Use FileZilla or similar to test your credentials.

If successful, continue.

🧬 Step 4: Add Secrets in GitHub

Go to your GitHub repo β†’ Settings β†’ Secrets β†’ Actions and add:

Name Value
FTP_HOST ftp.yourdomain.com
FTP_USERNAME Your FTP username
FTP_PASSWORD Your FTP password
FTP_PORT 21 (or as needed)
FTP_PATH /public_html/subdomain/

βš™οΈ Step 5: Add GitHub Action Workflow

Create a new file:

πŸ“„ .github/workflows/deploy.yml

name: πŸš€ Deploy to cPanel via FTP

on:
  push:
    branches: [main] # or your default branch

jobs:
  ftp-deploy:
    name: πŸ“¦ Deploy using FTP
    runs-on: ubuntu-latest

    steps:
      - name: ⬇️ Checkout Repo
        uses: actions/checkout@v3

      - name: πŸ“‚ Upload via FTP
        uses: SamKirkland/FTP-Deploy-Action@v4.3.4
        with:
          server: ${{ secrets.FTP_HOST }}
          username: ${{ secrets.FTP_USERNAME }}
          password: ${{ secrets.FTP_PASSWORD }}
          protocol: ftp
          port: ${{ secrets.FTP_PORT }}
          local-dir: ./ # root of repo
          server-dir: ${{ secrets.FTP_PATH }}
          exclude: |
            **/.git*
            **/.github*
            node_modules/
            .env
Enter fullscreen mode Exit fullscreen mode

🧠 Optional Enhancements

  • Add .env.example for reference.
  • Add scripts like:
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js"
  }
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ Step 6: Push to GitHub

git add .
git commit -m "Initial deploy with CI/CD"
git push origin main
Enter fullscreen mode Exit fullscreen mode

GitHub Actions will now:

  • Build your project (if needed)
  • Upload your files via FTP automatically to cPanel

🐞 Common Troubleshooting

Issue Fix
FTP login fails Double-check username, password, and port
Files not uploaded Make sure correct server-dir is set
Too many files skipped Check the exclude list

πŸ› οΈ MySQL Setup (in cPanel)

  1. Create MySQL Database
  2. Create MySQL User
  3. Assign user to database
  4. Update .env in cPanel with:
   DB_HOST=localhost
   DB_USER=your_mysql_user
   DB_PASS=your_mysql_pass
   DB_NAME=your_database_name
Enter fullscreen mode Exit fullscreen mode

πŸŽ‰ Done!

Your Express + Node.js server will now auto-deploy to cPanel every time you push to GitHub. πŸ”₯

Top comments (0)