簡體   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