worker-deploy.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env bash
  2. # Deploys the managed siteverify Worker template to the user's account
  3. # and sets TURNSTILE_SECRET_KEY as a Worker secret.
  4. #
  5. # Reads:
  6. # $CLOUDFLARE_API_TOKEN (required)
  7. # $WIDGET_SECRET (required; secret captured from widget-create.sh)
  8. #
  9. # Args:
  10. # --name <worker-name> Base name; appends a hash suffix if taken
  11. # --deploy-dir <path> Where to extract the template. Default: /tmp/turnstile-siteverify-deploy
  12. #
  13. # Outputs JSON. Exit 0 on success, non-zero on failure.
  14. # ok: {"status":"ok","worker_url":"<url>","worker_name":"<name>"}
  15. # conflict: {"status":"error","reason":"name_conflict_after_retry"}
  16. # deploy_failed: {"status":"error","reason":"deploy_failed"}
  17. # set_secret: {"status":"error","reason":"set_secret_failed","worker_name":"<name>"}
  18. # url_parse: {"status":"error","reason":"url_parse_failed","worker_name":"<name>"}
  19. set -uo pipefail
  20. NAME="${WORKER_NAME:-turnstile-siteverify}"
  21. DEPLOY_DIR="/tmp/turnstile-siteverify-deploy"
  22. while [[ $# -gt 0 ]]; do
  23. case $1 in
  24. --name) NAME="$2"; shift 2 ;;
  25. --deploy-dir) DEPLOY_DIR="$2"; shift 2 ;;
  26. *) echo "worker-deploy: unknown arg $1" >&2; exit 2 ;;
  27. esac
  28. done
  29. : "${CLOUDFLARE_API_TOKEN:?CLOUDFLARE_API_TOKEN must be set}"
  30. : "${WIDGET_SECRET:?WIDGET_SECRET must be set}"
  31. deploy_log=$(mktemp)
  32. deploy() {
  33. local target_name="$1"
  34. rm -rf "$DEPLOY_DIR"
  35. npx --yes degit cloudflare/skills/skills/turnstile-spin/templates/worker "$DEPLOY_DIR" >/dev/null 2>&1
  36. # Capture both streams. Wrangler prints the success URL and version ID on
  37. # stdout; progress indicators on stderr. Capturing only stderr loses the URL.
  38. (cd "$DEPLOY_DIR" && npx wrangler deploy --name "$target_name") >"$deploy_log" 2>&1
  39. }
  40. if ! deploy "$NAME"; then
  41. if grep -q "script name already in use" "$deploy_log"; then
  42. NAME="${NAME}-$(openssl rand -hex 3 2>/dev/null || date +%s | tail -c 5)"
  43. echo "worker-deploy: name conflict; retrying as $NAME" >&2
  44. if ! deploy "$NAME"; then
  45. echo "worker-deploy: deploy failed even with new name" >&2
  46. cat "$deploy_log" >&2
  47. rm -f "$deploy_log"
  48. echo "{\"status\":\"error\",\"reason\":\"name_conflict_after_retry\"}"
  49. exit 1
  50. fi
  51. else
  52. cat "$deploy_log" >&2
  53. rm -f "$deploy_log"
  54. echo "{\"status\":\"error\",\"reason\":\"deploy_failed\"}"
  55. exit 1
  56. fi
  57. fi
  58. # Set the secret. Use `echo` (not `printf '%s'`); wrangler secret put expects
  59. # newline-terminated stdin; printf without a trailing newline lands an empty
  60. # secret in the runtime even though wrangler reports success.
  61. secret_log=$(mktemp)
  62. set_secret() {
  63. echo "$WIDGET_SECRET" | (cd "$DEPLOY_DIR" && npx wrangler secret put TURNSTILE_SECRET_KEY --name "$NAME") >"$secret_log" 2>&1
  64. }
  65. if ! set_secret; then
  66. echo "worker-deploy: failed to set TURNSTILE_SECRET_KEY on $NAME" >&2
  67. cat "$secret_log" >&2
  68. detail=$(tail -3 "$secret_log" | tr '\n' ' ' | sed 's/"/\\"/g' | head -c 200)
  69. rm -f "$deploy_log" "$secret_log"
  70. echo "{\"status\":\"error\",\"reason\":\"set_secret_failed\",\"worker_name\":\"$NAME\",\"detail\":\"$detail\"}"
  71. exit 1
  72. fi
  73. rm -f "$secret_log"
  74. sleep 5
  75. # Extract the deployed URL. Try workers.dev first, then any https URL in the
  76. # log that is not the well-known cloudflare.com host (custom domain deploys
  77. # and Workers for Platforms don't always land at a workers.dev hostname).
  78. worker_url=$(grep -oE 'https://[a-zA-Z0-9._-]+\.workers\.dev' "$deploy_log" | head -1)
  79. if [ -z "$worker_url" ]; then
  80. worker_url=$(grep -oE 'https://[a-zA-Z0-9.-]+(/[^[:space:]]*)?' "$deploy_log" \
  81. | grep -v -E 'cloudflare\.com|workers-sdk|github\.com' \
  82. | head -1)
  83. fi
  84. rm -f "$deploy_log"
  85. if [ -z "$worker_url" ]; then
  86. echo "worker-deploy: deployed but could not parse Worker URL from wrangler output" >&2
  87. echo "worker-deploy: ask the user for the URL printed by wrangler deploy and pass it to validate.sh manually" >&2
  88. echo "{\"status\":\"error\",\"reason\":\"url_parse_failed\",\"worker_name\":\"$NAME\"}"
  89. exit 1
  90. fi
  91. echo "{\"status\":\"ok\",\"worker_url\":\"$worker_url\",\"worker_name\":\"$NAME\"}"