Deploying Pages with D1 Binding via Wrangler.toml and GH Action
I've got a SvelteKit application deployed to Pages+Functions using the
@sveltekit/adapter-cloudflare
adapter and the cloudflare/pages-action
GitHub Actions action. I've confirmed that invoking API endpoints runs a Function as it should, both locally (via npm run dev
) and on prod.
I'm now trying to add a D1 database. I've added the binding to my wrangler.toml
file, and it works great locally (which is awesome 🤩), but even after deploying the schema to the remote DB via the CLI, the D1 binding in the Function does not work on my branch deploy.
I suspect this is because the pages-action
doesn't pick up the wrangler.toml
, so I tried to switch to the wrangler-action
, but I haven't figured out how to get it to deploy both the pages site and the D1 binding.
The docs suggest configuring the binding via the dashboard but I'd like to keep it in my wrangler.toml
so I have one source of truth, if possible.
The diff of the PR where I've made these changes is here: https://github.com/tylermercer/log-thing/pull/4/files/155fe21a260815b0607712ab0b51d0c627982a6d
My deployment: https://433388eb.log-thing.pages.dev/
The D1 test route showing the failed binding (which causes a 500): https://433388eb.log-thing.pages.dev/app/guestbook
Deployment ID: 433388eb-4769-41d9-9dbd-56583c4a2014
Account ID: 191456bbc4d963669ab6dd59c256d84a
5 Replies
Interesting: it works when I do
npx wrangler pages deploy
manually. I don't even need to specify the output dir. Presumably it's reading that from the wrangler.toml? But why does that same command not work on GH Actions? Any help on that would be great 🤔I personally skip the action and just run
wrangler pages deploy
in my GH action. The supplied action is a wrapper around wrangler anyway and it seems unreliable from browsing this server for a while.
I can't tell if it's reading my wrangler.toml but worst case you can set the bindings in the dashboard.Ok I've wondered if I should just do that. How did you authenticate with Cloudflare from within your GH Action? I was under the impression that that was what the supplied action took care of (since it takes an API key).
Thanks for your help!
I set the following as action secrets:
I don't think it picks up the
wrangler.toml
using wrangler pages deploy
tho, as it does not support the config option. Going to ask about this in the discussion channel if you want to follow along.Thanks! I'll try that. You put those as environment variables in in
env:
?
FWIW wrangler pages deploy
has been picking up the TOML for me. (That's what I've been using in lieu of a GH Action---just deploying manually.)
Got it working! 🚀 Thank you so much for your help!
For completeness, here's how I used wrangler pages deploy
instead of the wrangler action:
And here's the whole pipeline where it's used, in case this is helpful to anyone who finds this in the future: