-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
name: Deploy to Cloudflare Workers | ||
|
||
on: | ||
workflow_dispatch: | ||
inputs: | ||
environment: | ||
description: 'wrangler environment to deploy to' | ||
required: true | ||
default: 'dev' | ||
type: choice | ||
options: | ||
- dev | ||
- prod | ||
commit: | ||
description: 'Git commit to deploy' | ||
default: 'main' | ||
required: true | ||
|
||
push: | ||
branches: | ||
- "main" | ||
repository_dispatch: | ||
|
||
env: | ||
GIT_REF: ${{ github.event.inputs.commit || github.ref }} | ||
WORKERS_ENV: ${{ github.event.inputs.environment || 'dev' }} | ||
|
||
jobs: | ||
deploy: | ||
name: Deploy worker | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
with: | ||
ref: ${{ env.GIT_REF }} | ||
|
||
- name: Install Wrangler | ||
run: npm install -g wrangler | ||
|
||
- name: Set Cloudflare Environment Variables | ||
run: | | ||
echo "CLOUDFLARE_ACCOUNT_ID=${{ secrets.CLOUDFLARE_ACCOUNT_ID }}" >> $GITHUB_ENV | ||
echo "CLOUDFLARE_API_TOKEN=${{ secrets.CLOUDFLARE_API_TOKEN }}" >> $GITHUB_ENV | ||
- name: Create or Use Existing KV Namespace | ||
id: create_or_find_kv | ||
run: | | ||
echo "Listing KV namespaces" | ||
namespace_output=$(wrangler kv:namespace list) | ||
echo "Namespace list output: $namespace_output" | ||
existing_namespace_id=$(echo "$namespace_output" | jq -r '.[] | select(.title == "oaifreehelper-oai_global_variables_preview") | .id') | ||
if [ -z "$existing_namespace_id" ]; then | ||
echo "No existing KV namespace found, creating a new one." | ||
namespace_creation_output=$(wrangler kv:namespace create "oai_global_variables" --preview) || exit 1 | ||
echo "Namespace creation output: $namespace_creation_output" | ||
namespace_id=$(echo "$namespace_creation_output" | grep -oP '(?<=preview_id = ")[^"]+') | ||
echo "CF_KV_NAMESPACE_ID=$namespace_id" >> $GITHUB_ENV | ||
else | ||
echo "Found existing KV namespace with ID: $existing_namespace_id" | ||
echo "CF_KV_NAMESPACE_ID=$existing_namespace_id" >> $GITHUB_ENV | ||
fi | ||
- name: Generate wrangler.toml | ||
run: | | ||
echo "name = \"oaifreehelper\"" > wrangler.toml | ||
echo "workers_dev = true" >> wrangler.toml | ||
echo "main = \"_worker.js\"" >> wrangler.toml | ||
echo "compatibility_date = \"2024-06-01\"" >> wrangler.toml | ||
echo "[[kv_namespaces]]" >> wrangler.toml | ||
echo "binding = \"oai_global_variables\"" >> wrangler.toml | ||
echo "id = \"$CF_KV_NAMESPACE_ID\"" >> wrangler.toml | ||
- name: Debug Environment Variables | ||
run: | | ||
echo "CLOUDFLARE_ACCOUNT_ID=$CLOUDFLARE_ACCOUNT_ID" | ||
echo "CLOUDFLARE_API_TOKEN=$CLOUDFLARE_API_TOKEN" | ||
echo "CF_KV_NAMESPACE_ID=$CF_KV_NAMESPACE_ID" | ||
- name: Publish to Cloudflare Workers | ||
env: | ||
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} | ||
CF_KV_NAMESPACE_ID: ${{ steps.create_or_find_kv.outputs.CF_KV_NAMESPACE_ID }} | ||
run: wrangler deploy |