-
Notifications
You must be signed in to change notification settings - Fork 6
182 lines (148 loc) · 5.44 KB
/
ci-cd.yaml
File metadata and controls
182 lines (148 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
name: CI CD - Whim
on:
push:
branches: [main]
pull_request:
branches: [main]
types: [opened, synchronize, reopened, closed]
permissions:
contents: read
pull-requests: write
issues: write
jobs:
build:
name: CI - Whim Lint & Build
runs-on: ubuntu-22.04
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install Bun
uses: oven-sh/setup-bun@v2
- name: Install dependencies
run: bun install --frozen-lockfile
- name: Build
run: bun run build
cleanup-preview:
name: Cleanup Preview
runs-on: ubuntu-22.04
if: github.event_name == 'pull_request' && github.event.action == 'closed'
steps:
- name: Setup SSH key
uses: webfactory/ssh-agent@v0.9.1
with:
ssh-private-key: ${{ secrets.VPS_KEY }}
- name: Remove Preview Deployment
run: |
PR_NUMBER=${{ github.event.number }}
ssh -o StrictHostKeyChecking=no ${{ secrets.VPS_USER }}@${{ secrets.VPS_HOST }} << EOF
# Stop and remove the preview container
docker stop whim-app-pr-${PR_NUMBER} 2>/dev/null || true
docker rm whim-app-pr-${PR_NUMBER} 2>/dev/null || true
# Remove the preview directory
rm -rf ~/whim-previews/pr-${PR_NUMBER}
# Remove Caddy config
rm -f ~/caddy-configs/pr-${PR_NUMBER}.conf
# Reload Caddy to remove the subdomain
cd ~/whim
docker compose restart caddy
EOF
- name: Comment Cleanup
uses: actions/github-script@v7
with:
script: |
const prNumber = context.issue.number;
github.rest.issues.createComment({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: `🧹 **Preview deployment cleaned up!**\n\n_The preview environment for this PR has been removed._`
});
preview:
name: Deploy Preview
runs-on: ubuntu-22.04
needs: build
if: github.event_name == 'pull_request' && github.event.action != 'closed'
steps:
- name: Setup SSH key
uses: webfactory/ssh-agent@v0.9.1
with:
ssh-private-key: ${{ secrets.VPS_KEY }}
- name: Deploy Preview
run: |
PR_NUMBER=${{ github.event.number }}
BRANCH_NAME="${{ github.head_ref }}"
ssh -o StrictHostKeyChecking=no ${{ secrets.VPS_USER }}@${{ secrets.VPS_HOST }} << EOF
# Create preview directory
mkdir -p ~/whim-previews/pr-${PR_NUMBER}
cd ~/whim-previews/pr-${PR_NUMBER}
# Clone or update the repository
if [ ! -d ".git" ]; then
git clone https://github.com/${{ github.repository }}.git .
fi
# Checkout the PR branch
git fetch origin
git checkout ${BRANCH_NAME}
git pull origin ${BRANCH_NAME}
# Copy environment file from main app
cp ~/whim/.env .env
# Build and start the preview with custom port
PREVIEW_PORT=\$((3000 + ${PR_NUMBER}))
# Update docker-compose for preview
cat > docker-compose.preview.yml << 'COMPOSE_EOF'
services:
app:
build: .
container_name: whim-app-pr-${PR_NUMBER}
restart: unless-stopped
volumes:
- ./db:/app/db
env_file:
- .env
ports:
- \${PREVIEW_PORT}:3000
networks:
- whimnet-pr-${PR_NUMBER}
networks:
whimnet-pr-${PR_NUMBER}:
driver: bridge
COMPOSE_EOF
# Deploy the preview
PREVIEW_PORT=\${PREVIEW_PORT} docker compose -f docker-compose.preview.yml up --build -d
# Update Caddy configuration for the new subdomain
echo "pr-${PR_NUMBER}.whim.day {
reverse_proxy localhost:\${PREVIEW_PORT}
}" > ~/caddy-configs/pr-${PR_NUMBER}.conf
# Reload Caddy to pick up new config
cd ~/whim
docker compose restart caddy
EOF
- name: Comment Preview URL
uses: actions/github-script@v7
with:
script: |
const prNumber = context.issue.number;
const previewUrl = `https://pr-${prNumber}.whim.day`;
github.rest.issues.createComment({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: `🚀 **Preview deployed!**\n\n📱 Preview URL: ${previewUrl}\n\n_This preview will be automatically updated on new commits to this PR._`
});
deploy:
name: CD - Whim Deploy
runs-on: ubuntu-22.04
needs: build
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
- name: Setup SSH key
uses: webfactory/ssh-agent@v0.9.1
with:
ssh-private-key: ${{ secrets.VPS_KEY }}
- name: SSH and Deploy
run: |
ssh -o StrictHostKeyChecking=no ${{ secrets.VPS_USER }}@${{ secrets.VPS_HOST }} << 'EOF'
cd whim
git pull origin main
docker compose up --build -d
docker compose restart caddy
EOF