Merge remote-tracking branch 'origin/astro-pure-v4_0_3' into astro-pu… #1377
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy Astro site to GitHub Pages & Cloudflare | |
on: | |
push: | |
branches: [ astro-pure-v4_0_3 ] | |
workflow_dispatch: | |
permissions: | |
contents: read | |
pages: write | |
id-token: write | |
concurrency: | |
group: "pages-deploy" | |
cancel-in-progress: true | |
jobs: | |
build: | |
runs-on: ubuntu-22.04 | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Setup Bun | |
uses: oven-sh/setup-bun@v1 | |
with: | |
bun-version: latest | |
- name: Install Dependencies | |
run: bun install | |
# - name: Fetch Giscus comments | |
# run: node src/scripts/fetch-comments.js | |
# env: | |
# # Ensure this secret has read access to discussions | |
# GITHUB_TOKEN: ${{ secrets.gh_action_token_PAT }} | |
- name: Build Production | |
run: bun run build | |
- name: Upload GitHub Pages artifact | |
uses: actions/upload-pages-artifact@v3 | |
with: | |
path: dist | |
deploy-github: | |
needs: build | |
runs-on: ubuntu-latest | |
environment: | |
name: github-pages | |
url: ${{ steps.deployment.outputs.page_url }} | |
steps: | |
- name: Deploy to GitHub Pages | |
id: deployment | |
uses: actions/deploy-pages@v4 | |
deploy-cloudflare: | |
needs: build | |
runs-on: ubuntu-latest | |
steps: | |
- name: Create Worker script | |
run: | | |
mkdir -p worker | |
cat > worker/wrangler.toml << EOF | |
name = "site-proxy" | |
main = "worker.js" | |
compatibility_date = "2024-01-01" | |
[[routes]] | |
pattern = "${{ secrets.DOMAIN_NAME }}/*" | |
zone_id = "${{ secrets.CLOUDFLARE_ZONE_ID }}" | |
EOF | |
# Note: Using a single secret GH_PAGES_URL for consistency. | |
# Make sure this secret is set correctly in your repository settings. | |
# It should be the base URL of your GitHub Pages site, e.g., "catcodeme.github.io". | |
cat > worker/worker.js << 'EOF' | |
export default { | |
async fetch(request, env, ctx) { | |
const url = new URL(request.url); | |
// All users will be proxied to GitHub Pages through this worker. | |
// The logic for differentiating users can be added here if needed. | |
const githubUrl = `https://${{ secrets.GH_PAGES_URL }}${url.pathname}${url.search}`; | |
// Implement caching strategy | |
const cacheKey = new Request(url.toString(), request); | |
const cache = caches.default; | |
let response = await cache.match(cacheKey); | |
if (!response) { | |
console.log(`Cache miss for ${url.toString()}. Fetching from origin.`); | |
response = await fetch(githubUrl, { | |
headers: { 'User-Agent': 'Cloudflare-Worker-Proxy' } | |
}); | |
if (response.ok) { | |
const headers = new Headers(response.headers); | |
headers.set('Cache-Control', 'public, max-age=3600'); // Cache for 1 hour | |
const cachedResponse = new Response(response.body, { | |
status: response.status, | |
statusText: response.statusText, | |
headers: headers | |
}); | |
ctx.waitUntil(cache.put(cacheKey, cachedResponse.clone())); | |
return cachedResponse; | |
} | |
} else { | |
console.log(`Cache hit for ${url.toString()}.`); | |
} | |
return response; | |
} | |
}; | |
EOF | |
- name: Deploy to Cloudflare Workers | |
uses: cloudflare/wrangler-action@v3 | |
with: | |
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
workingDirectory: 'worker' |