-
-
Save abdulhannanali/c288eaf8f64b035e676c8021eeabe5c4 to your computer and use it in GitHub Desktop.
This file contains 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 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 |
This file contains 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
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