|
1
|
name: Sync Resources References to R2 |
|
2
|
|
|
3
|
on: |
|
4
|
workflow_dispatch: |
|
5
|
inputs: |
|
6
|
commit_sha: |
|
7
|
description: "Commit SHA to retrieve changed files from (defaults to HEAD)" |
|
8
|
required: false |
|
9
|
type: string |
|
10
|
push: |
|
11
|
branches: |
|
12
|
- develop |
|
13
|
paths: |
|
14
|
- "www/apps/resources/references/**" |
|
15
|
|
|
16
|
jobs: |
|
17
|
sync: |
|
18
|
name: Sync changed reference files to R2 |
|
19
|
runs-on: ubuntu-latest |
|
20
|
defaults: |
|
21
|
run: |
|
22
|
working-directory: www/apps/resources |
|
23
|
|
|
24
|
steps: |
|
25
|
- uses: actions/checkout@v4 |
|
26
|
with: |
|
27
|
fetch-depth: ${{ inputs.commit_sha != '' && '0' || '2' }} |
|
28
|
|
|
29
|
- uses: actions/setup-node@v4 |
|
30
|
with: |
|
31
|
node-version: "22" |
|
32
|
cache: "yarn" |
|
33
|
|
|
34
|
- name: Install dependencies |
|
35
|
run: yarn install --immutable |
|
36
|
working-directory: . |
|
37
|
|
|
38
|
- name: Resolve dispatch base SHA |
|
39
|
if: inputs.commit_sha != '' |
|
40
|
id: dispatch_base |
|
41
|
working-directory: . |
|
42
|
run: echo "base_sha=$(git rev-parse ${{ inputs.commit_sha }}^)" >> "$GITHUB_OUTPUT" |
|
43
|
|
|
44
|
- name: Get changed reference files |
|
45
|
id: changed |
|
46
|
uses: tj-actions/changed-files@9426d40962ed5378910ee2e21d5f8c6fcbf2dd96 |
|
47
|
with: |
|
48
|
files: "www/apps/resources/references/**" |
|
49
|
separator: " " |
|
50
|
# Write the file lists to disk (.github/outputs/*.txt) instead of only |
|
51
|
# step outputs. A large batch of changes makes all_changed_files too big |
|
52
|
# to pass through a step env var: GitHub injects env values into the |
|
53
|
# process environment, and once the string exceeds the kernel's argv+env |
|
54
|
# limit, bash itself fails to start with "Argument list too long". |
|
55
|
write_output_files: true |
|
56
|
sha: ${{ inputs.commit_sha || '' }} |
|
57
|
base_sha: ${{ steps.dispatch_base.outputs.base_sha || '' }} |
|
58
|
|
|
59
|
- name: Upload added/modified references |
|
60
|
if: steps.changed.outputs.any_changed == 'true' |
|
61
|
env: |
|
62
|
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
|
63
|
CLOUDFLARE_R2_ACCESS_KEY_ID: ${{ secrets.CLOUDFLARE_R2_ACCESS_KEY_ID }} |
|
64
|
CLOUDFLARE_R2_SECRET_ACCESS_KEY: ${{ secrets.CLOUDFLARE_R2_SECRET_ACCESS_KEY }} |
|
65
|
R2_BUCKET_NAME: ${{ secrets.R2_RESOURCES_BUCKET_NAME }} |
|
66
|
run: | |
|
67
|
FILE="$GITHUB_WORKSPACE/.github/outputs/all_changed_files.txt" |
|
68
|
COUNT=$(tr ' ' '\n' < "$FILE" | grep -c '/page\.json$' || true) |
|
69
|
if [ "$COUNT" -eq 0 ]; then |
|
70
|
echo "No reference files to upload." |
|
71
|
exit 0 |
|
72
|
fi |
|
73
|
# xargs batches the paths so node is exec'd in bounded chunks, never a |
|
74
|
# single argv large enough to overflow the argument list limit. |
|
75
|
tr ' ' '\n' < "$FILE" | grep '/page\.json$' | sed 's|^www/apps/resources/||' \ |
|
76
|
| xargs -r -d '\n' -n 500 node ./scripts/upload-references-to-r2.mjs --upload |
|
77
|
|
|
78
|
- name: Remove deleted references |
|
79
|
if: steps.changed.outputs.any_deleted == 'true' |
|
80
|
env: |
|
81
|
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
|
82
|
CLOUDFLARE_R2_ACCESS_KEY_ID: ${{ secrets.CLOUDFLARE_R2_ACCESS_KEY_ID }} |
|
83
|
CLOUDFLARE_R2_SECRET_ACCESS_KEY: ${{ secrets.CLOUDFLARE_R2_SECRET_ACCESS_KEY }} |
|
84
|
R2_BUCKET_NAME: ${{ secrets.R2_RESOURCES_BUCKET_NAME }} |
|
85
|
run: | |
|
86
|
FILE="$GITHUB_WORKSPACE/.github/outputs/deleted_files.txt" |
|
87
|
COUNT=$(tr ' ' '\n' < "$FILE" | grep -c '/page\.json$' || true) |
|
88
|
if [ "$COUNT" -eq 0 ]; then |
|
89
|
echo "No reference files to remove." |
|
90
|
exit 0 |
|
91
|
fi |
|
92
|
tr ' ' '\n' < "$FILE" | grep '/page\.json$' | sed 's|^www/apps/resources/||' \ |
|
93
|
| xargs -r -d '\n' -n 500 node ./scripts/upload-references-to-r2.mjs --remove |