Repository navigation
226 lines (188 loc) · 7.05 KB
/
Copy pathbuild.yml
File metadata and controls
226 lines (188 loc) · 7.05 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
name: CI
on:
push:
branches: [main]
tags: ['v*'] # e.g. v1.0.0, v2.1.3 — triggers the publish job
pull_request:
branches: [main]
jobs:
lint-and-test:
name: Lint & Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- name: Install dependencies
run: npm install
- name: Generate Prisma client
run: npm run prisma:generate --workspace=apps/backend
- name: Lint backend
run: npm run lint --workspace=apps/backend
- name: Lint web
run: npm run lint --workspace=apps/web
- name: Test backend
run: npm test --workspace=apps/backend
env:
# Fallback is a dummy 64-char hex string used only for CI test runs.
# Set secrets.ENV_SYNC_SECRET in GitHub Settings → Secrets for production deploys.
ENV_SYNC_SECRET: ${{ secrets.ENV_SYNC_SECRET || 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' }}
HEALTH_SECRET: test-health-secret
build:
name: Build
runs-on: ubuntu-latest
needs: lint-and-test
steps:
- uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- name: Install dependencies
run: npm install
- name: Generate Prisma client
run: npm run prisma:generate --workspace=apps/backend
- name: Build backend
run: npm run build --workspace=apps/backend
- name: Build web
run: npm run build --workspace=apps/web
- name: Docker build (backend)
uses: docker/build-push-action@v5
with:
context: .
file: apps/backend/Dockerfile
push: false
- name: Docker build (web)
uses: docker/build-push-action@v5
with:
context: .
file: apps/web/Dockerfile
push: false
publish:
name: Publish Docker image
runs-on: ubuntu-latest
needs: build
# Push on merges to main (SHA tag) or when a version tag is pushed (semver tags)
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v'))
permissions:
contents: read
packages: write # Required to push to GitHub Container Registry
steps:
- uses: actions/checkout@v4
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
# GITHUB_TOKEN is automatically provided — no manual secret needed
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set lowercase owner
id: owner
run: echo "value=$(echo '${{ github.repository_owner }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT
- name: Extract backend image metadata (tags + labels)
id: meta-backend
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ steps.owner.outputs.value }}/envsync-backend
tags: |
# On every merge to main: tag with short git SHA for exact traceability
type=sha,format=short
# On release tags (v1.0.0): produce clean semver tags
type=semver,pattern={{version}} # → 1.0.0
type=semver,pattern={{major}}.{{minor}} # → 1.0
# 'latest' always points to the most recent main/release build
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push backend image
uses: docker/build-push-action@v5
with:
context: .
file: apps/backend/Dockerfile
push: true
tags: ${{ steps.meta-backend.outputs.tags }}
labels: ${{ steps.meta-backend.outputs.labels }}
- name: Extract web image metadata (tags + labels)
id: meta-web
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ steps.owner.outputs.value }}/envsync-web
tags: |
type=sha,format=short
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push web image
uses: docker/build-push-action@v5
with:
context: .
file: apps/web/Dockerfile
push: true
tags: ${{ steps.meta-web.outputs.tags }}
labels: ${{ steps.meta-web.outputs.labels }}
migrate-staging:
name: Run DB migrations (Staging)
runs-on: ubuntu-latest
needs: publish
# Runs on every merge to main — migrates Railway's external PostgreSQL.
# Railway auto-deploy handles the actual staging deploy via GitHub integration.
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- name: Install dependencies
run: npm install
- name: Run Prisma migrations
run: |
if [ -z "$DATABASE_URL" ]; then
echo "Skipping migrations: DATABASE_URL is empty (populated from the STAGING_DATABASE_URL GitHub Actions secret, which is not yet configured)"
exit 0
fi
npx prisma migrate deploy
working-directory: apps/backend
env:
DATABASE_URL: ${{ secrets.STAGING_DATABASE_URL }}
deploy-production:
name: Deploy to GKE (Production)
runs-on: ubuntu-latest
needs: publish
# Only deploys to production on version tag pushes — migrations handled by Helm pre-install hook
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
environment: production
steps:
- uses: actions/checkout@v4
- name: Authenticate to GCP
uses: google-github-actions/auth@v2
with:
credentials_json: ${{ secrets.GCP_SA_KEY }}
- name: Get GKE credentials
uses: google-github-actions/get-gke-credentials@v2
with:
cluster_name: ${{ vars.GKE_CLUSTER }}
location: ${{ vars.GKE_ZONE }}
project_id: ${{ vars.GCP_PROJECT }}
- name: Set up Helm
uses: azure/setup-helm@v4
- name: Fetch Helm dependencies
run: |
helm repo add bitnami https://charts.bitnami.com/bitnami
helm dependency build apps/backend/infra/helm
- name: Deploy via Helm
run: |
IMAGE_TAG="sha-$(echo "${{ github.sha }}" | cut -c1-7)"
OWNER=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')
helm upgrade --install envsync apps/backend/infra/helm \
--namespace production \
--create-namespace \
--set image.repository=ghcr.io/${OWNER}/envsync-backend \
--set image.tag=${IMAGE_TAG} \
--set image.pullPolicy=IfNotPresent \
--set postgresql.auth.database=envsync-production-db \
--wait \
--timeout 5m