繁体   English   中英

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

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

对于一个项目,我尝试在 GitHub 操作中使用 Node.js 单元测试。 但是我在将 Node.js 与我在 docker 中运行的数据库(通过 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

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

Github 操作

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

您需要等待 Docker 容器完全启动,因此您需要添加足够时间的睡眠。 我发现 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM