我正试图通过数据库依赖关系(实际上是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
因为要将mysql -uroot -e ...
传递到容器的入口点,所以它不会启动守护进程。
将env变量MYSQL_USER
、MYSQL_PASSWORD
等传递到容器是正确的方法。
--default-authentication-plugin=mysql_native_password
作为运行参数将确保其以正确的方式创建。
我在github服务中看不到错误,但日志似乎无法解决启动问题。
在花了很多小时试图找到解决方案后,我想出了一个:
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容器的主机和端口是什么。
可能会帮助搜索相同问题的人。