Skip to content

Instantly share code, notes, and snippets.

@abdulhannanali
Last active July 30, 2022 11:44
Show Gist options
  • Save abdulhannanali/c288eaf8f64b035e676c8021eeabe5c4 to your computer and use it in GitHub Desktop.
Save abdulhannanali/c288eaf8f64b035e676c8021eeabe5c4 to your computer and use it in GitHub Desktop.
name: Deploy Cloudflare Worker for All Github Environments
on:
push:
branches:
- main
jobs:
deploy-production-default:
runs-on: ubuntu-latest
name: Build and Deploy To Cloudflare
steps:
- uses: actions/checkout@v3
- name: Run npm install
run: npm install
- name: Publishing Worker for Github Environment ProdDefault
uses: cloudflare/wrangler-action@2.0.0
with:
apiToken: ${{ secrets.CF_API_TOKEN }}
accountId: ${{ secrets.CF_ACCOUNT_ID }}
command: publish --env proddefault
export interface Env {
START_PATH: string;
TARGET_URL: string;
}
export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext
): Promise<Response> {
const url = new URL(request.url)
if (url.pathname.startsWith(env.START_PATH)) {
return fetch(
getTargetURL(env, url),
request
)
}
return fetch(request)
},
};
function getTargetURL (env: Env, currentURL: URL): string {
const targetURL = new URL(env.TARGET_URL)
targetURL.pathname = currentURL.pathname
return targetURL.toString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment