Is possible to run migrations in Pages build command?

I'm not sure what is the right flow to run D1 migrations... If I understand it correctly so far, you have to: Push migrations with a command: npx wrangler d1 migrations apply db-name as a wrangler command from the local env to make changes in the prod. DB and then push the changed app code (wich rely on new DB schema) to git. Shouldn't be a migration run right after the deployment in the "build command" like: npm run build && npx wrangler d1 migrations apply db-name Is this possible? I tried the comamnd and got the error:
In a non-interactive environment, it's necessary to set a CLOUDFLARE_API_TOKEN environment variable for wrangler to work. Please go to https://developers.cloudflare.com/fundamentals/api/get-started/create-token/ for instructions on how to create an api token, and assign its value to CLOUDFLARE_API_TOKEN.
In a non-interactive environment, it's necessary to set a CLOUDFLARE_API_TOKEN environment variable for wrangler to work. Please go to https://developers.cloudflare.com/fundamentals/api/get-started/create-token/ for instructions on how to create an api token, and assign its value to CLOUDFLARE_API_TOKEN.
After creating CLOUDFLARE_API_TOKEN env var I get this error:
Failed: Error while executing user command. Exited with error code: 1
Failed: build command exited with code: 1
Failed: error occurred while running build command
Failed: Error while executing user command. Exited with error code: 1
Failed: build command exited with code: 1
Failed: error occurred while running build command
4 Replies
Lake
Lake2mo ago
hey, is there any answer to this?
Coolio85
Coolio852mo ago
The way I've tackled this issue is by using github actions to deploy and push to pages, and then run a command after which runs migrations This is my example preview deployment,yml
name: Preview Deployment

on:
push:
branches-ignore:
- "main"

jobs:
deploy-preview:
if: github.actor != 'dependabot[bot]'
runs-on: ubuntu-latest
name: Preview Deployment
steps:
- uses: actions/checkout@v4
- name: Deploy
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
preCommands: |
npm install
npx @cloudflare/next-on-pages@1
command: pages deploy --project-name=[PROJECT NAME] --commit-dirty
postCommands: |
echo "*** Applying Preview Migrations ***"
npx wrangler d1 migrations apply [PROJECT NAME] --preview --remote
echo "******"
name: Preview Deployment

on:
push:
branches-ignore:
- "main"

jobs:
deploy-preview:
if: github.actor != 'dependabot[bot]'
runs-on: ubuntu-latest
name: Preview Deployment
steps:
- uses: actions/checkout@v4
- name: Deploy
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
preCommands: |
npm install
npx @cloudflare/next-on-pages@1
command: pages deploy --project-name=[PROJECT NAME] --commit-dirty
postCommands: |
echo "*** Applying Preview Migrations ***"
npx wrangler d1 migrations apply [PROJECT NAME] --preview --remote
echo "******"
Lake
Lake2mo ago
awesome thanks
Coolio85
Coolio852mo ago
Replace the preCommands npx @cloudflare/next-on-pages@1 with npm run build or whatever :awesome: