🚀 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
└── ...
Also, ensure your .env
file is ignored:
# .gitignore
.env
node_modules
🔑 Step 2: Create FTP User in cPanel
- Log in to cPanel
- Go to FTP Accounts
- 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)
-
FTP Host (like
🧪 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/[email protected]
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
🧠 Optional Enhancements
- Add
.env.example
for reference. - Add scripts like:
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
}
🧪 Step 6: Push to GitHub
git add .
git commit -m "Initial deploy with CI/CD"
git push origin main
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)
- Create MySQL Database
- Create MySQL User
- Assign user to database
- Update
.env
in cPanel with:
DB_HOST=localhost
DB_USER=your_mysql_user
DB_PASS=your_mysql_pass
DB_NAME=your_database_name
🎉 Done!
Your Express + Node.js server will now auto-deploy to cPanel every time you push to GitHub. 🔥