In this step-by-step tutorial you'll learn how to:
- Generate and upload an SSH deploy key to EC2
- Link your local Vite React project to a GitHub repo
- Configure GitHub Secrets for secure SSH access
- Create a GitHub Actions workflow that:
- Builds your Vite React app
- Copies build artifacts to your EC2 via SCP
- Deploys them to Nginx via SSH
- Install Nginx & Certbot on EC2 and enable HTTPS with Let's Encrypt
Let's dive in!
GitHub Actions is GitHub's built-in CI/CD platform. It lets you define workflows in YAML that automatically run on events (e.g. push
to main
), so you can:
- Build your code
- Test against multiple Node.js versions
- Deploy to any environment (EC2, Kubernetes, Firebase, etc.)
All without leaving GitHub.
- An EC2 instance (Ubuntu or Amazon Linux) with public IP
- A domain pointing to your EC2's IP (optional for HTTPS)
- Your local machine with Git, Node.js & Yarn (or npm) installed
- A Vite React project already created (if not, run
npm create vite@latest my-app --template react
)
This key lets GitHub Actions SSH into your EC2 without exposing your personal key.
ssh-keygen -t rsa -b 4096 \
-C "github-deploy" \
-f ~/.ssh/github_deploy_key \
-N ""
- Private key:
~/.ssh/github_deploy_key
- Public key:
~/.ssh/github_deploy_key.pub
cat ~/.ssh/github_deploy_key.pub | \
ssh EC2_USER@EC2_HOST "\
mkdir -p ~/.ssh && \
cat >> ~/.ssh/authorized_keys && \
chmod 700 ~/.ssh && \
chmod 600 ~/.ssh/authorized_keys
"
✔️ Now GitHub Actions can SSH in using your deploy key.
---
## 3️⃣ Configure GitHub Secrets
1. Go to your GitHub repo → **Settings** → **Secrets & variables** → **Actions**
2. Click **New repository secret** and add:
| Secret Name | Value |
|--------------|---------------------------------------------|
| `EC2_HOST` | `your.ec2.public.ip.or.domain` |
| `EC2_USERNAME` | `ubuntu` (or `ec2-user`) |
| `EC2_SSH_KEY`| *(paste contents of `~/.ssh/github_deploy_key`)* |
---
## 4️⃣ Link & Push Your Project to GitHub
```bash
cd path/to/your-vite-react-app
git init
git remote add origin https://github.com/YOUR_USER/YOUR_REPO.git
git add .
git commit -m "Initial commit"
git branch -M main
git push -u origin main
Pushing to main
triggers the workflow we'll create next.
mkdir -p .github/workflows
touch .github/workflows/deploy.yml
name: Deploy to EC2
on:
push:
branches: [main] # 或者是你希望触发部署的分支
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install dependencies
run: yarn install
- name: Build project
run: yarn build
- name: Upload via SCP
uses: appleboy/scp-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
source: "dist/*"
target: "/tmp/frontend"
- name: Deploy to Nginx directory
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
script: |
sudo rm -rf /usr/share/nginx/html/*
sudo cp -r /tmp/frontend/* /usr/share/nginx/html/
git add .github/workflows/deploy.yml
git commit -m "Add CI/CD workflow"
git push
Pushing now will automatically build your app and deploy it to Nginx on EC2!
# Ubuntu
ssh EC2_USER@EC2_HOST
sudo apt update && sudo apt install -y nginx
sudo ufw allow 'Nginx Full'
# Amazon Linux
# sudo yum install -y nginx
# sudo systemctl enable --now nginx
# Ubuntu
sudo apt install -y certbot python3-certbot-nginx
# Amazon Linux
# sudo yum install -y certbot python3-certbot-nginx
sudo certbot --nginx \
-d yourdomain.com \
-d www.yourdomain.com
Certbot will:
- Obtain SSL certificates
- Auto-modify your Nginx config
- Reload Nginx
echo "0 3 * * * certbot renew --quiet --deploy-hook 'systemctl reload nginx'" \
| sudo tee -a /etc/crontab
-
Every push to
main
now triggers:- Build of your Vite React app
- SCP to EC2
- SSH deploy to Nginx
-
Your site is served over HTTPS via Let's Encrypt
🔗 Ready to share this on LinkedIn?
Copy, paste and hit Post! 👏
Have questions or need a GitHub template repo? Drop a comment below! ⬇️