1- name : Deploy Astro site to GitHub Pages & Cloudflare Pages
1+ name : Deploy Astro site to GitHub Pages & Cloudflare
22
33on :
44 push :
55 branches : [ astro-pure-v4_0_3 ]
66 workflow_dispatch :
7- workflow_run :
8- workflows : [build, deploy]
9- branches : [astro-pure-v4_0_3]
10- discussion :
11- types : [created, edited, deleted]
12- discussion_comment :
13- types : [created, edited, deleted]
14-
15- concurrency :
16- group : " pages-deploy"
17- cancel-in-progress : true
187
198permissions :
209 contents : read
2110 pages : write
2211 id-token : write
2312
24- jobs :
25- setup :
26- runs-on : ubuntu-22.04
27- steps :
28- - uses : actions/checkout@v4
29- with :
30- token : ${{ secrets.gh_action_token_PAT }}
31- fetch-depth : 0
32-
33- - name : Setup Bun
34- uses : oven-sh/setup-bun@v1
35- with :
36- bun-version : latest
13+ concurrency :
14+ group : " pages-deploy"
15+ cancel-in-progress : true
3716
17+ jobs :
3818 build :
39- needs : setup
4019 runs-on : ubuntu-22.04
41- outputs :
42- # 输出构建产物路径,供后续 job 使用
43- artifact-path : ${{ steps.build.outputs.artifact-path }}
4420 steps :
45- - uses : actions/checkout@v4
21+ - name : Checkout
22+ uses : actions/checkout@v4
4623 with :
47- token : ${{ secrets.gh_action_token_PAT }}
4824 fetch-depth : 0
49- submodules : ' recursive'
5025
5126 - name : Setup Bun
5227 uses : oven-sh/setup-bun@v1
@@ -59,50 +34,36 @@ jobs:
5934 - name : Fetch Giscus comments
6035 run : node src/scripts/fetch-comments.js
6136 env :
62- GITHUB_TOKEN_READ : ${{ secrets.gh_action_token_PAT }}
37+ # Ensure this secret has read access to discussions
38+ GITHUB_TOKEN : ${{ secrets.gh_action_token_PAT }}
6339
6440 - name : Build Production
65- id : build
66- run : |
67- bun run build
68- echo "artifact-path=dist" >> $GITHUB_OUTPUT
41+ run : bun run build
6942
70- # 为 GitHub Pages 上传 artifact
7143 - name : Upload GitHub Pages artifact
7244 uses : actions/upload-pages-artifact@v3
7345 with :
7446 path : dist
7547
76- # 为 Cloudflare Pages 缓存构建产物
77- - name : Cache build for Cloudflare
78- uses : actions/cache/save@v3
79- with :
80- path : dist
81- key : build-${{ github.sha }}-${{ github.run_id }}
82-
83- # GitHub Pages 部署 (海外访问)
8448 deploy-github :
8549 needs : build
50+ runs-on : ubuntu-latest
8651 environment :
8752 name : github-pages
8853 url : ${{ steps.deployment.outputs.page_url }}
89- runs-on : ubuntu-latest
9054 steps :
9155 - name : Deploy to GitHub Pages
9256 id : deployment
9357 uses : actions/deploy-pages@v4
9458
95- # Cloudflare Worker 部署 (智能分流)
9659 deploy-cloudflare :
9760 needs : build
9861 runs-on : ubuntu-latest
9962 steps :
100- - uses : actions/checkout@v4
101-
10263 - name : Create Worker script
10364 run : |
10465 mkdir -p worker
105- cat > worker/wrangler.toml << ' EOF'
66+ cat > worker/wrangler.toml << EOF
10667 name = "site-proxy"
10768 main = "worker.js"
10869 compatibility_date = "2024-01-01"
@@ -112,62 +73,51 @@ jobs:
11273 zone_id = "${{ secrets.CLOUDFLARE_ZONE_ID }}"
11374 EOF
11475
115- cat > worker/worker.js << 'EOF'
76+ # Note: Using a single secret GH_PAGES_URL for consistency.
77+ # Make sure this secret is set correctly in your repository settings.
78+ # It should be the base URL of your GitHub Pages site, e.g., "catcodeme.github.io".
79+ cat > worker/worker.js << EOF
11680 export default {
117- async fetch(request) {
81+ async fetch(request, env, ctx ) {
11882 const url = new URL(request.url);
119- const country = request.cf?.country || 'US';
120- const isChinaUser = ['CN', 'HK', 'MO', 'TW'].includes(country);
12183
122- if (isChinaUser) {
123- const cacheKey = new Request(url.toString(), request);
124- const cache = caches.default;
125- let response = await cache.match(cacheKey);
84+ // All users will be proxied to GitHub Pages through this worker.
85+ // The logic for differentiating users can be added here if needed.
86+ const githubUrl = `https://${{ secrets.GH_PAGES_URL }}${url.pathname}${url.search}`;
87+
88+ // Implement caching strategy
89+ const cacheKey = new Request(url.toString(), request);
90+ const cache = caches.default;
91+ let response = await cache.match(cacheKey);
92+
93+ if (!response) {
94+ console.log(`Cache miss for ${url.toString()}. Fetching from origin.`);
95+ response = await fetch(githubUrl, {
96+ headers: { 'User-Agent': 'Cloudflare-Worker-Proxy' }
97+ });
12698
127- if (!response) {
128- const githubUrl = `https://${{ secrets.GH_PAGES_URL }}${url.pathname}${url.search}`;
129- response = await fetch(githubUrl, {
130- headers: { 'User-Agent': 'CF-Worker' }
99+ if (response.ok) {
100+ const headers = new Headers(response.headers);
101+ headers.set('Cache-Control', 'public, max-age=3600'); // Cache for 1 hour
102+ const cachedResponse = new Response(response.body, {
103+ status: response.status,
104+ statusText: response.statusText,
105+ headers: headers
131106 });
132-
133- if (response.ok) {
134- const headers = new Headers(response.headers);
135- headers.set('Cache-Control', 'public, max-age=3600');
136- const cachedResponse = new Response(response.body, {
137- status: response.status,
138- headers: headers
139- });
140- caches.default.put(cacheKey, cachedResponse.clone());
141- return cachedResponse;
142- }
107+ ctx.waitUntil(cache.put(cacheKey, cachedResponse.clone()));
108+ return cachedResponse;
143109 }
144- return response;
110+ } else {
111+ console.log(`Cache hit for ${url.toString()}.`);
145112 }
146113
147- // 海外用户直接访问 GitHub Pages
148- const githubUrl = `https://${{ secrets.GITHUB_PAGES_URL }}${url.pathname}${url.search}`;
149- return fetch(githubUrl, {
150- method: request.method,
151- headers: request.headers,
152- body: request.body
153- });
114+ return response;
154115 }
155116 };
156117 EOF
157118
158- - name : Deploy Worker
119+ - name : Deploy to Cloudflare Workers
159120 uses : cloudflare/wrangler-action@v3
160121 with :
161122 apiToken : ${{ secrets.CLOUDFLARE_API_TOKEN }}
162123 workingDirectory : ' worker'
163-
164- # # 可选: 部署状态通知
165- # notify:
166- # needs: [deploy-github, deploy-cloudflare]
167- # runs-on: ubuntu-latest
168- # if: always()
169- # steps:
170- # - name: Deployment Status
171- # run: |
172- # echo "GitHub Pages: ${{ needs.deploy-github.result }}"
173- # echo "Cloudflare Pages: ${{ needs.deploy-cloudflare.result }}"
0 commit comments