@@ -2,23 +2,57 @@ name: Deploy Preview
22
33on :
44 pull_request :
5- branches : [main]
5+ push :
6+ branches-ignore : [main, develop]
67
78concurrency :
8- group : preview-${{ github.ref }}
9+ group : preview-${{ github.event.pull_request.number || github.ref_name }}
910 cancel-in-progress : true
1011
1112jobs :
12- deploy-preview :
13- name : Deploy Preview to Cloudflare Pages
13+ deploy :
14+ name : Deploy Preview to Cloudflare Workers
1415 runs-on : ubuntu-latest
1516 permissions :
1617 contents : read
1718 pull-requests : write
19+ deployments : write
20+
1821 steps :
1922 - name : Checkout code
2023 uses : actions/checkout@v6
2124
25+ - name : Compute preview identifiers
26+ id : preview_meta
27+ run : |
28+ if [[ "${{ github.event_name }}" == "pull_request" ]]; then
29+ CF_BRANCH="pr-${{ github.event.pull_request.number }}"
30+ CONTEXT_LABEL="PR #${{ github.event.pull_request.number }}"
31+ else
32+ RAW_BRANCH="${{ github.ref_name }}"
33+ SLUG="$(echo "$RAW_BRANCH" | tr '[:upper:]' '[:lower:]' | sed -E 's#[^a-z0-9]+#-#g; s#(^-+|-+$)##g' | cut -c1-40)"
34+ if [[ -z "$SLUG" ]]; then
35+ SLUG="branch"
36+ fi
37+ CF_BRANCH="br-$SLUG"
38+ CONTEXT_LABEL="Branch $RAW_BRANCH"
39+ fi
40+
41+ # Cloudflare Preview URLs (aliased) format:
42+ # <ALIAS>-<WORKER_NAME>.<SUBDOMAIN>.workers.dev
43+ # See: https://developers.cloudflare.com/workers/configuration/previews/
44+ PREVIEW_ALIAS="$CF_BRANCH"
45+ WORKER_NAME="aicodingstack"
46+ PREVIEW_URL="https://${PREVIEW_ALIAS}-${WORKER_NAME}.pr-preview.workers.dev"
47+
48+ {
49+ echo "cf_branch=$CF_BRANCH"
50+ echo "worker_name=$WORKER_NAME"
51+ echo "preview_alias=$PREVIEW_ALIAS"
52+ echo "preview_url=$PREVIEW_URL"
53+ echo "context_label=$CONTEXT_LABEL"
54+ } >> "$GITHUB_OUTPUT"
55+
2256 - name : Setup Node.js
2357 uses : actions/setup-node@v6
2458 with :
@@ -28,61 +62,72 @@ jobs:
2862 - name : Install dependencies
2963 run : npm ci
3064
31- - name : Run validation tests
32- run : npm run test:validate
33-
34- - name : Generate manifests and metadata
35- run : |
36- npm run generate:manifests
37- npm run generate:metadata
65+ - name : Run CI tests
66+ run : npm run test:ci
3867
3968 - name : Build with OpenNext
4069 run : npm run build
4170 env :
42- BUILD_TIME : ${{ github.event.pull_request.updated_at }}
43-
44- # TODO: Configure Cloudflare Pages deployment
45- # You'll need to add these secrets to your repository:
46- # - CLOUDFLARE_API_TOKEN
47- # - CLOUDFLARE_ACCOUNT_ID
48- #
49- # Uncomment the steps below after adding the secrets:
50- #
51- # - name: Deploy to Cloudflare Pages (Preview)
52- # id: deploy
53- # uses: cloudflare/wrangler-action@v3
54- # with:
55- # apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
56- # accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
57- # command: pages deploy .open-next --project-name=aicodingstack --branch=preview-${{ github.event.pull_request.number }}
58- #
59- # - name: Comment preview URL
60- # uses: actions/github-script@v7
61- # with:
62- # script: |
63- # const previewUrl = '${{ steps.deploy.outputs.deployment-url }}';
64- # const comment = `## 🚀 Preview Deployment
65- #
66- # Preview is ready!
67- #
68- # **Preview URL:** ${previewUrl}
69- #
70- # ### Build Details
71- # - **Commit:** ${context.sha.substring(0, 7)}
72- # - **Build Time:** ${{ github.event.pull_request.updated_at }}
73- #
74- # The preview will be automatically deleted when this PR is merged or closed.`;
75- #
76- # github.rest.issues.createComment({
77- # issue_number: context.issue.number,
78- # owner: context.repo.owner,
79- # repo: context.repo.repo,
80- # body: comment
81- # });
82-
83- - name : Preview deployment placeholder
71+ BUILD_TIME : ${{ github.event.pull_request.updated_at || github.event.head_commit.timestamp }}
72+
73+ - name : Deploy Preview
74+ uses : cloudflare/wrangler-action@v3
75+ with :
76+ apiToken : ${{ secrets.CLOUDFLARE_API_TOKEN }}
77+ accountId : ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
78+ command : versions upload --preview-alias ${{ steps.preview_meta.outputs.preview_alias }}
79+
80+ - name : Comment Preview URL
81+ if : github.event_name == 'pull_request'
82+ uses : actions/github-script@v7
83+ env :
84+ PREVIEW_URL : ${{ steps.preview_meta.outputs.preview_url }}
85+ with :
86+ script : |
87+ const prNumber = context.payload.pull_request.number;
88+ const previewUrl = process.env.PREVIEW_URL;
89+ const commitSha = context.sha.substring(0, 7);
90+
91+ // Find existing bot comment
92+ const { data: comments } = await github.rest.issues.listComments({
93+ owner: context.repo.owner,
94+ repo: context.repo.repo,
95+ issue_number: prNumber,
96+ });
97+
98+ const botComment = comments.find(c =>
99+ c.user.type === 'Bot' && c.body.includes('Preview deployment')
100+ );
101+
102+ const body = `### Preview deployment
103+
104+ | Status | URL |
105+ |--------|-----|
106+ | Ready | [${previewUrl}](${previewUrl}) |
107+
108+ **Commit:** \`${commitSha}\`
109+ **Updated:** ${new Date().toISOString()}`;
110+
111+ if (botComment) {
112+ await github.rest.issues.updateComment({
113+ owner: context.repo.owner,
114+ repo: context.repo.repo,
115+ comment_id: botComment.id,
116+ body
117+ });
118+ } else {
119+ await github.rest.issues.createComment({
120+ owner: context.repo.owner,
121+ repo: context.repo.repo,
122+ issue_number: prNumber,
123+ body
124+ });
125+ }
126+
127+ - name : Deployment summary
84128 run : |
85- echo "⚠️ Preview deployment not yet configured"
86- echo "To enable preview deployments:"
87- echo "1. Add CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID to repository secrets"
88- echo "2. Uncomment the deployment steps in .github/workflows/deploy-preview.yml"
129+ echo "### Preview Deployment" >> $GITHUB_STEP_SUMMARY
130+ echo "" >> $GITHUB_STEP_SUMMARY
131+ echo "- **Context:** ${{ steps.preview_meta.outputs.context_label }}" >> $GITHUB_STEP_SUMMARY
132+ echo "- **URL:** ${{ steps.preview_meta.outputs.preview_url }}" >> $GITHUB_STEP_SUMMARY
133+ echo "- **Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY
0 commit comments