无法在 GitHub 操作中将 Node.js 与 Docker MySQL 数据库连接

Can't connect Node.js with Docker MySQL database in GitHub actions

对于一个项目,我正在尝试在 GitHub 操作中使用 Node.js 单元测试。 但是我在 docker 中将 Node.js 与我的数据库 运行ning 连接时遇到问题(通过 GitHub)。

这是我的工作流程:

name: Node.js CI
on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
jobs:
  build:
    runs-on: ubuntu-latest
     # Service containers to run with `runner-job`
    services:
      # Label used to access the service container
      biddify-product-database:
        # Docker Hub image
        image: robfontys/biddify-product-database:latest
        #
        ports:
          # Opens tcp port 6379 on the host and service container
          - 3306:3306
    timeout-minutes: 1      
    strategy:
      matrix:
        node-version: [12.x, 14.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    - run: npm ci
    - run: npm run build --if-present
    - run: npm test

当我 运行 工作流程时,这是错误:

所以我的问题是如何将 Node.js 连接到 MySQL 数据库? contrainer的IP地址是127.0.0.1(localhost)吗?

需要等待Docker容器完全启动,所以需要添加足够的sleep时间。我发现 2 分钟对于小型项目来说绰绰有余。尝试 运行 以下内容:

    name: Node.js CI
    on:
      push:
        branches: [ master ]
      pull_request:
        branches: [ master ]
    jobs:
      build:
        runs-on: ubuntu-latest
         uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    - run: npm install
    - run: npm run build --if-present
    - run: docker-compose up -d biddify-product-database
    - name: Sleep for 120 seconds
      uses: jakejarvis/wait-action@master
      with:
        time: '120s'
    - run: npm test