docker-compose MySQL容器,可使用GithubActions进行自动测试

时间:2020-07-18 12:58:47

标签: mysql docker docker-compose continuous-integration github-actions

我有GitHub存储库,我想使用GithubActions在每个请求请求时自动执行单元测试。

我已经设置了工作流程文件:

   name: CI

on:
  pull_request:
    branches: [ master ]

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
      - name: Install
        run: npm ci
      - name: Linter
        run: npm run lint
      - name: Build
        run: npm run build
      - name: Docker
        run: docker-compose up -d
      - name: Wait / Sleep
        uses: jakejarvis/wait-action@v0.1.0
        with:
          time: '10s'
      - run: | 
          docker ps
          cat ./dumps/backup.sql | docker exec -i mysql-development_1 /usr/bin/mysql -u root --password=password        
      - name: Test
        run: npm test

由于我需要首先插入表,因此我想插入一个转储,该转储在我的计算机上可以使用此处使用的完全相同的命令来工作。

但是,该操作失败并显示以下错误:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
read unix @->/var/run/docker.sock: read: connection reset by peer
cat: write error: Broken pipe
##[error]Process completed with exit code 1.

如何在GithubActions中访问数据库?

docker-compose.yml:

version: '3'

services:

  mysql-development:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: test_db
    ports:
      - "3308:3306"

1 个答案:

答案 0 :(得分:1)

对于可能遇到相同问题的任何人:

https://github.blog/changelog/2020-02-21-github-actions-breaking-change-ubuntu-virtual-environments-will-no-longer-start-the-mysql-service-automatically/

您只需要手动启动mysql-service,也许要等上几秒钟