@@ -3,6 +3,9 @@ name: CI CD - Whim
33on :
44 push :
55 branches : [main]
6+ pull_request :
7+ branches : [main]
8+ types : [opened, synchronize, reopened, closed]
69
710jobs :
811 build :
@@ -22,10 +25,141 @@ jobs:
2225 - name : Build
2326 run : bun run build
2427
28+ cleanup-preview :
29+ name : Cleanup Preview
30+ runs-on : ubuntu-22.04
31+ if : github.event_name == 'pull_request' && github.event.action == 'closed'
32+
33+ steps :
34+ - name : Setup SSH key
35+ uses : webfactory/ssh-agent@v0.9.1
36+ with :
37+ ssh-private-key : ${{ secrets.VPS_KEY }}
38+
39+ - name : Remove Preview Deployment
40+ run : |
41+ PR_NUMBER=${{ github.event.number }}
42+
43+ ssh -o StrictHostKeyChecking=no ${{ secrets.VPS_USER }}@${{ secrets.VPS_HOST }} << EOF
44+ # Stop and remove the preview container
45+ docker stop whim-app-pr-${PR_NUMBER} 2>/dev/null || true
46+ docker rm whim-app-pr-${PR_NUMBER} 2>/dev/null || true
47+
48+ # Remove the preview directory
49+ rm -rf ~/whim-previews/pr-${PR_NUMBER}
50+
51+ # Remove Caddy config
52+ rm -f ~/caddy-configs/pr-${PR_NUMBER}.conf
53+
54+ # Reload Caddy to remove the subdomain
55+ cd ~/whim
56+ docker compose restart caddy
57+ EOF
58+
59+ - name : Comment Cleanup
60+ uses : actions/github-script@v7
61+ with :
62+ script : |
63+ const prNumber = context.issue.number;
64+
65+ github.rest.issues.createComment({
66+ issue_number: prNumber,
67+ owner: context.repo.owner,
68+ repo: context.repo.repo,
69+ body: `🧹 **Preview deployment cleaned up!**\n\n_The preview environment for this PR has been removed._`
70+ });
71+
72+ preview :
73+ name : Deploy Preview
74+ runs-on : ubuntu-22.04
75+ needs : build
76+ if : github.event_name == 'pull_request' && github.event.action != 'closed'
77+
78+ steps :
79+ - name : Setup SSH key
80+ uses : webfactory/ssh-agent@v0.9.1
81+ with :
82+ ssh-private-key : ${{ secrets.VPS_KEY }}
83+
84+ - name : Deploy Preview
85+ run : |
86+ PR_NUMBER=${{ github.event.number }}
87+ BRANCH_NAME="${{ github.head_ref }}"
88+
89+ ssh -o StrictHostKeyChecking=no ${{ secrets.VPS_USER }}@${{ secrets.VPS_HOST }} << EOF
90+ # Create preview directory
91+ mkdir -p ~/whim-previews/pr-${PR_NUMBER}
92+ cd ~/whim-previews/pr-${PR_NUMBER}
93+
94+ # Clone or update the repository
95+ if [ ! -d ".git" ]; then
96+ git clone https://github.com/${{ github.repository }}.git .
97+ fi
98+
99+ # Checkout the PR branch
100+ git fetch origin
101+ git checkout ${BRANCH_NAME}
102+ git pull origin ${BRANCH_NAME}
103+
104+ # Copy environment file from main app
105+ cp ~/whim/.env .env
106+
107+ # Build and start the preview with custom port
108+ PREVIEW_PORT=\$((3000 + ${PR_NUMBER}))
109+
110+ # Update docker-compose for preview
111+ cat > docker-compose.preview.yml << 'COMPOSE_EOF'
112+ services:
113+ app:
114+ build: .
115+ container_name: whim-app-pr-${PR_NUMBER}
116+ restart: unless-stopped
117+ volumes:
118+ - ./db:/app/db
119+ env_file:
120+ - .env
121+ ports:
122+ - \${PREVIEW_PORT}:3000
123+ networks:
124+ - whimnet-pr-${PR_NUMBER}
125+
126+ networks:
127+ whimnet-pr-${PR_NUMBER}:
128+ driver: bridge
129+ COMPOSE_EOF
130+
131+ # Deploy the preview
132+ PREVIEW_PORT=\${PREVIEW_PORT} docker compose -f docker-compose.preview.yml up --build -d
133+
134+ # Update Caddy configuration for the new subdomain
135+ echo "pr-${PR_NUMBER}.whim.day {
136+ reverse_proxy localhost:\${PREVIEW_PORT}
137+ }" > ~/caddy-configs/pr-${PR_NUMBER}.conf
138+
139+ # Reload Caddy to pick up new config
140+ cd ~/whim
141+ docker compose restart caddy
142+ EOF
143+
144+ - name : Comment Preview URL
145+ uses : actions/github-script@v7
146+ with :
147+ script : |
148+ const prNumber = context.issue.number;
149+ const previewUrl = `https://pr-${prNumber}.whim.day`;
150+
151+ github.rest.issues.createComment({
152+ issue_number: prNumber,
153+ owner: context.repo.owner,
154+ repo: context.repo.repo,
155+ body: `🚀 **Preview deployed!**\n\n📱 Preview URL: ${previewUrl}\n\n_This preview will be automatically updated on new commits to this PR._`
156+ });
157+
25158 deploy :
26159 name : CD - Whim Deploy
27160 runs-on : ubuntu-22.04
28161 needs : build
162+ if : github.event_name == 'push' && github.ref == 'refs/heads/main'
29163
30164 steps :
31165 - name : Setup SSH key
0 commit comments