Deploy Natali Proxy Worker #6
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 Natali Proxy Worker | |
| on: | |
| workflow_dispatch: | |
| permissions: | |
| contents: read | |
| jobs: | |
| deploy-proxy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Create & Deploy Natali Proxy Worker | |
| env: | |
| CF_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
| run: | | |
| set -x | |
| ZONE_ID="5b3f8237bfa2518bff375eb8163623a5" | |
| WORKER_NAME="natalie-proxy" | |
| GH_PAGES_DOMAIN="spivanatalie64.github.io" | |
| # Create the Worker script using Service Worker format | |
| cat > /tmp/worker.js << 'WORKEREOF' | |
| addEventListener('fetch', event => { | |
| event.respondWith(handleRequest(event.request)); | |
| }); | |
| async function handleRequest(request) { | |
| const url = new URL(request.url); | |
| const originUrl = 'https://spivanatalie64.github.io' + url.pathname + url.search; | |
| const modifiedHeaders = new Headers(request.headers); | |
| modifiedHeaders.set('Host', 'spivanatalie64.github.io'); | |
| const response = await fetch(originUrl, { | |
| method: request.method, | |
| headers: modifiedHeaders, | |
| body: request.body, | |
| }); | |
| const newHeaders = new Headers(response.headers); | |
| newHeaders.set('X-Proxied-By', 'natalie-worker'); | |
| return new Response(response.body, { | |
| status: response.status, | |
| statusText: response.statusText, | |
| headers: newHeaders, | |
| }); | |
| } | |
| WORKEREOF | |
| # Also create metadata for ES module | |
| cat > /tmp/metadata.json << 'METADATAEOF' | |
| { | |
| "body_part": "script", | |
| "bindings": [], | |
| "compatibility_date": "2026-06-20", | |
| "compatibility_flags": [] | |
| } | |
| METADATAEOF | |
| # Upload worker as multipart | |
| echo "=== Uploading worker ===" | |
| curl -s -X PUT \ | |
| "https://api.cloudflare.com/client/v4/accounts/6a46b418508c26278ae2d3373f41da81/workers/scripts/$WORKER_NAME" \ | |
| -H "Authorization: Bearer $CF_API_TOKEN" \ | |
| -F "metadata=@/tmp/metadata.json;type=application/json" \ | |
| -F "script=@/tmp/worker.js;type=application/javascript" | jq . | |
| # Update the route (in case it already exists from previous attempt) | |
| echo "=== Updating route ===" | |
| # List routes to check | |
| EXISTING_ROUTES=$(curl -s -H "Authorization: Bearer $CF_API_TOKEN" \ | |
| "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/workers/routes") | |
| NATALIE_ROUTE_ID=$(echo "$EXISTING_ROUTES" | jq -r '.result[] | select(.pattern == "natalie.acreetionos.org/*") | .id') | |
| if [ -n "$NATALIE_ROUTE_ID" ] && [ "$NATALIE_ROUTE_ID" != "null" ]; then | |
| echo "Updating existing route: $NATALIE_ROUTE_ID" | |
| curl -s -X PUT \ | |
| "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/workers/routes/$NATALIE_ROUTE_ID" \ | |
| -H "Authorization: Bearer $CF_API_TOKEN" \ | |
| -H "Content-Type: application/json" \ | |
| -d "{ | |
| \"pattern\": \"natalie.acreetionos.org/*\", | |
| \"script\": \"$WORKER_NAME\" | |
| }" | jq . | |
| else | |
| echo "Creating new route" | |
| curl -s -X POST \ | |
| "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/workers/routes" \ | |
| -H "Authorization: Bearer $CF_API_TOKEN" \ | |
| -H "Content-Type: application/json" \ | |
| -d "{ | |
| \"pattern\": \"natalie.acreetionos.org/*\", | |
| \"script\": \"$WORKER_NAME\" | |
| }" | jq . | |
| fi | |
| echo "=== Done ===" |