如何 运行 使用 Mysql 在 Docker 上使用 Github 操作进行 Prisma 迁移

How to run a Prisma migration with Mysql on Docker with Github-actions

我的应用程序使用 Docker 到 运行 一个 MySQL 数据库,并使用 Prisma 连接到它。我正在编写一个 gh-action,它在每次推送后测试应用程序。我已遵循本指南 here on how to run docker on gh-actions. I am also testing this action with act

我的 .github/workflows/main.yml 看起来像这样:

name: NestJS CI/CD

on:
  push:
    branches:
      - feature/gh-actions

jobs:
  runner-job:
    runs-on: ubuntu-latest

    container: node:latest

    services:
      mysql:
        image: mysql
        ports:
          - '3306:3306'
        env:
          MYSQL_ROOT_PASSWORD: password
          MYSQL_DATABASE: db
        options: >-
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - name: Check out repository code
        uses: actions/checkout@v2

      - name: Install dependencies
        run: npm ci

      - name: Apply migrations
        run: npx prisma migrate dev
        env:
          DATABASE_URL: mysql://root:password@mysql:3306/db

      - name: Apply seed
        run: npx prisma db seed

      - name: Test the project
        run: npm run test

但我不断收到:

| Error: P1001: Can't reach database server at `mysql`:`3306`

我是不是漏掉了什么?

我可能是错的,但我认为你应该

DATABASE_URL: mysql://root:password@localhost:3306/db?

对于任何坚持同一件事的人。为了让 act 识别连接,您应该使用 localhost 而不是 mysql。同时,当您实际在 github 上使用它时,这将不起作用,您必须在这种情况下使用 mysql。真的很令人沮丧,因为您必须更改某些内容才能在本地进行测试,这在开发过程中毫无意义。也许我在表演中遗漏了一些东西