docker-compose MySQL 容器,用于使用 Github Actions 进行自动化测试

docker-compose MySQL container for automated testing with GithubActions

我有 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"

对于可能 运行 遇到同样问题的任何人:

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

您只需要手动启动 mysql-服务,也许等待几秒钟