-
-
Notifications
You must be signed in to change notification settings - Fork 0
324 lines (276 loc) · 9.74 KB
/
sync.yaml
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
name: Update Template Files Across Repos
on:
push:
branches: [main]
workflow_dispatch:
env:
IMAGE_NAME: template-files
jobs:
changesets:
name: Changesets
runs-on: ubuntu-latest
outputs:
hasChangesets: ${{ steps.changesets.outputs.hasChangesets }}
permissions:
contents: write
pull-requests: write
steps:
- name: Generate GitHub App token
id: generate_token
uses: tibdex/[email protected]
with:
app_id: ${{ secrets.BOT_APP_ID }}
private_key: ${{ secrets.BOT_PRIVATE_KEY }}
- name: Checkout Repo
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup PNPM
uses: pnpm/action-setup@v3
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
cache: "pnpm"
- name: Install Dependencies
run: pnpm i
- name: Create Release Pull Request
id: changesets
uses: changesets/action@v1
with:
commit: "[ci] release"
title: "[ci] release"
env:
GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }}
prepare-matrix:
name: Prepare Matrix
runs-on: ubuntu-latest
outputs:
repo-matrix: "${{ env.REPO_MATRIX }}"
steps:
- name: Checkout current repository
uses: actions/checkout@v4
- name: Install jq
run: sudo apt-get install -y jq
- name: Read Repositories from JSON
id: set-matrix
run: |
repos="$(jq -c '.repositories[] | {name: .name, files: .files}' repos.json | jq -s .)"
{
echo "REPO_MATRIX<<EOF"
echo "${repos}"
echo EOF
} >> $GITHUB_ENV
- name: Print Matrix
run: echo "${{ env.REPO_MATRIX }}"
sync:
runs-on: ubuntu-latest
needs: [changesets, prepare-matrix]
strategy:
fail-fast: false
matrix:
repo: "${{ fromJson(needs.prepare-matrix.outputs.repo-matrix) }}"
if: >
(
needs.changesets.outputs.hasChangesets == 'false' &&
(
contains(github.event.head_commit.message, 'deploy') ||
contains(github.event.head_commit.message, '[ci] release')
)
) ||
github.event_name == 'workflow_dispatch'
steps:
- name: Generate GitHub App token
id: generate_token
uses: tibdex/github-app-token@v2
with:
app_id: ${{ secrets.BOT_APP_ID }}
private_key: ${{ secrets.BOT_PRIVATE_KEY }}
- name: Checkout current repository
uses: actions/checkout@v4
- name: Setup PNPM
uses: pnpm/action-setup@v3
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
cache: "pnpm"
- name: Install jq, sort-package-json and markdown-replace-section
run: |
sudo apt-get install -y jq
npm install -g sort-package-json
npm install -g markdown-replace-section
- name: Ensure "🤖 bot" label exists
env:
GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }}
run: |
# Check if label exists in target repository, create if not
if ! gh label list -R "${{ matrix.repo.name }}" | grep -q "🤖 bot"; then
gh label create "🤖 bot" \
--description "Automatically generated pull request" \
--color "0075ca" \
-R "${{ matrix.repo.name }}"
fi
- name: Run synchronization script
env:
GH_TOKEN: ${{ steps.generate_token.outputs.token }}
REPO_NAME: ${{ matrix.repo.name }}
FILES: ${{ toJson(matrix.repo.files) }}
run: ./sync_templates.sh
- name: Append summary for repository
if: success()
env:
GH_TOKEN: ${{ steps.generate_token.outputs.token }}
run: |
repo_name="${{ matrix.repo.name }}"
branch_name="update-template-files"
pr_url="https://github.com/${repo_name}/pull/$(gh pr list --repo "$repo_name" --base main --head "$branch_name" --json number --jq '.[0].number')"
changed_files="${{ env.CHANGED_FILES }}"
mkdir -p "summary"
mkdir -p "discord"
if [ "$changed_files" -gt 0 ]; then
echo "| $repo_name | [PR Link]($pr_url) | $changed_files |" >> summary/summary_table.txt
if [ "$changed_files" -eq 1 ]; then
echo "- $repo_name -- $changed_files file changed: ([PR Link]($pr_url))" >> discord/discord_list.txt
else
echo "- $repo_name -- $changed_files files changed: ([PR Link]($pr_url))" >> discord/discord_list.txt
fi
else
echo "| $repo_name | No PR | $changed_files |" >> summary/summary_table.txt
echo "- $repo_name -- No files changed" >> discord/discord_list.txt
fi
- name: Replace "/" with "-" in repo name for artifact upload
run: |
repo_name="${{ matrix.repo.name }}"
sanitized_repo_name="${repo_name//\//-}"
echo "Sanitized repo name: $sanitized_repo_name"
echo "SANITIZED_REPO_NAME=$sanitized_repo_name" >> $GITHUB_ENV
- name: Upload summary as artifact
uses: actions/upload-artifact@v4
with:
name: summary-table-${{ env.SANITIZED_REPO_NAME }}
path: summary/summary_table.txt
- name: Upload summary as artifact
uses: actions/upload-artifact@v4
with:
name: discord-list-${{ env.SANITIZED_REPO_NAME }}
path: discord/discord_list.txt
summarize:
name: Synchronization Report
runs-on: ubuntu-latest
needs: sync
steps:
- uses: actions/checkout@v4
- name: Download summary artifacts
uses: actions/download-artifact@v4
- name: Combine summaries and generate final markdown table
run: |
# Create or append to the final summary file
echo "### Summary of Changes" > final_summary.md
echo "" >> final_summary.md
echo "| Repository | PR Link | Files Changed |" >> final_summary.md
echo "|------------|---------|---------------|" >> final_summary.md
# Loop over the downloaded summary artifacts and append their content
for artifact in $(find . -name "summary_table.txt"); do
cat "$artifact" >> final_summary.md
done
- name: Upload final summary as artifact
uses: actions/upload-artifact@v4
with:
name: final-summary
path: final_summary.md
- name: Combine discord and generate final markdown list
run: |
# Create or append to the final summary file
echo "### Summary of Changes" > discord_summary.md
echo "" >> discord_summary.md
# Loop over the downloaded summary artifacts and append their content
for artifact in $(find . -name "discord_list.txt"); do
cat "$artifact" >> discord_summary.md
done
- name: Upload final summary as artifact
uses: actions/upload-artifact@v4
with:
name: discord-summary
path: discord_summary.md
- name: Display summary in the GitHub Actions summary
run: |
if [ -f final_summary.md ]; then
cat final_summary.md >> $GITHUB_STEP_SUMMARY
else
echo "No data collected" >> $GITHUB_STEP_SUMMARY
fi
image-tag:
name: Image Tag
runs-on: ubuntu-latest
outputs:
IMAGE_TAG: ${{ env.IMAGE_TAG }}
steps:
- name: Check out the repo
uses: actions/checkout@v4
- name: Read version from package.json
id: get_version
run: |
VERSION=$(jq -r '.version' package.json)
echo "IMAGE_TAG=$VERSION" >> $GITHUB_ENV
release:
name: Release
needs: [sync, image-tag, summarize]
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Check out the repo
uses: actions/checkout@v4
- id: extract-changelog
uses: sean0x42/[email protected]
with:
file: CHANGELOG.md
pattern: ${{ needs.image-tag.outputs.IMAGE_TAG }}
- name: Download summary artifacts
uses: actions/download-artifact@v4
with:
name: final-summary
- name: Download summary artifacts
uses: actions/download-artifact@v4
with:
name: discord-summary
- name: Save summary into Github environment variable
run: |
{
echo "SYNC_SUMMARY<<EOF"
cat final_summary.md
echo "EOF"
} >> $GITHUB_ENV
{
echo "DISCORD_SUMMARY<<EOF"
cat discord_summary.md
echo "EOF"
} >> $GITHUB_ENV
- uses: ncipollo/release-action@v1
id: create_release
with:
tag: ${{ env.IMAGE_NAME }}@${{ needs.image-tag.outputs.IMAGE_TAG }}
makeLatest: true
body: |
${{ steps.extract-changelog.outputs.markdown }}
${{ env.SYNC_SUMMARY }}
skipIfReleaseExists: true
- name: Check if release was created
id: check_release
run: |
if [ -z "${{ steps.create_release.outputs.html_url }}" ]; then
echo "RELEASE_SKIPPED=true" >> $GITHUB_ENV
else
echo "RELEASE_SKIPPED=false" >> $GITHUB_ENV
fi
- name: Discord notification
if: env.RELEASE_SKIPPED == 'false'
env:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_URL }}
uses: Ilshidur/[email protected]
with:
args: |
# ${{ env.IMAGE_NAME }}@${{ needs.image-tag.outputs.IMAGE_TAG }}
${{ steps.extract-changelog.outputs.markdown }}
${{ env.DISCORD_SUMMARY }}