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

如何在Github Actions中运行Web应用并完成Cypress自动化测试

问题修复方案

核心问题原因

你遇到的ECONNREFUSED错误与GitHub端口封禁无关,是Docker容器未做端口映射导致:你启动容器时没有将容器内监听的8080端口暴露给GitHub Actions的宿主机运行环境,跑在宿主机上的Cypress自然无法访问到容器内的Web服务。

具体修复步骤

  • 第一步:确认项目Dockerfile中已添加端口暴露配置,无则补充:
# 放在Dockerfile合适位置即可
EXPOSE 8080
  • 第二步:修改工作流中的docker run命令,添加端口映射参数:
    将原命令
    docker run dashboard:${{ github.run_id }}-test &
    修改为
    docker run -p 8080:8080 dashboard:${{ github.run_id }}-test &
    -p 8080:8080参数的作用是将宿主机的8080端口映射到容器内部的8080端口,打通两者的网络访问链路。

可选优化(建议添加)

固定sleep 60的方式稳定性差、耗时高,可替换为服务可用性检测,避免服务未完全启动就执行测试:

# 替换原sleep 60步骤
- run: npm install -g wait-on
- run: wait-on http://127.0.0.1:8080

修复后完整工作流参考

name: Build and test
# event listener to trigger the workflow
on:
  push:
    branches:
      - 'workflows-dev'
      - 'master'

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2 # Checkout into the repo
      - run: sed -i -e "s/http\:\/\/<STRAPI_URL>/${{ secrets.PREPROD_URL }}/" Dockerfile
      - run: docker build -t dashboard:${{ github.run_id }}-test . # Build docker image with a unique tag
      - run: docker run -p 8080:8080 dashboard:${{ github.run_id }}-test & # 新增端口映射
      - run: npm install -g wait-on
      - run: wait-on http://127.0.0.1:8080 # 等待服务就绪,替代sleep 60
      - run: |
            echo {\"site\": \"http://127.0.0.1:8080\", \"identifier\": \"${{ secrets.PREPROD_USER }}\", \"password\": \"${{ secrets.PREPROD_PASSWORD }}\"} > tests/cypress.env.json # Setup ENV variables for the tests
      - run: npm test

问题排查小技巧

如果修改后仍有异常,可在启动容器后新增诊断步骤快速定位问题:

- run: docker ps # 查看容器是否正常运行
- run: curl -v http://127.0.0.1:8080 # 测试宿主机是否能正常访问容器服务

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 07:36:03