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

GitHub Workflow中chown报错:路径正确却提示不存在的原因

问题描述

尝试通过GitHub Workflow用Capistrano部署Rails应用,Workflow能正常运行bash脚本,但执行chown命令时抛出错误:

chown: cannot access /var/www/html/xxxx/xxxx/: No such file or directory

已确认路径是正确的,想了解错误原因。对应的Workflow文件内容如下:

# This is a basic workflow to help you get started with Actions
name: Deploy with Capistrano

# Controls when the action will run. 
on:
  # Triggers the workflow on push or pull request events but only for the main branch
  push:
    branches: [main]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  deploy:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    - uses: actions/checkout@v2
    - uses: ruby/setup-ruby@v1
      with:
        # ruby-version: 3.0.1 # Not needed with a .ruby-version file
        bundler-cache: true # runs 'bundle install' and caches installed gems automatically
    - uses: miloserdow/capistrano-deploy@master
      with:
        target: production # Defines the environment that will be used for the deployment
        deploy_key: ${{ secrets.MONITOR }} # Name of the variable configured in Settings/Secrets of your github project
    - run: sudo chown -R $(user -u):$(group -g) "/var/www/html/xxxx/xxxx/"
错误原因

核心问题是chown命令的执行位置完全错误:

  • Capistrano的作用是把代码部署到你的远程生产服务器,miloserdow/capistrano-deploy这个Action是在GitHub提供的ubuntu-latest临时虚拟机(runner)上运行Capistrano命令,再通过SSH连接到你的远程服务器完成部署。
  • 你最后一步的run: sudo chown...是直接在GitHub的临时虚拟机上执行的,这台机器和你的远程生产服务器完全是两个独立的环境,根本不存在你远程服务器上的/var/www/html/xxxx/xxxx/路径,自然会提示找不到文件或目录。
  • 另外你的chown命令还有语法错误:user -u和group -g是错误写法,正确的应该是id -u和id -g,不过这不是当前报错的主要原因。
解决方向

如果需要修改远程服务器上部署目录的权限,有两种可行方式:

  • 通过Capistrano配置自动执行:在Capistrano的部署配置文件(比如deploy.rb或对应环境的配置文件)中添加权限设置任务,让Capistrano在部署过程中自动在远程服务器执行chown命令。示例配置:
    after :deploy, :set_permissions do
      on roles(:web) do
        execute :sudo, :chown, '-R', "#{fetch(:user)}:#{fetch(:group)}", release_path
      end
    end
    
  • 通过SSH远程执行命令:在miloserdow/capistrano-deploy步骤之后,使用SSH类的GitHub Action连接到你的远程服务器,再执行chown命令,而不是在GitHub的runner上直接运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 17:55:46