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)