0

我试图弄清楚使用数据库依赖项(实际上是 MySQL)的 Github 工作流程发生了什么,但我找不到任何解释或解决方案来解决我的情况。

这是我的工作流程 yaml 文件:

name: docker

on:
    push:
        # publish image as master=dev or on new tag
        # except on document and ci changes
        branches:
            - main
        tags:
            - '*'
        paths-ignore:
            - '**.md'
            - '.github/workflows/*yml'

    # always run tests on merge
    # except on document and ci changes
    pull_request:
        paths-ignore:
            - '**.md'
            - '.github/workflows/*yml'

jobs:
    unit_test:
        runs-on: ubuntu-latest
        if: github.event_name == 'pull_request'
        services:
            mysql:
                image: mysql:8
                ports:
                    - 3306
                env:
                    MYSQL_USER: phalcon
                    MYSQL_PASSWORD: secret
                    MYSQL_DATABASE: shop_products_test
                    MYSQL_ROOT_PASSWORD: root
                options: --health-cmd="mysqladmin ping" --health-interval=5s --health-timeout=2s --health-retries=3
        steps:
            - name: Create docker network
              run: docker network create marketplace-network
            - name: Check out Site Repository 
              uses: actions/checkout@v2
            - name: Create .env file
              run: cp .env.example .env
            - name: Replace environment variables
              run: |
                  sed -i 's/MYSQL_HOST.*/MYSQL_HOST=0.0.0.0/g' .env
                  sed -i 's/MYSQL_PORT.*/MYSQL_PORT=${{ job.services.mysql.ports[3306] }}/g' .env
            - name: Build docker image
              run: docker build -t marketplace_shop_products .
            - name: Running unit test
              run: docker-compose up products-unit-test

此工作流的目的是运行单元测试。但是在运行单元测试之前,需要先执行一些迁移来创建测试表。我正在使用 Phalcon 框架,但我认为没关系。实际上发生了什么,我一直收到“连接被拒绝”,而我确定 MySQL 容器已启动并准备好使用并且 MySQL 容器的 IP 地址是正确的,但不知何故,它不可用或我的下一个无法访问容器“产品单元测试”。

我要做的是在执行单元测试之前在容器内本地安装 MySQL 服务器,但我认为这不是最佳实践。我需要一个单独的 MySQL 容器,单元测试容器连接到它以运行迁移并进行测试。

products-unit-test_1  | Copying php extensions to container ...
products-unit-test_1  | Run migrations ...
products-unit-test_1  | 
products-unit-test_1  | Phalcon DevTools (3.4.11)
products-unit-test_1  | 
products-unit-test_1  | Running migrations:
products-unit-test_1  | ERROR: SQLSTATE[HY000] [2002] Connection refused
products-unit-test_1  | 
products-unit-test_1  | PHPUnit 7.5.20 by Sebastian Bergmann and contributors.
products-unit-test_1  | 
products-unit-test_1  | Runtime:       PHP 7.3.28 with Xdebug 2.9.1
products-unit-test_1  | Configuration: /src/tests/phpunit.xml
products-unit-test_1  | 
products-unit-test_1  | ....                                                                4 / 4 (100%)
products-unit-test_1  | 
products-unit-test_1  | Time: 230 ms, Memory: 4.00 MB
products-unit-test_1  | 
products-unit-test_1  | OK (4 tests, 4 assertions)
shop_products_products-unit-test_1 exited with code 0
4

2 回答 2

0

在花了很多时间试图找到解决方案之后,我想出了这个:

jobs:
    unit_test:
        runs-on: ubuntu-latest
        if: github.event_name == 'pull_request'
        services:
            mysql:
                image: mysql:8
                ports:
                    - 3306
                options: --health-cmd="mysqladmin ping"
                         --health-interval=5s
                         --health-timeout=2s
                         --health-retries=3
        steps:
            - name: Get MySQL service ID
              id: mysql-service
              run: echo "::set-output name=container-id::$(docker ps | grep -i mysql | awk '{print $1}')"
            - name: Get Github network gateway address
              id: github-network
              run: echo "::set-output name=gateway-address::$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.Gateway}}{{end}}' ${{ steps.mysql-service.outputs.container-id }})"
            - name: Check out Site Repository 
              uses: actions/checkout@v2
            - name: Create .env file
              run: cp .env.example .env
            - name: Replace environment variables
              run: |
                  sed -i 's/MYSQL_HOST.*/MYSQL_HOST=${{ steps.github-network.outputs.gateway-address }}/g' .env
                  sed -i 's/MYSQL_PORT.*/MYSQL_PORT=${{ job.services.mysql.ports[3306] }}/g' .env
            ...

首先,我获取 MySQL 服务 ID,然后获取 Github 所创建网络的网关地址。这样,我确定 MySQL 容器的主机和端口是什么。

可以帮助搜索相同问题的人。

于 2021-05-08T21:03:47.410 回答
0

因为您正在传递mysql -uroot -e ...到容器的入口点,所以它没有启动守护程序。

将环境变量等传递MYSQL_USERMYSQL_PASSWORD容器是正确的方法。

--default-authentication-plugin=mysql_native_password作为一个运行参数将确保它以正确的方式创建。

我在 github 服务中看不到错误,但是日志似乎并没有影响启动。

于 2021-05-07T22:51:19.723 回答