Raw
1 name: Sync API Reference Specs 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/api-reference/specs/**"
15
16 jobs:
17 sync:
18 name: Sync changed spec files to R2
19 runs-on: ubuntu-latest
20 defaults:
21 run:
22 working-directory: www/apps/api-reference
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 spec files
45 id: changed
46 uses: tj-actions/changed-files@9426d40962ed5378910ee2e21d5f8c6fcbf2dd96
47 with:
48 files: "www/apps/api-reference/specs/**"
49 separator: " "
50 sha: ${{ inputs.commit_sha || '' }}
51 base_sha: ${{ steps.dispatch_base.outputs.base_sha || '' }}
52
53 - name: Upload added/modified specs
54 if: steps.changed.outputs.any_changed == 'true'
55 env:
56 CHANGED_FILES: ${{ steps.changed.outputs.all_changed_files }}
57 CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
58 CLOUDFLARE_R2_ACCESS_KEY_ID: ${{ secrets.CLOUDFLARE_R2_ACCESS_KEY_ID }}
59 CLOUDFLARE_R2_SECRET_ACCESS_KEY: ${{ secrets.CLOUDFLARE_R2_SECRET_ACCESS_KEY }}
60 R2_BUCKET_NAME: ${{ secrets.R2_API_REFERENCE_BUCKET_NAME }}
61 run: |
62 STRIPPED=$(echo "$CHANGED_FILES" | tr ' ' '\n' | sed 's|^www/apps/api-reference/||' | tr '\n' ' ')
63 node ./scripts/upload-specs-to-r2.mjs --upload $STRIPPED
64
65 - name: Remove deleted specs
66 if: steps.changed.outputs.any_deleted == 'true'
67 env:
68 DELETED_FILES: ${{ steps.changed.outputs.deleted_files }}
69 CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
70 CLOUDFLARE_R2_ACCESS_KEY_ID: ${{ secrets.CLOUDFLARE_R2_ACCESS_KEY_ID }}
71 CLOUDFLARE_R2_SECRET_ACCESS_KEY: ${{ secrets.CLOUDFLARE_R2_SECRET_ACCESS_KEY }}
72 R2_BUCKET_NAME: ${{ secrets.R2_API_REFERENCE_BUCKET_NAME }}
73 run: |
74 STRIPPED=$(echo "$DELETED_FILES" | tr ' ' '\n' | sed 's|^www/apps/api-reference/||' | tr '\n' ' ')
75 node ./scripts/upload-specs-to-r2.mjs --remove $STRIPPED
76
77 # The `secrets` context isn't available in a step's `if`, so the check is
78 # done here and exposed as an output.
79 - name: Check whether the resync webhook is configured
80 id: webhook
81 if: steps.changed.outputs.any_changed == 'true' || steps.changed.outputs.any_deleted == 'true'
82 env:
83 MEDUSA_CLOUD_API_URL: ${{ secrets.MEDUSA_CLOUD_API_URL }}
84 MEDUSA_CLOUD_API_KEY: ${{ secrets.MEDUSA_CLOUD_API_KEY }}
85 run: |
86 if [ -n "$MEDUSA_CLOUD_API_URL" ] && [ -n "$MEDUSA_CLOUD_API_KEY" ]; then
87 echo "configured=true" >> "$GITHUB_OUTPUT"
88 else
89 echo "configured=false" >> "$GITHUB_OUTPUT"
90 echo "::notice::Skipping the resync trigger: MEDUSA_CLOUD_API_URL and/or MEDUSA_CLOUD_API_KEY aren't set."
91 fi
92
93 - name: Trigger specs resync
94 if: steps.webhook.outputs.configured == 'true'
95 env:
96 MEDUSA_CLOUD_API_URL: ${{ secrets.MEDUSA_CLOUD_API_URL }}
97 MEDUSA_CLOUD_API_KEY: ${{ secrets.MEDUSA_CLOUD_API_KEY }}
98 COMMIT_SHA: ${{ inputs.commit_sha || github.sha }}
99 run: |
100 # the secret may hold a bare host, so the scheme is added when missing
101 BASE_URL="$MEDUSA_CLOUD_API_URL"
102 case "$BASE_URL" in
103 http://*|https://*) ;;
104 *) BASE_URL="https://$BASE_URL" ;;
105 esac
106 BASE_URL="${BASE_URL%/}"
107
108 curl --fail-with-body --silent --show-error --location \
109 --request POST "$BASE_URL/hooks/oas-specs" \
110 --header "Authorization: Basic $MEDUSA_CLOUD_API_KEY" \
111 --header "Content-Type: application/json" \
112 --data "{\"commit_sha\": \"$COMMIT_SHA\"}"