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

Github Action中working-directory配置不生效问题排查

问题原因与解决方法

核心问题1:defaults.run.working-directory作用范围有限

这个配置仅对用run:直接定义的步骤生效(比如你的yarn、yarn build步骤),但actions/checkout@v3、actions/setup-node@v3这类第三方Action不受这个全局默认配置约束,它们会默认使用GitHub Runner的工作目录(也就是你看到的_work下的目录)。

核心问题2:路径写法错误

你写的/../../../../var/www/mysite.com是从根目录向上跳转的无效路径,正确的绝对路径应该直接写/var/www/mysite.com。

修正后的完整配置

# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

defaults:
  run:
    working-directory: /var/www/mysite.com

jobs:
  deployment:
    runs-on: self-hosted

    steps:
    - name: Checkout code to target directory
      uses: actions/checkout@v3
      with:
        path: /var/www/mysite.com

    - name: Use Node.js
      uses: actions/setup-node@v3
      with:
        node-version: '14.x'

    - name: Install dependencies
      run: yarn
    - name: Build
      run: yarn build
    - name: Restart server application
      run: pm2 restart pm2_script.json

额外注意事项

  • 确保GitHub Runner的运行用户对/var/www/mysite.com目录有读写权限,否则拉取代码、安装依赖等操作会报错。
  • 如果目标目录已存在旧代码,actions/checkout默认会覆盖旧内容;若需保留旧文件,可添加fetch-depth: 0等参数调整拉取策略。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 19:15:49