You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitHub Actions:如何让phing-action访问MySQL服务容器?

解决GitHub Actions中Phing动作无法访问MySQL服务的问题

问题根源

你使用的maxxer/phing-github-action是一个Docker容器化动作,它与你定义的maindb服务容器不在同一个网络环境:

  • GitHub Actions的services容器默认与job的runner虚拟机(ubuntu-20.04)共享网络,只有在runner主机内运行的命令才能通过服务名(如maindb)访问服务。
  • Docker容器化动作会启动独立的容器,默认无法解析runner网络内的服务主机名,因此出现Unknown server host 'maindb'错误。

具体解决方案

方案一:在runner主机直接运行Phing(推荐,配置最简单)

放弃Docker动作,在ubuntu runner上直接安装Phing及依赖,这样就能直接通过localhost访问映射到主机的MySQL服务:

- name: 安装Phing与依赖
  run: |
    sudo apt-get update
    sudo apt-get install -y phing php-cli php-mysql
- name: 运行Phing测试
  run: phing runtests

注意:需要确保你的Phing配置中,数据库主机设置为localhost,或者通过环境变量传递该值覆盖配置。

方案二:让Phing容器加入服务所在网络

如果必须使用自定义Phing镜像,可通过指定网络让容器与maindb处于同一网络环境。GitHub Actions会自动为job创建专用网络,可通过变量获取并指定:

- name: 用自定义镜像运行Phing测试
  run: |
    docker run --network ${{ job.container.network }} -v $PWD:/workspace -w /workspace 你的自定义Phing镜像 phing runtests

此方式下,Phing容器就能直接通过maindb主机名访问MySQL服务。

方案三:通过runner主机IP访问MySQL

在Docker容器中,host.docker.internal会解析到宿主机(即runner虚拟机),利用这一点修改Phing的数据库主机配置:

- name: 运行Phing测试
  uses: maxxer/phing-github-action@main
  with:
    targets: runtests
    properties: db.host=host.docker.internal

这样Phing执行的MySQL命令会指向host.docker.internal:3306,从而连接到映射在runner主机上的MySQL服务。

验证步骤(可选)

在Phing步骤前添加测试命令,确认runner主机能正常访问MySQL:

- name: 测试runner与MySQL的连接
  run: |
    sudo apt-get install -y mysql-client
    mysql -udbuser -pdbpass --port=3306 -hlocalhost dbname -e "SELECT 1;"

内容的提问来源于stack exchange,提问作者Maxxer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 04:01:09