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
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# GitHub Auto-Deployment Setup Guide
|
| 2 |
+
|
| 3 |
+
## Overview
|
| 4 |
+
Your code is now on GitHub and ready for auto-deployment to AWS EC2!
|
| 5 |
+
|
| 6 |
+
**What happens now:**
|
| 7 |
+
1. You push code to GitHub β GitHub Actions automatically deploys to EC2
|
| 8 |
+
2. No more manual SSH or file uploads needed!
|
| 9 |
+
|
| 10 |
+
## Step 1: Add GitHub Secrets (Required)
|
| 11 |
+
|
| 12 |
+
You need to add 3 secrets to your GitHub repository:
|
| 13 |
+
|
| 14 |
+
### 1. Go to GitHub Repository Settings
|
| 15 |
+
1. Open: https://github.com/KiWA001/kai-api-gateway
|
| 16 |
+
2. Click **"Settings"** tab (top right)
|
| 17 |
+
3. In left sidebar, click **"Secrets and variables"** β **"Actions"**
|
| 18 |
+
4. Click **"New repository secret"**
|
| 19 |
+
|
| 20 |
+
### 2. Add These 3 Secrets:
|
| 21 |
+
|
| 22 |
+
**Secret 1: EC2_SSH_KEY**
|
| 23 |
+
- Name: `EC2_SSH_KEY`
|
| 24 |
+
- Value: Copy and paste the ENTIRE contents of your .pem key file
|
| 25 |
+
- (Open the .pem file in a text editor, select all, copy, paste)
|
| 26 |
+
|
| 27 |
+
**Secret 2: EC2_HOST**
|
| 28 |
+
- Name: `EC2_HOST`
|
| 29 |
+
- Value: `44.201.146.74`
|
| 30 |
+
|
| 31 |
+
**Secret 3: EC2_USER**
|
| 32 |
+
- Name: `EC2_USER`
|
| 33 |
+
- Value: `ubuntu`
|
| 34 |
+
|
| 35 |
+
### 3. Verify Secrets
|
| 36 |
+
You should see these 3 secrets listed:
|
| 37 |
+
- EC2_SSH_KEY
|
| 38 |
+
- EC2_HOST
|
| 39 |
+
- EC2_USER
|
| 40 |
+
|
| 41 |
+
## Step 2: Prepare Your EC2 Server
|
| 42 |
+
|
| 43 |
+
SSH into your EC2 instance and run these commands:
|
| 44 |
+
|
| 45 |
+
```bash
|
| 46 |
+
# SSH into your server (replace with your key path)
|
| 47 |
+
ssh -i ~/Downloads/YOUR_KEY.pem ubuntu@44.201.146.74
|
| 48 |
+
|
| 49 |
+
# Once inside, run these commands:
|
| 50 |
+
|
| 51 |
+
# 1. Install git
|
| 52 |
+
sudo apt-get update
|
| 53 |
+
sudo apt-get install -y git
|
| 54 |
+
|
| 55 |
+
# 2. Clone your repository
|
| 56 |
+
cd ~
|
| 57 |
+
git clone https://github.com/KiWA001/kai-api-gateway.git kai-api
|
| 58 |
+
|
| 59 |
+
# 3. Run initial setup
|
| 60 |
+
cd kai-api
|
| 61 |
+
chmod +x aws-setup.sh
|
| 62 |
+
./aws-setup.sh
|
| 63 |
+
|
| 64 |
+
# 4. Start the server for the first time
|
| 65 |
+
./start-production.sh
|
| 66 |
+
|
| 67 |
+
# Or run as daemon:
|
| 68 |
+
# ./run-daemon.sh start
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
## Step 3: Test Auto-Deployment
|
| 72 |
+
|
| 73 |
+
1. Make a small change to any file (e.g., edit README.md)
|
| 74 |
+
2. Commit and push:
|
| 75 |
+
```bash
|
| 76 |
+
git add .
|
| 77 |
+
git commit -m "Test auto-deployment"
|
| 78 |
+
git push origin main
|
| 79 |
+
```
|
| 80 |
+
3. Go to GitHub β Actions tab
|
| 81 |
+
4. Watch the deployment happen automatically!
|
| 82 |
+
|
| 83 |
+
## What Happens During Auto-Deployment?
|
| 84 |
+
|
| 85 |
+
Every time you push to GitHub:
|
| 86 |
+
|
| 87 |
+
1. GitHub Actions triggers automatically
|
| 88 |
+
2. It connects to your EC2 via SSH
|
| 89 |
+
3. Runs `git pull` to get latest code
|
| 90 |
+
4. Installs any new dependencies
|
| 91 |
+
5. Restarts the server
|
| 92 |
+
6. Verifies the deployment
|
| 93 |
+
|
| 94 |
+
## Troubleshooting
|
| 95 |
+
|
| 96 |
+
### Deployment Failed?
|
| 97 |
+
1. Check GitHub Actions logs: GitHub β Actions tab
|
| 98 |
+
2. Common issues:
|
| 99 |
+
- SSH key not set correctly in secrets
|
| 100 |
+
- EC2 instance not running
|
| 101 |
+
- Security group blocking SSH
|
| 102 |
+
|
| 103 |
+
### Server Not Starting?
|
| 104 |
+
SSH into EC2 and check logs:
|
| 105 |
+
```bash
|
| 106 |
+
ssh -i ~/Downloads/YOUR_KEY.pem ubuntu@44.201.146.74
|
| 107 |
+
tail -f /tmp/kai-api.log
|
| 108 |
+
```
|
| 109 |
+
|
| 110 |
+
### Permission Denied?
|
| 111 |
+
Make sure your .pem key has correct permissions:
|
| 112 |
+
```bash
|
| 113 |
+
chmod 600 ~/Downloads/YOUR_KEY.pem
|
| 114 |
+
```
|
| 115 |
+
|
| 116 |
+
## Manual Deployment (If Needed)
|
| 117 |
+
|
| 118 |
+
If auto-deployment fails, you can always deploy manually:
|
| 119 |
+
|
| 120 |
+
```bash
|
| 121 |
+
ssh -i ~/Downloads/YOUR_KEY.pem ubuntu@44.201.146.74
|
| 122 |
+
cd ~/kai-api
|
| 123 |
+
git pull origin main
|
| 124 |
+
source venv/bin/activate
|
| 125 |
+
pip install -r requirements.txt
|
| 126 |
+
pkill -f uvicorn
|
| 127 |
+
nohup ./venv/bin/uvicorn main:app --host 0.0.0.0 --port 8000 --workers 1 > /tmp/kai-api.log 2>&1 &
|
| 128 |
+
```
|
| 129 |
+
|
| 130 |
+
## Important Notes
|
| 131 |
+
|
| 132 |
+
β
**Security:**
|
| 133 |
+
- Never commit your .pem key to GitHub
|
| 134 |
+
- The SSH key in GitHub secrets is encrypted and secure
|
| 135 |
+
- Only GitHub Actions can access these secrets
|
| 136 |
+
|
| 137 |
+
β
**Best Practices:**
|
| 138 |
+
- Always test locally before pushing
|
| 139 |
+
- Check GitHub Actions logs after each push
|
| 140 |
+
- Keep your EC2 security group updated
|
| 141 |
+
|
| 142 |
+
β
**Cost:**
|
| 143 |
+
- EC2 t3.micro is free for 12 months
|
| 144 |
+
- GitHub Actions has 2,000 free minutes/month
|
| 145 |
+
- You won't be charged for deployments
|
| 146 |
+
|
| 147 |
+
## Next Steps
|
| 148 |
+
|
| 149 |
+
1. **Add the 3 GitHub secrets** (most important!)
|
| 150 |
+
2. **Prepare your EC2 server** (run the commands above)
|
| 151 |
+
3. **Test with a small push**
|
| 152 |
+
4. **Start using your auto-deployment!**
|
| 153 |
+
|
| 154 |
+
Once set up, every `git push` will automatically update your live server! π
|