cloudflare / workers-sdk

⛅️ Home to Wrangler, the CLI for Cloudflare Workers®
https://developers.cloudflare.com/workers/
Apache License 2.0
2.75k stars 734 forks source link

🐛 BUG: D1 migration doesn't work on Github Actions #3598

Closed shimahi closed 1 year ago

shimahi commented 1 year ago

THIS PROBLEM HAS BEEN RESOLVED.

Thx for✨ → https://github.com/cloudflare/workers-sdk/issues/3598#issuecomment-1742059834


Which Cloudflare product(s) does this pertain to?

D1

What version of Wrangler are you using?

3.1.2

What operating system are you using?

Linux

Describe the Bug

When I try to apply a d1 database migration using wrangler d1 migrations apply {dbName} command on GitHub Actions, the action fails with the error ELIFECYCLE Command failed with exit code 1.

# .github/workflows/migration.workflow.yml

name: Migrate D1 Database

on:
  workflow_dispatch:
  push:
      branches: main

jobs:
  release:
    runs-on: ubuntu-latest
    env:
      NO_D1_WARNING: true
    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js 18
        uses: actions/setup-node@v3
        with:
          node-version: 18
      - run: corepack enable pnpm
      - run: pnpm install --frozen-lockfile
      - run: pnpm migrate:apply
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
# package.json
{
  "name": "d1_actions",
  "version": "1.0.0",
  "main": "./dist/index.js",
  "packageManager": "pnpm@8.6.5",
  "scripts": {
    "migrate:apply": "wrangler d1 migrations apply d1-test",
    "wrangler": "wrangler"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/node": "^20.3.1",
    "better-sqlite3": "^8.4.0",
    "typescript": "^5.0.4",
    "wrangler": "^3.1.2"
  },
  "engines": {
    "pnpm": ">=8.6.5"
  }
}

↓ Logs

Run pnpm migrate:apply
  pnpm migrate:apply
  shell: /usr/bin/bash -e {0}
  env:
    NO_D1_WARNING: true
    CLOUDFLARE_API_TOKEN: ***

> d1_actions@1.0.0 migrate:apply /home/runner/work/d1_migration_test/d1_migration_test
> wrangler d1 migrations apply d1-test

 ELIFECYCLE  Command failed with exit code 1.
Error: Process completed with exit code 1.

https://github.com/studioreah/d1_migration_test You can see that all the sources are in this repository and that the migration has failed from the ACTIONS. (If you are to try, please set up your own d1 environment wrangler.toml)

Is it possible to resolve this or at least show the error content?

rodbs commented 1 year ago

I got the exact same error running D1 migrations with GithHub Actions. (I'm using Drizzle-Kit to generate the migration files)

- name: Deploying migrations to Staging
        if: ${{ github.ref == 'refs/heads/dev' }}
        run: |
          cd ./apps/remix-admin
          npx wrangler d1 migrations apply d1_turbolang --preview --experimental-backend
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
        shell: bash
bruceharrison1984 commented 1 year ago

Hm, I ran into this as well with wrangler@3.4.0, Remix, and Drizzle. Rolling wrangler back to 3.1.1 didn't fix the issue. I'm wondering if it's GH-runner related.

I can still deploy locally into CF with the same command.

o0th commented 1 year ago

Same here with wrangler 3.4.0. Using yes Y | wrangler ... got yes: standard output: Broken pipe

dschewchenko commented 1 year ago

Same with wrangler@3.5.0. Just error, without any description

▲ [WARNING] Processing wrangler.toml configuration:
--------------------
🚧 D1 is currently in open alpha and is not recommended for production data and traffic
🚧 Please report any bugs to https://github.com/cloudflare/workers-sdk/issues/new/choose
🚧 To request features, visit https://community.cloudflare.com/c/developers/d1

🚧 To give feedback, visit https://discord.gg/cloudflaredev
--------------------

    - D1 Bindings are currently in alpha to allow the API to evolve before general availability.
      Please report any issues to https://github.com/cloudflare/workers-sdk/issues/new/choose
      Note: Run this command with the environment variable NO_D1_WARNING=true to hide this message

      For example: `export NO_D1_WARNING=true && wrangler <YOUR COMMAND HERE>`
    - D1 Bindings are currently in alpha to allow the API to evolve before general availability.
      Please report any issues to https://github.com/cloudflare/workers-sdk/issues/new/choose
      Note: Run this command with the environment variable NO_D1_WARNING=true to hide this message

      For example: `export NO_D1_WARNING=true && wrangler <YOUR COMMAND HERE>`

Error: Process completed with exit code 1.
o0th commented 1 year ago

The error persists in wrangler@3.6.0.

rodbs commented 1 year ago

The error still persists in wrangler@3.7.0.

juretopolak commented 1 year ago

I would like to apply D1 migrations in the CF Pages deploy script like

npm run build && npx wrangler d1 migrations apply db-name.

First I got the error that I needed to use a token... after creating and adding a token to the env. variable, I got an 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

Can be migrations run in the CF Pages "deploy script"? If yes is this a bug mentioned here?

timanrebel commented 1 year ago

Could the reason be that we have to press Y to apply the migration, and it errors out because of this?

o0th commented 1 year ago

Could the reason be that we have to press Y to apply the migration, and it errors out because of this?

Unfortunately, no; when in CI environment, wrangler will not ask to press Y.

pirciualex commented 1 year ago

I had a similar issue and apparently, I had to add 'D1:Edit' permission to the token I was using for the app. Here is a repo for reproduction and you can see that the migration step of the deployment flow was successful. I am using wrangler@3.10.1

mrbbot commented 1 year ago

Hey! 👋 It seems like this is no longer an issue? I'm going to close this, but feel free to re-open if I'm mistaken. 👍

cptchloroplast commented 4 months ago

I am receiving this error with wrangler 3.68.0 with a token that includes D1:Edit scope

manuelbiermann commented 2 months ago

I also struggling to get this running. Token has correct permissions and trying to run: "wrangler d1 migrations apply DB-NAME --remote", which works when run locally from terminal.