-
Notifications
You must be signed in to change notification settings - Fork 13
237 lines (214 loc) · 8.27 KB
/
integration.yml
File metadata and controls
237 lines (214 loc) · 8.27 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
name: 🚧 Integration
permissions:
contents: read
on:
pull_request:
push:
branches:
- 'master'
concurrency:
group: deploy-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
format:
name: 💅 Check format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main
- uses: pnpm/action-setup@v5.0.0
name: Install pnpm
- uses: actions/setup-node@main
with:
cache: pnpm
- run: pnpm install --frozen-lockfile --prefer-offline
- run: pnpm fmt:check
- run: pnpm lint
nextjs:
name: ⚛️ NextJS application
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main
- uses: pnpm/action-setup@v5.0.0
name: Install pnpm
- uses: actions/setup-node@main
with:
cache: pnpm
- run: pnpm install --frozen-lockfile --prefer-offline
- run: pnpm build
docker:
name: 🐳 Docker build & push
needs: [format, nextjs]
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
scw_image: ${{ steps.meta.outputs.scw_image }}
steps:
- uses: actions/checkout@main
- uses: docker/setup-buildx-action@v4
- name: Login to GHCR
if: github.event_name == 'push'
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Login to Scaleway Container Registry
uses: docker/login-action@v4
with:
registry: rg.fr-par.scw.cloud
username: nologin
password: ${{ secrets.SCW_SECRET_KEY }}
- name: Compute image tags
id: meta
run: |
SCW_REGISTRY="${{ vars.SCW_REGISTRY_ENDPOINT }}"
GHCR_IMAGE="ghcr.io/lyonjs/lyonjs.github.com"
SCW_IMAGE="${SCW_REGISTRY}/lyonjs-website"
if [ "${{ github.event_name }}" = "push" ]; then
ALL_TAGS="${GHCR_IMAGE}:latest,${GHCR_IMAGE}:${{ github.sha }},${SCW_IMAGE}:latest,${SCW_IMAGE}:${{ github.sha }}"
echo "scw_image=${SCW_IMAGE}:${{ github.sha }}" >> "$GITHUB_OUTPUT"
else
PR_NUMBER="${{ github.event.pull_request.number }}"
ALL_TAGS="${SCW_IMAGE}:pr-${PR_NUMBER}"
echo "scw_image=${SCW_IMAGE}:pr-${PR_NUMBER}" >> "$GITHUB_OUTPUT"
fi
echo "tags=${ALL_TAGS}" >> "$GITHUB_OUTPUT"
- uses: docker/build-push-action@v7
with:
context: .
push: true
load: true
tags: ${{ steps.meta.outputs.tags }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Docker image summary
run: |
IMAGE=$(echo "${{ steps.meta.outputs.tags }}" | cut -d',' -f1)
SIZE_BYTES=$(docker image inspect "$IMAGE" --format='{{.Size}}')
SIZE_MB=$(awk "BEGIN {printf \"%.1f\", ${SIZE_BYTES}/1024/1024}")
LAYERS=$(docker image inspect "$IMAGE" --format='{{len .RootFS.Layers}}')
CREATED=$(docker image inspect "$IMAGE" --format='{{.Created}}')
echo "## 🐳 Docker image" >> "$GITHUB_STEP_SUMMARY"
echo "" >> "$GITHUB_STEP_SUMMARY"
echo "| | |" >> "$GITHUB_STEP_SUMMARY"
echo "|---|---|" >> "$GITHUB_STEP_SUMMARY"
echo "| **Image** | \`${IMAGE}\` |" >> "$GITHUB_STEP_SUMMARY"
echo "| **Size** | ${SIZE_MB} MB |" >> "$GITHUB_STEP_SUMMARY"
echo "| **Layers** | ${LAYERS} |" >> "$GITHUB_STEP_SUMMARY"
echo "| **Created** | ${CREATED} |" >> "$GITHUB_STEP_SUMMARY"
deploy-production:
name: 🚀 Deploy production
if: github.event_name == 'push'
needs: docker
runs-on: ubuntu-latest
permissions:
deployments: write
environment:
name: production
url: ${{ steps.deploy.outputs.url }}
env:
SCW_ACCESS_KEY: ${{ secrets.SCW_ACCESS_KEY }}
SCW_SECRET_KEY: ${{ secrets.SCW_SECRET_KEY }}
SCW_DEFAULT_PROJECT_ID: ${{ secrets.SCW_DEFAULT_PROJECT_ID }}
SCW_DEFAULT_ORGANIZATION_ID: ${{ secrets.SCW_DEFAULT_ORGANIZATION_ID }}
steps:
- name: Install Scaleway CLI
run: curl -s https://raw.githubusercontent.com/scaleway/scaleway-cli/master/scripts/get.sh | sudo sh
- name: Deploy container
id: deploy
run: |
scw container container update \
"${{ vars.SCW_PROD_CONTAINER_ID }}" \
registry-image="${{ needs.docker.outputs.scw_image }}" \
redeploy=true \
region=fr-par
# Poll until ready (timeout 3 min)
for i in $(seq 1 36); do
STATUS=$(scw container container get "${{ vars.SCW_PROD_CONTAINER_ID }}" region=fr-par -o json | jq -r '.status')
if [ "$STATUS" = "ready" ]; then
URL=$(scw container container get "${{ vars.SCW_PROD_CONTAINER_ID }}" region=fr-par -o json | jq -r '.domain_name')
echo "url=https://${URL}" >> "$GITHUB_OUTPUT"
echo "Container is ready at https://${URL}"
exit 0
fi
echo "Status: ${STATUS} - waiting..."
sleep 5
done
echo "::error::Deployment timed out"
exit 1
deploy-preview:
name: 🔍 Deploy preview
if: github.event_name == 'pull_request'
needs: docker
runs-on: ubuntu-latest
permissions:
deployments: write
environment:
name: preview-pr-${{ github.event.pull_request.number }}
url: ${{ steps.deploy.outputs.url }}
env:
SCW_ACCESS_KEY: ${{ secrets.SCW_ACCESS_KEY }}
SCW_SECRET_KEY: ${{ secrets.SCW_SECRET_KEY }}
SCW_DEFAULT_PROJECT_ID: ${{ secrets.SCW_DEFAULT_PROJECT_ID }}
SCW_DEFAULT_ORGANIZATION_ID: ${{ secrets.SCW_DEFAULT_ORGANIZATION_ID }}
steps:
- name: Install Scaleway CLI
run: curl -s https://raw.githubusercontent.com/scaleway/scaleway-cli/master/scripts/get.sh | sudo sh
- name: Deploy preview container
id: deploy
run: |
PR_NUMBER="${{ github.event.pull_request.number }}"
CONTAINER_NAME="preview-pr-${PR_NUMBER}"
NAMESPACE_ID="${{ vars.SCW_PREVIEW_NAMESPACE_ID }}"
IMAGE="${{ needs.docker.outputs.scw_image }}"
# Check if the container already exists (exact name match)
EXISTING=$(scw container container list \
namespace-id="${NAMESPACE_ID}" \
region=fr-par \
-o json | jq -r --arg name "$CONTAINER_NAME" '.[] | select(.name == $name) | .id')
if [ -n "$EXISTING" ]; then
echo "Updating existing container ${EXISTING}"
scw container container update \
"${EXISTING}" \
registry-image="${IMAGE}" \
redeploy=true \
region=fr-par
CONTAINER_ID="${EXISTING}"
else
echo "Creating new preview container"
RESULT=$(scw container container create \
namespace-id="${NAMESPACE_ID}" \
name="${CONTAINER_NAME}" \
registry-image="${IMAGE}" \
port=3000 \
min-scale=0 \
max-scale=1 \
memory-limit=256 \
cpu-limit=250 \
privacy=public \
http-option=redirected \
environment-variables.DEPLOY_ENV=preview \
environment-variables.NODE_ENV=production \
environment-variables.HOSTNAME=0.0.0.0 \
region=fr-par \
-o json)
CONTAINER_ID=$(echo "$RESULT" | jq -r '.id')
# Deploy the newly created container
scw container container deploy "${CONTAINER_ID}" region=fr-par
fi
# Poll until ready (timeout 3 min)
for i in $(seq 1 36); do
STATUS=$(scw container container get "${CONTAINER_ID}" region=fr-par -o json | jq -r '.status')
if [ "$STATUS" = "ready" ]; then
URL=$(scw container container get "${CONTAINER_ID}" region=fr-par -o json | jq -r '.domain_name')
echo "url=https://${URL}" >> "$GITHUB_OUTPUT"
echo "Container is ready at https://${URL}"
exit 0
fi
echo "Status: ${STATUS} - waiting..."
sleep 5
done
echo "::error::Preview deployment timed out"
exit 1