@@ -2,23 +2,52 @@ 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+ WORKER_NAME="aicodingstack-$CF_BRANCH"
42+ PREVIEW_URL="https://${WORKER_NAME}.pr-preview.workers.dev"
43+
44+ {
45+ echo "cf_branch=$CF_BRANCH"
46+ echo "worker_name=$WORKER_NAME"
47+ echo "preview_url=$PREVIEW_URL"
48+ echo "context_label=$CONTEXT_LABEL"
49+ } >> "$GITHUB_OUTPUT"
50+
2251 - name : Setup Node.js
2352 uses : actions/setup-node@v6
2453 with :
@@ -28,61 +57,72 @@ jobs:
2857 - name : Install dependencies
2958 run : npm ci
3059
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
60+ - name : Run CI tests
61+ run : npm run test:ci
3862
3963 - name : Build with OpenNext
4064 run : npm run build
4165 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
66+ BUILD_TIME : ${{ github.event.pull_request.updated_at || github.event.head_commit.timestamp }}
67+
68+ - name : Deploy Preview
69+ uses : cloudflare/wrangler-action@v3
70+ with :
71+ apiToken : ${{ secrets.CLOUDFLARE_API_TOKEN }}
72+ accountId : ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
73+ command : deploy --branch ${{ steps.preview_meta.outputs.cf_branch }}
74+
75+ - name : Comment Preview URL
76+ if : github.event_name == 'pull_request'
77+ uses : actions/github-script@v7
78+ env :
79+ PREVIEW_URL : ${{ steps.preview_meta.outputs.preview_url }}
80+ with :
81+ script : |
82+ const prNumber = context.payload.pull_request.number;
83+ const previewUrl = process.env.PREVIEW_URL;
84+ const commitSha = context.sha.substring(0, 7);
85+
86+ // Find existing bot comment
87+ const { data: comments } = await github.rest.issues.listComments({
88+ owner: context.repo.owner,
89+ repo: context.repo.repo,
90+ issue_number: prNumber,
91+ });
92+
93+ const botComment = comments.find(c =>
94+ c.user.type === 'Bot' && c.body.includes('Preview deployment')
95+ );
96+
97+ const body = `### Preview deployment
98+
99+ | Status | URL |
100+ |--------|-----|
101+ | Ready | [${previewUrl}](${previewUrl}) |
102+
103+ **Commit:** \`${commitSha}\`
104+ **Updated:** ${new Date().toISOString()}`;
105+
106+ if (botComment) {
107+ await github.rest.issues.updateComment({
108+ owner: context.repo.owner,
109+ repo: context.repo.repo,
110+ comment_id: botComment.id,
111+ body
112+ });
113+ } else {
114+ await github.rest.issues.createComment({
115+ owner: context.repo.owner,
116+ repo: context.repo.repo,
117+ issue_number: prNumber,
118+ body
119+ });
120+ }
121+
122+ - name : Deployment summary
84123 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"
124+ echo "### Preview Deployment" >> $GITHUB_STEP_SUMMARY
125+ echo "" >> $GITHUB_STEP_SUMMARY
126+ echo "- **Context:** ${{ steps.preview_meta.outputs.context_label }}" >> $GITHUB_STEP_SUMMARY
127+ echo "- **URL:** ${{ steps.preview_meta.outputs.preview_url }}" >> $GITHUB_STEP_SUMMARY
128+ echo "- **Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY
0 commit comments