-
Notifications
You must be signed in to change notification settings - Fork 12
279 lines (254 loc) · 10.5 KB
/
Copy pathpreview-shop-pr.yml
File metadata and controls
279 lines (254 loc) · 10.5 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
# Dynamic mirrord Preview Environment: detects changed services, builds images,
# and starts a preview for each. Posts a single aggregated PR comment + Slack message.
# Uses metalbear-co/mirrord-preview GitHub Action.
# Requires: GCP_WIF_PROVIDER + GCP_SERVICE_ACCOUNT + SLACK_WEBHOOK_URL secrets,
# mirrord operator with Enterprise license.
name: Preview Environment - Shop (Dynamic)
on:
pull_request:
types: [opened, reopened, closed]
concurrency:
group: preview-shop-${{ github.event.pull_request.number }}
cancel-in-progress: true
env:
PREVIEW_KEY: "${{ github.head_ref }}"
jobs:
# ─── Job 1: Detect which services changed ───
detect-changes:
name: Detect changed services
if: github.event.action != 'closed'
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.build-matrix.outputs.matrix }}
has_changes: ${{ steps.build-matrix.outputs.has_changes }}
services_list: ${{ steps.build-matrix.outputs.services_list }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Detect changed services via git diff
id: build-matrix
run: |
CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD)
CHANGES='[]'
for path in $(jq -r '.services[].path' .github/preview-services.json); do
name=$(jq -r --arg p "$path" '.services[] | select(.path == $p) | .name' .github/preview-services.json)
if echo "$CHANGED_FILES" | grep -q "^${path}/"; then
CHANGES=$(echo "$CHANGES" | jq -c --arg n "$name" '. + [$n]')
fi
done
MATRIX=$(jq -c --argjson changes "$CHANGES" \
'{include: [.services[] | select(.name as $n | $changes | index($n))]}' \
.github/preview-services.json)
SERVICES_LIST=$(jq -r --argjson changes "$CHANGES" \
'[.services[] | select(.name as $n | $changes | index($n)) | .name] | join(", ")' \
.github/preview-services.json)
COUNT=$(echo "$CHANGES" | jq 'length')
echo "matrix=$MATRIX" >> "$GITHUB_OUTPUT"
echo "services_list=$SERVICES_LIST" >> "$GITHUB_OUTPUT"
if [ "$COUNT" -gt 0 ]; then
echo "has_changes=true" >> "$GITHUB_OUTPUT"
else
echo "has_changes=false" >> "$GITHUB_OUTPUT"
fi
echo "Changed services ($COUNT): $SERVICES_LIST"
# ─── Job 2: Build image + start preview for each changed service ───
build-and-preview:
name: Build & Preview (${{ matrix.name }})
needs: detect-changes
if: needs.detect-changes.outputs.has_changes == 'true'
runs-on: ubuntu-latest
timeout-minutes: 30
strategy:
matrix: ${{ fromJSON(needs.detect-changes.outputs.matrix) }}
fail-fast: false
permissions:
contents: read
packages: write
id-token: write
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push image
uses: docker/build-push-action@v5
with:
context: ${{ matrix.path }}
file: ${{ matrix.path }}/Dockerfile
platforms: linux/amd64
push: true
tags: ghcr.io/metalbear-co/playground-${{ matrix.name }}:preview-${{ github.head_ref }}-${{ github.sha }}
build-args: ${{ matrix.build_args }}
cache-from: type=gha,scope=${{ matrix.name }}
cache-to: type=gha,scope=${{ matrix.name }},mode=max
- name: Authenticate to GCP via Workload Identity Federation
uses: google-github-actions/auth@v2
with:
workload_identity_provider: ${{ secrets.GCP_WIF_PROVIDER }}
service_account: ${{ secrets.GCP_SERVICE_ACCOUNT }}
- name: Get GKE credentials
uses: google-github-actions/get-gke-credentials@v2
with:
cluster_name: playground-cluster-1
location: us-central1-c
project_id: playground-383912
- name: Start mirrord preview
uses: metalbear-co/mirrord-preview@master
with:
action: start
target: deployment/${{ matrix.deployment }}
namespace: ${{ matrix.namespace }}
image: ghcr.io/metalbear-co/playground-${{ matrix.name }}:preview-${{ github.head_ref }}-${{ github.sha }}
mode: steal
filter: 'baggage:\s*[^\n]*\bmirrord={{ key }}\b'
ttl_mins: '120'
key: ${{ env.PREVIEW_KEY }}
extra_config: ${{ matrix.extra_config }}
- name: Write result artifact
if: success()
run: |
mkdir -p /tmp/preview-results
echo '{"name":"${{ matrix.name }}"}' > /tmp/preview-results/${{ matrix.name }}.json
- name: Upload result artifact
if: success()
uses: actions/upload-artifact@v4
with:
name: preview-result-${{ matrix.name }}
path: /tmp/preview-results/${{ matrix.name }}.json
retention-days: 1
# ─── Job 3: Post aggregated PR comment + Slack notification ───
notify:
name: Post notifications
needs: [detect-changes, build-and-preview]
if: needs.detect-changes.outputs.has_changes == 'true' && !cancelled()
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Download all result artifacts
uses: actions/download-artifact@v4
with:
pattern: preview-result-*
path: /tmp/preview-results
merge-multiple: true
- name: Post PR comment with preview details
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const path = require('path');
const key = `${{ env.PREVIEW_KEY }}`;
const url = 'https://playground.metalbear.dev/shop';
// Read all result files to get list of successfully previewed services
const dir = '/tmp/preview-results';
let services = [];
try {
const files = fs.readdirSync(dir).filter(f => f.endsWith('.json'));
services = files.map(f => JSON.parse(fs.readFileSync(path.join(dir, f), 'utf8')));
} catch (e) {
console.log('No preview results found, some or all previews may have failed.');
}
const serviceList = services.length > 0
? services.map(s => `| \`${s.name}\` | \`deployment/${s.name}\` |`).join('\n')
: '| _(no previews succeeded)_ | |';
const body = [
'## mirrord Preview Environment - Metal Mart',
'',
'Preview environments are running for this PR.',
'',
'| Service | Target |',
'|---|---|',
serviceList,
'',
'| | |',
'|---|---|',
`| **Preview URL** | [${url}](${url}) |`,
`| **Header** | \`baggage: mirrord=${key}\` |`,
'',
'To send traffic to this preview:',
`- Use the [mirrord Browser Extension](https://metalbear.com/mirrord/docs/using-mirrord/browser-extension) and set the header for this URL, or`,
`- \`curl -H "baggage: mirrord=${key}" ${url}\``,
'',
'*Preview is stopped when the PR is merged or closed.*',
].join('\n');
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(c => c.body && c.body.includes('## mirrord Preview Environment - Metal Mart'));
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body,
});
}
- name: Post preview link to Slack
uses: slackapi/slack-github-action@v1.27.0
with:
payload: |
{
"blocks": [
{
"type": "header",
"text": { "type": "plain_text", "text": "mirrord Preview Environment - Metal Mart" }
},
{
"type": "section",
"fields": [
{ "type": "mrkdwn", "text": "*PR:*\n<${{ github.event.pull_request.html_url }}|#${{ github.event.pull_request.number }} ${{ github.event.pull_request.title }}>" },
{ "type": "mrkdwn", "text": "*Services:*\n${{ needs.detect-changes.outputs.services_list }}" },
{ "type": "mrkdwn", "text": "*Preview URL:*\n<https://playground.metalbear.dev/shop|Open Preview>" },
{ "type": "mrkdwn", "text": "*Header:*\n`baggage: mirrord=${{ env.PREVIEW_KEY }}`" }
]
}
]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
# ─── Job 4: Stop all previews on merge/close ───
preview-stop:
name: Stop all previews on merge/close
if: github.event.action == 'closed'
runs-on: ubuntu-latest
timeout-minutes: 5
permissions:
contents: read
id-token: write
steps:
- name: Authenticate to GCP via Workload Identity Federation
uses: google-github-actions/auth@v2
with:
workload_identity_provider: ${{ secrets.GCP_WIF_PROVIDER }}
service_account: ${{ secrets.GCP_SERVICE_ACCOUNT }}
- name: Get GKE credentials
uses: google-github-actions/get-gke-credentials@v2
with:
cluster_name: playground-cluster-1
location: us-central1-c
project_id: playground-383912
- name: Stop mirrord preview
uses: metalbear-co/mirrord-preview@master
with:
action: stop
key: ${{ env.PREVIEW_KEY }}