feat:增加直链链接 #9
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 Worker | |
on: | |
workflow_dispatch: | |
repository_dispatch: | |
push: | |
branches: | |
- main | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Setup Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: '21' | |
- name: Install wrangler | |
run: npm install -g wrangler | |
- name: Check and Create KV Namespace | |
env: | |
CLOUDFLARE_API_TOKEN: ${{ secrets.CF_API_TOKEN }} | |
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }} | |
run: | | |
# 检查 PASTE_STORE | |
KV_NAMESPACE="PASTE_STORE" | |
echo "Checking for KV namespace: $KV_NAMESPACE" | |
LIST_OUTPUT=$(wrangler kv namespace list) | |
echo "KV namespace list output: $LIST_OUTPUT" | |
KV_ID=$(echo "$LIST_OUTPUT" | jq -r '.[] | select(.title == "cloudpaste-'$KV_NAMESPACE'") | .id') | |
if [ -z "$KV_ID" ]; then | |
echo "KV namespace $KV_NAMESPACE does not exist. Creating..." | |
CREATE_OUTPUT=$(wrangler kv namespace create "$KV_NAMESPACE") | |
echo "Create KV namespace output: $CREATE_OUTPUT" | |
KV_ID=$(echo "$CREATE_OUTPUT" | sed -n 's/.*id = "\([^"]*\)".*/\1/p') | |
if [ -z "$KV_ID" ]; then | |
echo "Failed to extract KV ID. Full output: $CREATE_OUTPUT" | |
exit 1 | |
fi | |
echo "KV namespace $KV_NAMESPACE created successfully with ID: $KV_ID" | |
else | |
echo "KV namespace $KV_NAMESPACE already exists with ID: $KV_ID" | |
fi | |
echo "PASTE_STORE_ID=$KV_ID" >> $GITHUB_ENV | |
# 检查 UPLOAD_STATUS | |
KV_NAMESPACE="UPLOAD_STATUS" | |
echo "Checking for KV namespace: $KV_NAMESPACE" | |
KV_ID=$(echo "$LIST_OUTPUT" | jq -r '.[] | select(.title == "cloudpaste-'$KV_NAMESPACE'") | .id') | |
if [ -z "$KV_ID" ]; then | |
echo "KV namespace $KV_NAMESPACE does not exist. Creating..." | |
CREATE_OUTPUT=$(wrangler kv namespace create "$KV_NAMESPACE") | |
echo "Create KV namespace output: $CREATE_OUTPUT" | |
KV_ID=$(echo "$CREATE_OUTPUT" | sed -n 's/.*id = "\([^"]*\)".*/\1/p') | |
if [ -z "$KV_ID" ]; then | |
echo "Failed to extract KV ID. Full output: $CREATE_OUTPUT" | |
exit 1 | |
fi | |
echo "KV namespace $KV_NAMESPACE created successfully with ID: $KV_ID" | |
else | |
echo "KV namespace $KV_NAMESPACE already exists with ID: $KV_ID" | |
fi | |
echo "UPLOAD_STATUS_ID=$KV_ID" >> $GITHUB_ENV | |
- name: Check and Create R2 Bucket | |
env: | |
CLOUDFLARE_API_TOKEN: ${{ secrets.CF_API_TOKEN }} | |
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }} | |
run: | | |
R2_BUCKET="cloudpaste-files" | |
R2_BINDING="FILE_STORE" | |
echo "Checking for R2 bucket: $R2_BUCKET" | |
# List buckets and check if our bucket exists | |
BUCKET_LIST=$(wrangler r2 bucket list) | |
BUCKET_EXISTS=$(echo "$BUCKET_LIST" | grep -w "$R2_BUCKET" || true) | |
if [ -z "$BUCKET_EXISTS" ]; then | |
echo "R2 bucket $R2_BUCKET does not exist. Creating..." | |
CREATE_OUTPUT=$(wrangler r2 bucket create "$R2_BUCKET" --location apac) | |
echo "R2 bucket $R2_BUCKET created successfully" | |
else | |
echo "R2 bucket $R2_BUCKET already exists" | |
fi | |
- name: Update wrangler.toml | |
run: | | |
# Read the entire content of wrangler.toml | |
WRANGLER_CONTENT=$(cat wrangler.toml) | |
# Update both KV namespace IDs | |
UPDATED_CONTENT=$(echo "$WRANGLER_CONTENT" | \ | |
sed -e "s/id = \"temporary-id-12345\"/id = \"$PASTE_STORE_ID\"/" \ | |
-e "s/id = \"temporary-id-67890\"/id = \"$UPLOAD_STATUS_ID\"/") | |
# Write the updated content back to wrangler.toml | |
echo "$UPDATED_CONTENT" > wrangler.toml | |
echo "Updated wrangler.toml content:" | |
cat wrangler.toml | |
- name: Deploy to Cloudflare Workers | |
uses: cloudflare/[email protected] | |
with: | |
apiToken: ${{ secrets.CF_API_TOKEN }} | |
command: deploy --var ADMIN_USERNAME:${{ secrets.ADMIN_USERNAME }} --var ADMIN_PASSWORD:${{ secrets.ADMIN_PASSWORD }} | |
env: | |
ADMIN_USERNAME: ${{ secrets.ADMIN_USERNAME }} | |
ADMIN_PASSWORD: ${{ secrets.ADMIN_PASSWORD }} |