1616 workflow_dispatch :
1717 workflow_call :
1818 inputs :
19- environment :
20- required : true
21- type : string
22- description : ' The environment to deploy to (staging- preview or production-preview) '
19+ preview :
20+ required : false
21+ type : boolean
22+ description : ' Whether this is a preview deployment '
2323 head_sha :
2424 required : false
2525 type : string
2828 required : false
2929 type : string
3030 description : ' The branch name to deploy (defaults to github.ref_name)'
31+ branch_alias :
32+ required : false
33+ type : string
34+ description : ' The persistent preview alias for the branch'
3135
3236concurrency :
33- group : ${{ github.workflow }}-${{ github.ref }}-${{ inputs.environment || 'push' }}
37+ group : ${{ github.workflow }}-${{ github.ref }}-${{ inputs.preview && 'preview' || 'push' }}
3438 cancel-in-progress : ${{ github.ref != 'refs/heads/main' && github.ref != 'refs/heads/prod' }}
3539
3640jobs :
@@ -49,23 +53,31 @@ jobs:
4953 - name : Configure environment
5054 id : meta
5155 env :
52- ENV_INPUT : ${{ inputs.environment }}
56+ PREVIEW_INPUT : ${{ inputs.preview }}
5357 REF : ${{ github.ref }}
5458 DEPLOY_SHA : ${{ inputs.head_sha || github.sha }}
59+ BRANCH_ALIAS : ${{ inputs.branch_alias }}
5560 run : |
5661 echo "cmd=deploy" >> $GITHUB_OUTPUT
5762
5863 SHA_SHORT="${DEPLOY_SHA::8}"
5964
60- if [ "$ENV_INPUT" = "staging-preview" ]; then
61- echo "env=staging" >> $GITHUB_OUTPUT
62- echo "url=https://git-$SHA_SHORT-frontend-staging.modrinth.workers.dev" >> $GITHUB_OUTPUT
63- echo "cmd=versions upload --preview-alias git-$SHA_SHORT --var PREVIEW:true" >> $GITHUB_OUTPUT
64-
65- elif [ "$ENV_INPUT" = "production-preview" ]; then
65+ if [ "$PREVIEW_INPUT" = "true" ]; then
66+ URL="https://git-$SHA_SHORT-frontend.modrinth.workers.dev"
67+ STAGING_URL="https://git-$SHA_SHORT-staging-frontend.modrinth.workers.dev"
6668 echo "env=production" >> $GITHUB_OUTPUT
67- echo "url=https://git-$SHA_SHORT-frontend.modrinth.workers.dev" >> $GITHUB_OUTPUT
68- echo "cmd=versions upload --preview-alias git-$SHA_SHORT --var PREVIEW:true" >> $GITHUB_OUTPUT
69+ echo "url=$URL" >> $GITHUB_OUTPUT
70+ echo "staging_url=$STAGING_URL" >> $GITHUB_OUTPUT
71+ echo "cmd=versions upload --preview-alias git-$SHA_SHORT --var PREVIEW:true --var CF_PAGES_URL:$URL" >> $GITHUB_OUTPUT
72+
73+ STAGING_VARS=$(jq -r '.env.staging.vars | to_entries[] | "--var \(.key):\(.value)"' ./apps/frontend/wrangler.jsonc | tr '\n' ' ')
74+ echo "staging_cmd=versions upload --preview-alias git-$SHA_SHORT-staging ${STAGING_VARS}--var PREVIEW:true --var CF_PAGES_URL:$STAGING_URL" >> $GITHUB_OUTPUT
75+
76+ if [ -n "$BRANCH_ALIAS" ]; then
77+ echo "branch_alias_url=https://$BRANCH_ALIAS-frontend.modrinth.workers.dev" >> $GITHUB_OUTPUT
78+ echo "staging_branch_alias_url=https://$BRANCH_ALIAS-staging-frontend.modrinth.workers.dev" >> $GITHUB_OUTPUT
79+ echo "staging_branch_alias_cmd=versions upload --preview-alias $BRANCH_ALIAS-staging ${STAGING_VARS}--var PREVIEW:true --var CF_PAGES_URL:https://$BRANCH_ALIAS-staging-frontend.modrinth.workers.dev" >> $GITHUB_OUTPUT
80+ fi
6981
7082 elif [ "$REF" = "refs/heads/main" ]; then
7183 echo "env=staging" >> $GITHUB_OUTPUT
@@ -120,7 +132,7 @@ jobs:
120132 CF_PAGES_COMMIT_SHA : ${{ inputs.head_sha || github.sha }}
121133 CF_PAGES_URL : ${{ steps.meta.outputs.url }}
122134 BUILD_ENV : ${{ steps.meta.outputs.env }}
123- PREVIEW : ${{ inputs.environment != '' && 'true' || 'false' }}
135+ PREVIEW : ${{ inputs.preview && 'true' || 'false' }}
124136 SENTRY_AUTH_TOKEN : ${{ secrets.SENTRY_AUTH_TOKEN }}
125137
126138 - name : Create Sentry release and upload sourcemaps
@@ -146,6 +158,39 @@ jobs:
146158 wranglerVersion : ' 4.54.0'
147159 command : ${{ steps.meta.outputs.cmd }}
148160
161+ - name : Deploy staging Cloudflare Worker preview
162+ if : ${{ inputs.preview }}
163+ uses : cloudflare/wrangler-action@9acf94ace14e7dc412b076f2c5c20b8ce93c79cd # v3.15.0
164+ with :
165+ apiToken : ${{ secrets.CF_API_TOKEN }}
166+ accountId : ${{ secrets.CF_ACCOUNT_ID }}
167+ workingDirectory : ./apps/frontend
168+ packageManager : pnpm
169+ wranglerVersion : ' 4.54.0'
170+ command : ${{ steps.meta.outputs.staging_cmd }}
171+
172+ - name : Deploy persistent Cloudflare Worker preview
173+ if : ${{ inputs.preview && inputs.branch_alias != '' }}
174+ uses : cloudflare/wrangler-action@9acf94ace14e7dc412b076f2c5c20b8ce93c79cd # v3.15.0
175+ with :
176+ apiToken : ${{ secrets.CF_API_TOKEN }}
177+ accountId : ${{ secrets.CF_ACCOUNT_ID }}
178+ workingDirectory : ./apps/frontend
179+ packageManager : pnpm
180+ wranglerVersion : ' 4.54.0'
181+ command : versions upload --preview-alias ${{ inputs.branch_alias }} --var PREVIEW:true --var CF_PAGES_URL:${{ steps.meta.outputs.branch_alias_url }}
182+
183+ - name : Deploy persistent staging Cloudflare Worker preview
184+ if : ${{ inputs.preview && inputs.branch_alias != '' }}
185+ uses : cloudflare/wrangler-action@9acf94ace14e7dc412b076f2c5c20b8ce93c79cd # v3.15.0
186+ with :
187+ apiToken : ${{ secrets.CF_API_TOKEN }}
188+ accountId : ${{ secrets.CF_ACCOUNT_ID }}
189+ workingDirectory : ./apps/frontend
190+ packageManager : pnpm
191+ wranglerVersion : ' 4.54.0'
192+ command : ${{ steps.meta.outputs.staging_branch_alias_cmd }}
193+
149194 - name : Purge cache
150195 if : github.ref == 'refs/heads/prod'
151196 run : |
@@ -155,14 +200,22 @@ jobs:
155200 --data '{"hosts": ["modrinth.com", "www.modrinth.com", "staging.modrinth.com"]}' \
156201 https://api.cloudflare.com/client/v4/zones/e39df17b9c4ef44cbce2646346ee6d33/purge_cache
157202
158- - name : Write deployment URL to file
159- if : ${{ inputs.environment != '' }}
203+ - name : Write deployment URLs to files
204+ if : ${{ inputs.preview }}
160205 run : |
161- echo "${{ steps.meta.outputs.url }}" > deployment-url-${{ inputs.environment }}.txt
206+ echo "${{ steps.meta.outputs.url }}" > deployment-url-production.txt
207+ echo "${{ steps.meta.outputs.staging_url }}" > deployment-url-staging.txt
208+
209+ - name : Upload production deployment URL
210+ if : ${{ inputs.preview }}
211+ uses : actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
212+ with :
213+ name : deployment-url-production
214+ path : deployment-url-production.txt
162215
163- - name : Upload deployment URL
164- if : ${{ inputs.environment != '' }}
216+ - name : Upload staging deployment URL
217+ if : ${{ inputs.preview }}
165218 uses : actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
166219 with :
167- name : deployment-url-${{ inputs.environment }}
168- path : deployment-url-${{ inputs.environment }} .txt
220+ name : deployment-url-staging
221+ path : deployment-url-staging .txt
0 commit comments