๐ 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/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
๐ง Optional Enhancements
- Add
.env.examplefor 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
.envin 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. ๐ฅ
Top comments (0)