简体   繁体   English

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

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

For a project I'm trying to use Node.js unit tests in GitHub actions.对于一个项目,我尝试在 GitHub 操作中使用 Node.js 单元测试。 But I'm having problems connecting the Node.js with my database running in docker (via GitHub).但是我在将 Node.js 与我在 docker 中运行的数据库(通过 GitHub)连接时遇到了问题。

This is my workflow:这是我的工作流程:

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

When I run the workflow this is the error:当我运行工作流时,这是错误:

Github 操作

So my question is how to connect Node.js to the MySQL database?所以我的问题是如何将 Node.js 连接到 MySQL 数据库? Is the IP address of the contrainer 127.0.0.1 (localhost)?容器的 IP 地址是 127.0.0.1 (localhost) 吗?

You need to wait for the Docker container to be fully started up, so you need to add a sleep for a sufficient amount of time.您需要等待 Docker 容器完全启动,因此您需要添加足够时间的睡眠。 I found that 2 minutes is more than enough for small projects.我发现 2 分钟对于小型项目来说已经足够了。 Try to run the following:尝试运行以下命令:

    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