11name : ' Preview Deployment'
22on :
33 pull_request_target :
4+ types : [opened, synchronize, reopened]
45 branches :
56 - master
67
78jobs :
8- deploy :
9- permissions :
10- actions : read
11- contents : read
12- deployments : write
13- pull-requests : write
9+ # Job 1: Build the code (no secrets here)
10+ build :
1411 runs-on : ubuntu-latest
15- timeout-minutes : 5
1612 steps :
1713 - name : Checkout code
1814 uses : actions/checkout@v4
15+ with :
16+ ref : ${{ github.event.pull_request.head.sha }}
17+ persist-credentials : false # Don't persist GitHub token
1918
2019 - name : Cache node_modules
2120 uses : actions/cache@v4
@@ -32,19 +31,49 @@ jobs:
3231 - run : yarn install
3332 - run : yarn build
3433
34+ - name : Upload build artifact
35+ uses : actions/upload-artifact@v4
36+ with :
37+ name : dist-files
38+ path : dist
39+
40+ # Job 2: Deploy with secrets (no PR code checkout)
41+ deploy :
42+ needs : build # Wait for build job to complete
43+ permissions :
44+ actions : read
45+ contents : read
46+ deployments : write
47+ issues : write
48+ pull-requests : write
49+ runs-on : ubuntu-latest
50+ timeout-minutes : 5
51+ steps :
52+ - name : Download build artifact
53+ uses : actions/download-artifact@v4
54+ with :
55+ name : dist-files
56+ path : dist
57+
58+ - name : Set short git commit SHA
59+ id : vars
60+ run : |
61+ calculatedSha=$(echo ${{ github.event.pull_request.head.sha }} | head -c 8)
62+ echo "COMMIT_SHORT_SHA=$calculatedSha" >> $GITHUB_ENV
63+
3564 - name : Deploy to Cloudflare
3665 id : deploy
3766 uses : cloudflare/wrangler-action@v3
3867 with :
3968 apiToken : ${{ secrets.CLOUDFLARE_API_TOKEN }}
4069 accountId : ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
41- command : pages deploy dist --project-name=${{ vars.CLOUDFLARE_PROJECT_NAME }} --branch ${{ github.head_ref }} --commit-dirty=true
70+ command : pages deploy dist --project-name=${{ vars.CLOUDFLARE_PROJECT_NAME }} --branch= ${{ env.COMMIT_SHORT_SHA }} --commit-dirty=true
4271
4372 - name : Add deployment comment
4473 uses : thollander/actions-comment-pull-request@v3
4574 with :
4675 message : |
47- Preview URL: ${{ steps.deploy.outputs.deployment-url }}
76+ Preview URL: ${{ steps.deploy.outputs.pages- deployment-alias -url }}
4877 reactions : eyes, rocket
4978 comment-tag : ' Preview URL'
5079 mode : recreate
0 commit comments