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

使用GitHub Action部署React应用到AWS EC2出现10分钟超时如何解决

问题原因

  1. SSH Action自带超时限制未配置:你使用的appleboy/ssh-action默认命令执行超时时间为10分钟,你在Job层面配置的timeout-minutes: 30是整个Job的全局超时限制,Step层面的timeout: 40属于参数写错,GitHub Action Step的超时参数应为timeout-minutes,且SSH Action自身的超时配置优先级更高,因此命令执行到10分钟就被强制中断。
  2. 参数配置无效导致构建效率极低:你将node-version、cache这两个actions/setup-node专属的参数写到了SSH Action的入参中,SSH Action完全不识别这两个参数,等于你没有用到GitHub Runner的Node环境和npm缓存,所有依赖安装、React构建操作全部在EC2上执行,低配置EC2执行这类操作的速度非常慢,很容易超过默认的10分钟超时。
  3. 执行过程潜在卡住风险:如果你的EC2配置较低(比如1核1G的免费 tier 实例),React构建过程很容易出现内存不足OOM卡住的情况,也会触发超时;如果EC2的网络访问npm官方源速度慢,也会拉长npm install的执行时间。

解决方案

方案1:修复现有EC2构建的配置(快速修复)

直接修正超时参数,并优化EC2上的执行命令:

name: Node.js CI

on:
  push:
    branches: [ staging ]
  pull_request:
    branches: [ staging ]

jobs:
  build:
    runs-on: ubuntu-latest
    timeout-minutes: 30
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Deployment
        timeout-minutes: 25 # 修正Step超时参数写法
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SECRET_LINK }}
          key: ${{ secrets.SECRET_KEY }}
          username: ${{ secrets.SECRET_NAME }}
          command_timeout: 25m # 新增SSH Action专属超时配置
          script: |
            cd /var/www/html/
            git checkout staging
            git pull
            # 优化依赖安装和构建命令
            npm config set registry https://registry.npmmirror.com
            npm ci
            NODE_OPTIONS=--max-old-space-size=1024 npm run build

方案2:优化构建流程(更推荐)

把依赖安装、构建环节放到性能更强的GitHub Runner上执行,只把最终构建产物传到EC2,既可以大幅降低执行时间,也不会占用EC2的资源:

name: Node.js CI

on:
  push:
    branches: [ staging ]
  pull_request:
    branches: [ staging ]

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 18.x # 建议用更高的LTS版本
          cache: 'npm'
      # 在Runner上安装依赖、构建
      - name: Build
        run: |
          npm ci
          npm run build
      # 把构建产物传到EC2
      - name: Deploy to EC2
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.SECRET_LINK }}
          key: ${{ secrets.SECRET_KEY }}
          username: ${{ secrets.SECRET_NAME }}
          source: "build/*"
          target: "/var/www/html/"
          strip_components: 1 # 去掉build前缀,直接把文件放到目标目录

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 00:15:03