From e37f93d3e2995dad7d44ab3f14530828f01f0f35 Mon Sep 17 00:00:00 2001 From: eoao Date: Thu, 25 Sep 2025 22:23:00 +0800 Subject: [PATCH] =?UTF-8?q?Github=20Action=20=E6=94=AF=E6=8C=81=E6=97=A0R2?= =?UTF-8?q?,=20=E8=87=AA=E5=8A=A8=E8=8E=B7=E5=8F=96=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy-cloudflare.yml | 69 ++++++++++++------------- mail-worker/wrangler-action.toml | 10 ++-- 2 files changed, 38 insertions(+), 41 deletions(-) diff --git a/.github/workflows/deploy-cloudflare.yml b/.github/workflows/deploy-cloudflare.yml index 3be97ee..e3436c4 100644 --- a/.github/workflows/deploy-cloudflare.yml +++ b/.github/workflows/deploy-cloudflare.yml @@ -22,53 +22,62 @@ jobs: DOMAIN: ${{ secrets.DOMAIN }} ADMIN: ${{ secrets.ADMIN }} JWT_SECRET: ${{ secrets.JWT_SECRET }} - INIT_URL: ${{ secrets.INIT_URL }} - + outputs: deployment_skipped: ${{ steps.deploy.outputs.deployment_skipped }} + worker_url: ${{ steps.deploy.outputs.worker_url }} steps: - - name: ➡️ Checkout repository + - name: ➡️ 检出代码仓库 - Checkout repository uses: actions/checkout@v4 - - name: 📦 Setup pnpm + - name: 📦 设置 pnpm - Setup pnpm uses: pnpm/action-setup@v4.1.0 with: version: latest - - name: 📦 Setup Node.js + - name: 📦 设置 Node.js - Setup Node.js uses: actions/setup-node@v4 with: node-version: "20" cache: "pnpm" cache-dependency-path: "./mail-worker/pnpm-lock.yaml" - - name: 📥 Install dependencies + - name: 📥 安装依赖 - Install dependencies run: pnpm install --frozen-lockfile working-directory: ./mail-worker - - name: 📡 Disable wrangler telemetry + - name: 📡 禁用 Wrangler 遥测 - Disable wrangler telemetry working-directory: ./mail-worker run: npx wrangler telemetry disable - - name: 🛠️ Prepare Config, Set Secrets and Deploy + - name: 🛠️ 设置环境变量和部署 - Set Secrets and Deploy id: deploy working-directory: ./mail-worker run: | - if [ -z "$D1_DATABASE_ID" ] || [ -z "$KV_NAMESPACE_ID" ] || [ -z "$R2_BUCKET_NAME" ]; then - echo "⚠️ Required secrets (D1_DATABASE_ID, KV_NAMESPACE_ID, or R2_BUCKET_NAME) are not set." - echo "🟡 Skipping deployment." + if [ -z "$D1_DATABASE_ID" ] || [ -z "$KV_NAMESPACE_ID" ]; then + echo "⚠️ Required secrets (D1_DATABASE_ID or KV_NAMESPACE_ID) are not set." + echo "❌ Skipping deployment." echo "deployment_skipped=true" >> $GITHUB_OUTPUT exit 0 fi echo "deployment_skipped=false" >> $GITHUB_OUTPUT + CONFIG_FILE="wrangler-action.toml" + + if [ -z "$R2_BUCKET_NAME" ]; then + sed -i "18,20d" "$CONFIG_FILE" + fi + echo "⚙️ Dynamically updating '$CONFIG_FILE' with binding IDs..." sed -i "s|\${D1_DATABASE_ID}|${D1_DATABASE_ID}|g" "$CONFIG_FILE" sed -i "s|\${KV_NAMESPACE_ID}|${KV_NAMESPACE_ID}|g" "$CONFIG_FILE" sed -i "s|\${R2_BUCKET_NAME}|${R2_BUCKET_NAME}|g" "$CONFIG_FILE" + sed -i "s|\"\${DOMAIN}\"|${DOMAIN}|g" "$CONFIG_FILE" + sed -i "s|\${ADMIN}|${ADMIN}|g" "$CONFIG_FILE" + sed -i "s|\${JWT_SECRET}|${JWT_SECRET}|g" "$CONFIG_FILE" echo "🔍 Debug: Checking configuration after replacement..." echo "R2_BUCKET_NAME value: '$R2_BUCKET_NAME'" @@ -76,51 +85,39 @@ jobs: grep -A 2 -B 2 "bucket_name" "$CONFIG_FILE" || true echo "🔒 Setting Worker secrets..." - WORKER_NAME="cloud-mail" - for VAR in DOMAIN ADMIN JWT_SECRET; do - if [ -n "${!VAR}" ]; then - VAR_LOWER=$(echo "$VAR" | tr '[:upper:]' '[:lower:]') - echo ">> Processing secret: '$VAR_LOWER'" - (echo "${!VAR}" | npx wrangler secret put "$VAR_LOWER" --name "$WORKER_NAME" -c "$CONFIG_FILE") || true - else - echo "⚠️ Warning: GitHub Secret '$VAR' is not set. Skipping." - fi - done - echo "🚀 Configuration updated. Starting deployment..." - npx wrangler deploy -c "$CONFIG_FILE" | grep -v "https://.*\.workers\.dev" || true + npx wrangler deploy -c "$CONFIG_FILE" | tee deploy.log | grep -v "https://.*\.workers\.dev" + WORKER_URL=$(grep -o "https://.*\.workers\.dev" deploy.log) + echo "::add-mask::$WORKER_URL" + echo "worker_url=$WORKER_URL" >> $GITHUB_OUTPUT echo "✅ Deployment command executed." - - name: 🗄️ Initialize Database (if INIT_URL is set) + - name: 🗄️ 初始化数据库 - Initialize Database if: ${{ steps.deploy.outputs.deployment_skipped == 'false' }} run: | - if [ -z "$INIT_URL" ]; then - echo "✅ Deployment successful. INIT_URL not set, skipping initialization." - exit 0 - fi - echo "⏳ Waiting 15 秒之前 before checking initialization status..." + echo "⏳ Waiting 15s before checking initialization status..." sleep 15 - HTTP_CODE=$(curl -s -w "%{http_code}" -o response.txt "$INIT_URL") + HTTP_CODE=$(curl -s -w "%{http_code}" -o response.txt "${{ steps.deploy.outputs.worker_url }}/api/init/${JWT_SECRET}") RESPONSE_BODY=$(cat response.txt) echo "🔎 Checking response... (Status: $HTTP_CODE)" - if [ "$HTTP_CODE" = "200" ] && [ "$RESPONSE_BODY" = "初始化成功" ]; then - echo "🎉✅ Fresh initialization successful!" + if [ "$HTTP_CODE" = "200" ] && [ "$RESPONSE_BODY" = "初始化成功" -o "$RESPONSE_BODY" = "Successfully initialized" ]; then + echo "✅ Database initialization successful!" elif [ "$HTTP_CODE" = "200" ]; then - echo "✅ Database is already initialized or in a stable state. Response: $RESPONSE_BODY" + echo "❌ Database initialization error: $RESPONSE_BODY" else - echo "⚠️ Database initialization check failed with HTTP status: $HTTP_CODE. Please check your worker logs." + echo "❌ Database initialization check failed with HTTP status: $HTTP_CODE. response: $RESPONSE_BODY" fi - - name: 📣 Notify Final Status + - name: 📣 部署状态 - Notify Final Status if: always() run: | if [ "${{ job.status }}" == "success" ]; then if [ "${{ steps.deploy.outputs.deployment_skipped }}" == "true" ]; then - echo "🟡 Deployment was skipped due to missing configuration." + echo "❌ Deployment was skipped due to missing configuration." else echo "🎉🎉🎉 Hooray! Deployment completed successfully! 🎉🎉🎉" fi diff --git a/mail-worker/wrangler-action.toml b/mail-worker/wrangler-action.toml index 3537fb7..b3c5558 100644 --- a/mail-worker/wrangler-action.toml +++ b/mail-worker/wrangler-action.toml @@ -1,7 +1,7 @@ name = "cloud-mail" main = "src/index.js" compatibility_date = "2025-06-04" -keep_vars = true + [observability] enabled = true @@ -29,11 +29,11 @@ run_worker_first = true crons = ["0 16 * * *"] #定时任务每天晚上12点执行 -#[vars] +[vars] #orm_log = false -#domain = [] #邮件域名可可配置多个 示例: ["example1.com","example2.com"] -#admin = "" #管理员的邮箱 示例: admin@example.com -#jwt_secret = "" #jwt令牌的密钥,随便填一串字符串 +domain = "${DOMAIN}" #邮件域名可可配置多个 示例: ["example1.com","example2.com"] +admin = "${ADMIN}" #管理员的邮箱 示例: admin@example.com +jwt_secret = "${JWT_SECRET}" #jwt令牌的密钥,随便填一串字符串 [build] command = "pnpm --prefix ../mail-vue install && pnpm --prefix ../mail-vue run build"