-
Notifications
You must be signed in to change notification settings - Fork 0
280 lines (259 loc) · 10.1 KB
/
Copy pathdeploy-production.yml
File metadata and controls
280 lines (259 loc) · 10.1 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
name: Deploy Production
on:
workflow_call:
inputs:
image-tag:
description: Docker image tag to deploy (e.g. 1.2.3).
required: false
type: string
default: ""
workflow_dispatch:
inputs:
image-tag:
description: Docker image tag to deploy (e.g. 1.2.3).
required: true
type: string
jobs:
deploy-production:
name: Deploy to Production
runs-on: ubuntu-latest
environment: production
steps:
- name: Install kubectl
uses: azure/setup-kubectl@v4
- name: Deploy
env:
KUBECONFIG_B64: ${{ secrets.KUBECONFIG_B64 }}
GHCR_USERNAME: ${{ secrets.GHCR_USERNAME }}
GHCR_TOKEN: ${{ secrets.GHCR_TOKEN }}
K8S_API_SERVER: ${{ secrets.K8S_API_SERVER }}
TENANT_ID: ${{ secrets.TENANT_ID }}
API_AUDIENCE: ${{ secrets.API_AUDIENCE }}
IMAGE_TAG_INPUT: ${{ inputs.image-tag }}
GIT_REF_NAME: ${{ github.ref_name }}
REPO_NAME: ${{ github.event.repository.name }}
REPO_OWNER: ${{ github.repository_owner }}
REPO_FULL: ${{ github.repository }}
run: |
set -eu
# --- Kubeconfig ---
KUBECONFIG="$(mktemp)"
export KUBECONFIG
printf '%s' "$KUBECONFIG_B64" | base64 -d > "$KUBECONFIG"
if [ -n "${K8S_API_SERVER:-}" ]; then
sed -i "s#https://127.0.0.1:6443#$K8S_API_SERVER#g" "$KUBECONFIG"
fi
# --- Derive naming from repository ---
APP_NAME="${APP_NAME:-${REPO_NAME:-${REPO_FULL##*/}}}"
APP_NAME="$(printf '%s' "$APP_NAME" | tr '[:upper:]' '[:lower:]' | tr '._' '-')"
ORG_GHCR="${ORG_GHCR:-${REPO_OWNER:-}}"
if [ -z "$ORG_GHCR" ]; then
ORG_GHCR="$(printf '%s' "${REPO_FULL:-}" | cut -d/ -f1)"
fi
ORG_DNS="${ORG_DNS:-}"
if [ -z "$ORG_DNS" ]; then
case "$ORG_GHCR" in
*-com) ORG_DNS="${ORG_GHCR%-com}.com" ;;
*-org) ORG_DNS="${ORG_GHCR%-org}.org" ;;
*-net) ORG_DNS="${ORG_GHCR%-net}.net" ;;
*) ORG_DNS="$ORG_GHCR" ;;
esac
fi
ORG_NS="${ORG_NS:-$ORG_GHCR}"
case "$ORG_NS" in
*-com) ORG_NS="${ORG_NS%-com}" ;;
*-org) ORG_NS="${ORG_NS%-org}" ;;
*-net) ORG_NS="${ORG_NS%-net}" ;;
esac
NAMESPACE="${ORG_NS}-prod"
HOST="${APP_NAME}.${ORG_DNS}"
TLS_SECRET="$(printf '%s' "$HOST" | tr '.' '-')-tls"
# Image tag: explicit input, or strip leading 'v' from git tag
if [ -n "${IMAGE_TAG_INPUT:-}" ]; then
IMAGE_TAG="$IMAGE_TAG_INPUT"
else
IMAGE_TAG="${GIT_REF_NAME#v}"
fi
DB_NAME="$(printf '%s' "$APP_NAME" | tr '-' '_')_prod"
render_apply() {
sed \
-e "s#__APP_NAME__#${APP_NAME}#g" \
-e "s#__NAMESPACE__#${NAMESPACE}#g" \
-e "s#__ORG_GHCR__#${ORG_GHCR}#g" \
-e "s#__IMAGE_TAG__#${IMAGE_TAG}#g" \
-e "s#__HOST__#${HOST}#g" \
-e "s#__TLS_SECRET__#${TLS_SECRET}#g" \
-e "s#__TENANT_ID__#${TENANT_ID}#g" \
-e "s#__API_AUDIENCE__#${API_AUDIENCE}#g" \
| kubectl apply -f -
}
# --- Namespace and registry credentials ---
printf '%s\n' \
"apiVersion: v1" \
"kind: Namespace" \
"metadata:" \
" name: __NAMESPACE__" \
| render_apply
kubectl -n "$NAMESPACE" create secret docker-registry ghcr-creds \
--docker-server=ghcr.io \
--docker-username="$GHCR_USERNAME" \
--docker-password="$GHCR_TOKEN" \
--dry-run=client -o yaml | kubectl apply -f -
# --- CloudNativePG: create database in the shared cluster (default ns) ---
printf '%s\n' \
"apiVersion: postgresql.cnpg.io/v1" \
"kind: Database" \
"metadata:" \
" name: __APP_NAME__-prod-db" \
" namespace: default" \
"spec:" \
" name: __APP_NAME___prod" \
" owner: app" \
" cluster:" \
" name: pg" \
| render_apply
# --- Wait for CNPG database to be applied ---
echo "Waiting for database ${DB_NAME} to be applied …"
DB_READY=false
for _i in $(seq 1 30); do
APPLIED="$(kubectl -n default get database/"${APP_NAME}-prod-db" \
-o jsonpath='{.status.applied}' 2>/dev/null || true)"
if [ "$APPLIED" = "true" ]; then
echo "Database ${DB_NAME} is applied."
DB_READY=true
break
fi
echo " attempt ${_i}/30 — applied=${APPLIED:-<unset>}"
sleep 5
done
if [ "$DB_READY" != "true" ]; then
echo "ERROR: timed out waiting for database ${DB_NAME}"
echo "--- Database resource status ---"
kubectl -n default get database/"${APP_NAME}-prod-db" -o yaml || true
exit 1
fi
# --- Build DATABASE_URL from the CNPG cluster's app secret ---
PGUSER="$(kubectl -n default get secret pg-app -o jsonpath='{.data.username}' | base64 -d)"
PGPASS="$(kubectl -n default get secret pg-app -o jsonpath='{.data.password}' | base64 -d)"
PGHOST="pg-rw.default.svc"
PGPORT="5432"
DATABASE_URL="postgresql://${PGUSER}:${PGPASS}@${PGHOST}:${PGPORT}/${DB_NAME}"
kubectl -n "$NAMESPACE" create secret generic "${APP_NAME}-db-credentials" \
--from-literal="DATABASE_URL=${DATABASE_URL}" \
--dry-run=client -o yaml | kubectl apply -f -
# --- ConfigMap ---
printf '%s\n' \
"apiVersion: v1" \
"kind: ConfigMap" \
"metadata:" \
" name: __APP_NAME__-config" \
" namespace: __NAMESPACE__" \
"data:" \
' AUTH_DISABLED: "false"' \
' TENANT_ID: "__TENANT_ID__"' \
' API_AUDIENCE: "__API_AUDIENCE__"' \
' CORS_ALLOWED_ORIGINS: "https://__HOST__"' \
| render_apply
# --- Deployment ---
printf '%s\n' \
"apiVersion: apps/v1" \
"kind: Deployment" \
"metadata:" \
" name: __APP_NAME__" \
" namespace: __NAMESPACE__" \
"spec:" \
" replicas: 1" \
" selector:" \
" matchLabels:" \
" app: __APP_NAME__" \
" template:" \
" metadata:" \
" labels:" \
" app: __APP_NAME__" \
" spec:" \
" imagePullSecrets:" \
" - name: ghcr-creds" \
" containers:" \
" - name: __APP_NAME__" \
" image: ghcr.io/__ORG_GHCR__/__APP_NAME__:__IMAGE_TAG__" \
" imagePullPolicy: Always" \
" ports:" \
" - containerPort: 8000" \
" envFrom:" \
" - configMapRef:" \
" name: __APP_NAME__-config" \
" env:" \
" - name: DATABASE_URL" \
" valueFrom:" \
" secretKeyRef:" \
" name: __APP_NAME__-db-credentials" \
" key: DATABASE_URL" \
" startupProbe:" \
" httpGet:" \
" path: /health" \
" port: 8000" \
" initialDelaySeconds: 5" \
" periodSeconds: 5" \
" failureThreshold: 30" \
" readinessProbe:" \
" httpGet:" \
" path: /health" \
" port: 8000" \
" periodSeconds: 10" \
" livenessProbe:" \
" httpGet:" \
" path: /health" \
" port: 8000" \
" periodSeconds: 30" \
" resources:" \
" requests:" \
" cpu: 100m" \
" memory: 256Mi" \
" limits:" \
" cpu: 500m" \
" memory: 512Mi" \
| render_apply
# --- Service ---
printf '%s\n' \
"apiVersion: v1" \
"kind: Service" \
"metadata:" \
" name: __APP_NAME__" \
" namespace: __NAMESPACE__" \
"spec:" \
" selector:" \
" app: __APP_NAME__" \
" ports:" \
" - name: http" \
" port: 80" \
" targetPort: 8000" \
| render_apply
# --- Ingress ---
printf '%s\n' \
"apiVersion: networking.k8s.io/v1" \
"kind: Ingress" \
"metadata:" \
" name: __APP_NAME__" \
" namespace: __NAMESPACE__" \
" annotations:" \
" traefik.ingress.kubernetes.io/router.entrypoints: web,websecure" \
' traefik.ingress.kubernetes.io/router.tls: "true"' \
"spec:" \
" ingressClassName: traefik" \
" rules:" \
" - host: __HOST__" \
" http:" \
" paths:" \
" - path: /" \
" pathType: Prefix" \
" backend:" \
" service:" \
" name: __APP_NAME__" \
" port:" \
" number: 80" \
" tls:" \
" - hosts:" \
" - __HOST__" \
" secretName: __TLS_SECRET__" \
| render_apply
kubectl -n "$NAMESPACE" rollout status deployment/"$APP_NAME" --timeout=180s