π 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.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. π₯
Top comments (0)