skymethod / denoflare

Develop, test, and deploy Cloudflare Workers with Deno.
https://denoflare.dev
MIT License
710 stars 36 forks source link

The uploaded script has no registered event handlers #72

Closed HTMHell closed 3 months ago

HTMHell commented 9 months ago

When I run it locally, it works. When I run it via Github Actions (ubuntu-latest), I get the following error:

bundling index into bundle.js...
bundle finished (process) in 2400ms
computed bindings in 1ms
putting module-based worker index... (0bytes) (20bytes compressed)
error: Uncaught (in promise) Error: putScript failed: status=400, errors=10068 The uploaded script has no registered event handlers.
    at execute (https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/common/cloudflare_api.ts:1343:15)
    at eventLoopTick (ext:core/01_core.js:153:7)
    at async putScript (https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/common/cloudflare_api.ts:97:13)
    at async buildAndPutScript (https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/cli/cli_push.ts:86:9)
    at async Object.push [as handler] (https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/cli/cli_push.ts:120:5)
    at async CliCommand.routeSubcommand (https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/cli/cli_command.ts:104:13)
    at async https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/cli/cli.ts:33:5

This is the script I run:

#!/bin/sh

# abort on any non-zero exit code
set -e

# ensure required environment variables are defined
if [ -z "$SUPABASE_DB_URL" ] || [ -z "$CF_ACCOUNT_ID" ] || [ -z "$CF_API_TOKEN" ] || [ -z "$CF_CUSTOM_DOMAIN" ] || [ -z "$CF_SCRIPT_NAME" ]; then
  echo "\$SUPABASE_DB_URL, \$CF_ACCOUNT_ID, \$CF_API_TOKEN, \$CF_CUSTOM_DOMAIN, and \$CF_SCRIPT_NAME are required"
  exit 1
fi

# install deno
DENO_VERSION="v1.40.5"
DENOFLARE_VERSION="v0.5.11"
curl -fsSL https://deno.land/x/install/install.sh | DENO_INSTALL=./deno-$DENO_VERSION sh -s $DENO_VERSION

# run unit tests as a sanity check
# NO_COLOR=1 DENO_VERSION=$DENO_VERSION ./deno-$DENO_VERSION/bin/deno test

# denoflare push the worker script to cloudflare
NO_COLOR=1 DENO_VERSION=$DENO_VERSION DENOFLARE_VERSION=${DENOFLARE_VERSION} ./deno-$DENO_VERSION/bin/deno run --unstable --allow-all https://raw.githubusercontent.com/skymethod/denoflare/$DENOFLARE_VERSION/cli/cli.ts \
push ./supabase/functions/cloudflare/worker.ts --account-id $CF_ACCOUNT_ID --api-token $CF_API_TOKEN --custom-domain $CF_CUSTOM_DOMAIN --name $CF_SCRIPT_NAME \
--secret-binding SUPABASE_DB_URL:$SUPABASE_DB_URL

My worker.ts:

export default {
  async fetch(req: Request, env: Record<string, string>) {
    ...
    return new Response();
  },
};

Any help would be appreciated.

johnspurlock-skymethod commented 9 months ago

Hmm, did you check out your repo as the first step of your gh action?

Doesn't look like it is finding your worker at ./supabase/functions/cloudflare/worker.ts for whatever the cwd happens to be

Here is a public example action I have that might be helpful, note the checkout step and the path in the deploy.sh: https://github.com/skymethod/op3/blob/master/.github/workflows/deploy-on-push.yml

HTMHell commented 9 months ago

@johnspurlock-skymethod Thank you for your reply. I do a checkout as a first step, I also tried to run the deployment script as a step after:

name: Deploy to Production

on:
  workflow_dispatch:

jobs:
  release:
    runs-on: ubuntu-latest

    env:
      SUPABASE_ACCESS_TOKEN: ${{ secrets.SUPABASE_ACCESS_TOKEN }}
      SUPABASE_DB_PASSWORD: ${{ secrets.PRODUCTION_DB_PASSWORD }}
      PROJECT_ID: foobar

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Set up environment variables
        run: |
          echo "SUPABASE_DB_URL=postgres://postgres.${PROJECT_ID}:${SUPABASE_DB_PASSWORD}@aws-0-eu-central-1.pooler.supabase.com:5432/postgres" >> $GITHUB_ENV

      - name: ☁️ Deploy Cloudflare Worker
        run: ./deploy_cf.sh
        env:
          CF_SCRIPT_NAME: index
          CF_CUSTOM_DOMAIN: mydomain.com
          CF_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }}
          CF_API_TOKEN: ${{ secrets.CF_API_TOKEN }}

      ... # other steps

I also tried running the script with a wrong path, and I get a different error mesasge:

error: Uncaught (in promise) Error: Bad scriptSpec: not a valid script name or file path or https url: ./supabase/functions/cloudflare/testworker.ts
        throw new Error(`Bad scriptSpec: not a valid script name or file path or https url: ${scriptSpec}`);

Also, I don't have any uncommitted files, and the repository is up to date

johnspurlock-skymethod commented 9 months ago

That's strange. Maybe add the --verbose flag to your denoflare push command and see if there are any clues?

The fact that it says your bundled module JS is zero bytes is a clue

putting module-based worker index... (0bytes) (20bytes compressed)

maybe run deno bundle on your module entry point locally and see what comes out. That's effectively what push does to bundle your worker.