-
Notifications
You must be signed in to change notification settings - Fork 0
384 lines (336 loc) · 14.4 KB
/
nib-push.yml
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
name: Push to BlockJoy
on:
pull_request:
types: [closed]
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.head.ref || github.head_ref || github.ref }}
cancel-in-progress: false
jobs:
detect-changes:
runs-on: dev
if: github.event.pull_request.merged == true
outputs:
version_changes: ${{ steps.check-versions.outputs.version_changes }}
content_changes: ${{ steps.check-content.outputs.content_changes }}
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
with:
fetch-depth: 0
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@d6e91a2266cdb9d62096cebf1e8546899c6aa18f # v45
with:
base_sha: ${{ github.event.pull_request.base.sha }}
fetch_depth: 0
files: |
protocols/**/babel.yaml
- name: Check content changes
id: check-content
run: |
declare -a CONTENT_CHANGES=()
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
if [[ "$file" == *"babel.yaml" ]]; then
echo "Processing $file for content changes"
DIR=$(dirname "$file")
PROTOCOL=$(basename "$DIR")
# Check if file content has changed
if ! git diff --quiet ${{ github.event.pull_request.base.sha }} HEAD -- "$file"; then
echo "Content changed in $file"
CONTENT_CHANGES+=("{\"image_path\":\"$DIR\",\"protocol\":\"$PROTOCOL\"}")
fi
fi
done
# Create content changes array using jq for proper JSON handling
if [ ${#CONTENT_CHANGES[@]} -gt 0 ]; then
echo "content_changes=$(printf '%s\n' "${CONTENT_CHANGES[@]}" | jq -sc '.')" >> $GITHUB_OUTPUT
else
echo "content_changes=[]" >> $GITHUB_OUTPUT
fi
- name: Check version changes
id: check-versions
run: |
# Get changed files from previous step
CHANGED_FILES="${{ steps.changed-files.outputs.all_changed_files }}"
# Initialize array for babel files with version changes
declare -a CHANGED_BABEL=()
# Process each file
for file in $CHANGED_FILES; do
if [[ "$file" == *"babel.yaml" ]]; then
echo "Processing $file for version changes"
DIR=$(dirname "$file")
PROTOCOL=$(echo "$DIR" | cut -d'/' -f2)
IMAGE_NAME=$(basename "$DIR")
# Compare version key between current and base
CURRENT_VERSION=$(yq e '.version' "$file")
PREV_VERSION=$(git show ${{ github.event.pull_request.base.sha }}:"$file" | yq e '.version' -)
if [ "$CURRENT_VERSION" != "$PREV_VERSION" ]; then
echo "Version changed in $file: $PREV_VERSION -> $CURRENT_VERSION"
CHANGED_BABEL+=("{\"protocol\":\"$PROTOCOL\",\"babel_yaml\":\"$file\",\"version\":\"$CURRENT_VERSION\"}")
fi
fi
done
# Create array using jq for proper JSON handling
if [ ${#CHANGED_BABEL[@]} -gt 0 ]; then
echo "DEBUG: Raw babel changes:"
printf "%s\n" "${CHANGED_BABEL[@]}"
# Join array elements with commas and wrap in brackets
BABEL_JSON="[$(IFS=,; echo "${CHANGED_BABEL[*]}")]"
BABEL_ARRAY=$(echo "$BABEL_JSON" | jq -c '.')
echo "DEBUG: Babel files with version changes:"
echo "$BABEL_ARRAY" | jq '.'
echo "version_changes=$BABEL_ARRAY" >> $GITHUB_OUTPUT
else
echo "DEBUG: No version changes detected in babel files"
echo "version_changes=[]" >> $GITHUB_OUTPUT
fi
check-protocols:
needs: [detect-changes]
if: fromJson(needs.detect-changes.outputs.content_changes)[0]
runs-on: dev
outputs:
protocols_changed: ${{ steps.check-protocol.outputs.protocols_changed }}
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
- name: Check protocol keys in protocols.yaml
id: check-protocol
run: |
# Get all changed protocols from detect-changes
CHANGED_PROTOCOLS='${{ needs.detect-changes.outputs.content_changes }}'
# Check each protocol key
echo "$CHANGED_PROTOCOLS" | jq -r '.[] | .image_path' | while read -r IMAGE_PATH; do
echo "Checking protocol in $IMAGE_PATH"
# Get protocol key from babel.yaml
PROTOCOL_KEY=$(yq e '.protocol_key' "$IMAGE_PATH/babel.yaml")
echo "Checking for protocol key: $PROTOCOL_KEY"
# Check if key exists in protocols.yaml
if ! yq e '.[] | select(.key == "'$PROTOCOL_KEY'")' protocols/protocols.yaml > /dev/null 2>&1; then
echo " Protocol key '$PROTOCOL_KEY' not found in protocols/protocols.yaml"
exit 1
fi
done
# Check if protocols.yaml has changed
if git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | grep -q "^protocols/protocols.yaml$"; then
echo "protocols/protocols.yaml has changed, will push protocol updates"
echo "protocols_changed=true" >> $GITHUB_OUTPUT
else
echo "protocols/protocols.yaml unchanged"
echo "protocols_changed=false" >> $GITHUB_OUTPUT
fi
push-protocols-staging:
environment: Staging
needs: [detect-changes, check-protocols]
if: needs.check-protocols.outputs.protocols_changed == 'true'
runs-on: dev
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
- name: Setup NIB auth
run: |
echo '${{ secrets.NIB_AUTH }}' > ~/.nib.json
- name: Push protocols to staging
run: |
echo "Pushing protocol updates to staging"
nib protocol push --path protocols/protocols.yaml
push-protocols-prod:
environment: Prod
needs: [detect-changes, check-protocols]
if: needs.check-protocols.outputs.protocols_changed == 'true'
runs-on: dev
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
- name: Setup NIB auth
run: |
echo '${{ secrets.NIB_AUTH }}' > ~/.nib.json
- name: Push protocols to production
run: |
echo "Pushing protocol updates to production"
nib protocol push --path protocols/protocols.yaml
push-dev:
environment: Dev
needs: [detect-changes]
if: fromJson(needs.detect-changes.outputs.content_changes)[0]
runs-on: dev
permissions:
contents: read
packages: write
pull-requests: write
strategy:
matrix:
include: ${{ fromJson(needs.detect-changes.outputs.content_changes) }}
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
with:
fetch-depth: 0
- name: Setup NIB auth
run: |
echo '${{ secrets.NIB_AUTH }}' > ~/.nib.json
- name: Setup GitHub CLI
run: |
gh auth login --with-token <<< "${{ secrets.GITHUB_TOKEN }}"
- name: Push to dev environment
id: push-dev
run: |
echo "Pushing ${{ matrix.protocol }} to dev environment"
OUTPUT=$(nib image push --path ${{ matrix.image_path }}/babel.yaml)
echo "$OUTPUT"
# Extract image ID from output
IMAGE_ID=$(echo "$OUTPUT" | grep "^Image '" | cut -d"'" -f2)
if [ ! -z "$IMAGE_ID" ]; then
# Split image ID into components
IFS='/' read -r PROTOCOL VARIANT VERSION BUILDNUM <<< "$IMAGE_ID"
echo "protocol=$PROTOCOL" >> $GITHUB_OUTPUT
echo "variant=$VARIANT" >> $GITHUB_OUTPUT
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "buildnum=$BUILDNUM" >> $GITHUB_OUTPUT
fi
- name: Comment on PR (Dev)
if: steps.push-dev.outputs.protocol != ''
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Capitalize first letter of protocol
PROTOCOL=$(echo "${{ steps.push-dev.outputs.protocol }}" | sed 's/\b\(.\)/\u\1/')
COMMENT="${PROTOCOL}: ${{ steps.push-dev.outputs.variant }} version ${{ steps.push-dev.outputs.version }} and build number ${{ steps.push-dev.outputs.buildnum }} has been pushed to the Dev API."
gh pr comment ${{ github.event.pull_request.number }} --body "$COMMENT"
push-staging:
environment: Staging
needs: [detect-changes, push-dev, check-protocols]
if: fromJson(needs.detect-changes.outputs.version_changes)[0]
runs-on: dev
permissions:
contents: write
packages: write
pull-requests: write
strategy:
matrix:
include: ${{ fromJson(needs.detect-changes.outputs.version_changes) }}
steps:
- name: Debug matrix
run: |
echo "DEBUG: Matrix context:"
echo '${{ toJSON(matrix) }}' | jq '.'
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
with:
fetch-depth: 0
- name: Setup NIB auth
run: |
echo '${{ secrets.NIB_AUTH }}' > ~/.nib.json
- name: Setup GitHub CLI
run: |
gh auth login --with-token <<< "${{ secrets.GITHUB_TOKEN }}"
- name: Push to staging environment
id: push-staging
run: |
echo "Pushing ${{ matrix.protocol }} to staging environment"
OUTPUT=$(nib image push --path ${{ matrix.babel_yaml }})
echo "$OUTPUT"
# Extract image ID from output
IMAGE_ID=$(echo "$OUTPUT" | grep "^Image '" | cut -d"'" -f2)
if [ ! -z "$IMAGE_ID" ]; then
# Split image ID into components
IFS='/' read -r PROTOCOL VARIANT VERSION BUILDNUM <<< "$IMAGE_ID"
echo "protocol=$PROTOCOL" >> $GITHUB_OUTPUT
echo "variant=$VARIANT" >> $GITHUB_OUTPUT
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "buildnum=$BUILDNUM" >> $GITHUB_OUTPUT
fi
- name: Create version tag
if: matrix.version_changed == true
run: |
TAG="${{ steps.push-staging.outputs.protocol }}/v${{ steps.push-staging.outputs.version }}"
if ! git rev-parse "$TAG" >/dev/null 2>&1; then
echo "Creating tag $TAG"
git tag $TAG
git push origin $TAG
else
echo "Tag $TAG already exists, skipping creation"
fi
- name: Comment on PR (Staging)
if: steps.push-staging.outputs.protocol != ''
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Capitalize first letter of protocol
PROTOCOL=$(echo "${{ steps.push-staging.outputs.protocol }}" | sed 's/\b\(.\)/\u\1/')
COMMENT="${PROTOCOL}: ${{ steps.push-staging.outputs.variant }} version ${{ steps.push-staging.outputs.version }} and build number ${{ steps.push-staging.outputs.buildnum }} has been pushed to the Staging API."
gh pr comment ${{ github.event.pull_request.number }} --body "$COMMENT"
push-prod:
environment: Prod
needs: [detect-changes, push-staging, check-protocols]
if: fromJson(needs.detect-changes.outputs.version_changes)[0]
runs-on: dev
permissions:
contents: read
packages: write
pull-requests: write
strategy:
matrix:
include: ${{ fromJson(needs.detect-changes.outputs.version_changes) }}
steps:
- name: Debug matrix
run: |
echo "DEBUG: Matrix context:"
echo '${{ toJSON(matrix) }}' | jq '.'
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
with:
fetch-depth: 0
- name: Setup NIB auth
run: |
echo '${{ secrets.NIB_AUTH }}' > ~/.nib.json
- name: Setup GitHub CLI
run: |
gh auth login --with-token <<< "${{ secrets.GITHUB_TOKEN }}"
- name: Push to production environment
id: push-prod
run: |
echo "Pushing ${{ matrix.protocol }} to production environment"
OUTPUT=$(nib image push --path ${{ matrix.babel_yaml }})
echo "$OUTPUT"
# Extract image ID from output
IMAGE_ID=$(echo "$OUTPUT" | grep "^Image '" | cut -d"'" -f2)
if [ ! -z "$IMAGE_ID" ]; then
# Split image ID into components
IFS='/' read -r PROTOCOL VARIANT VERSION BUILDNUM <<< "$IMAGE_ID"
echo "protocol=$PROTOCOL" >> $GITHUB_OUTPUT
echo "variant=$VARIANT" >> $GITHUB_OUTPUT
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "buildnum=$BUILDNUM" >> $GITHUB_OUTPUT
fi
- name: Comment on PR (Prod)
if: steps.push-prod.outputs.protocol != ''
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Capitalize first letter of protocol
PROTOCOL=$(echo "${{ steps.push-prod.outputs.protocol }}" | sed 's/\b\(.\)/\u\1/')
COMMENT="${PROTOCOL}: ${{ steps.push-prod.outputs.variant }} version ${{ steps.push-prod.outputs.version }} and build number ${{ steps.push-prod.outputs.buildnum }} has been pushed to the Production API."
gh pr comment ${{ github.event.pull_request.number }} --body "$COMMENT"
create-release:
needs: [detect-changes, push-prod]
runs-on: dev
if: success()
permissions:
contents: write
steps:
- name: Create Release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Parse version_changes JSON array
VERSION_CHANGES='${{ needs.detect-changes.outputs.version_changes }}'
echo "Creating releases for changes: $VERSION_CHANGES"
# Process each protocol version
echo "$VERSION_CHANGES" | jq -c '.[]' | while read -r change; do
PROTOCOL=$(echo $change | jq -r '.protocol')
VERSION=$(echo $change | jq -r '.version')
TAG="$PROTOCOL/v$VERSION"
echo "Creating release for $TAG"
gh release create "$TAG" \
--title "$PROTOCOL version $VERSION" \
--notes "Release of $PROTOCOL version $VERSION" \
--target ${{ github.event.pull_request.merge_commit_sha }}