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

使用github-push-action时文件推送路径不符的问题排查

问题:GitHub Action推送文件至错误目录

问题描述

尝试将文件和文件夹提交至主项目的子文件夹,但发起Pull Request后,所有文件都被推送到根目录,而非预期的/docs/build/html路径。首次使用GitHub Actions,已在仓库提交Issue#199,需确认操作是否有误。

涉及的工作流脚本

- name: Init new repo in dist folder and commit generated files
        run: |
          cd docs/build/html/
          git init
          touch .nojekyll
          git add -A
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git commit -m 'deploy'

      - name: Config global safe directory
        run: |
          git config --global --add safe.directory /github/workspace/docs/build/html

      - name: Force push to destination branch
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: rtd-pages
          force: true
          directory: './docs/build/html'

问题原因及修正方案

原因

你在docs/build/html/目录下初始化了一个全新的Git仓库,推送时这个新仓库的根目录对应目标分支的根目录,所以文件会被放到rtd-pages分支的根路径,而非主项目的子文件夹。

修正方案

方案1:在主仓库上下文提交子目录文件

取消初始化新仓库的步骤,直接在主仓库中操作目标子目录的文件,确保提交的路径对应主项目的子文件夹:

- name: Add and commit generated docs
        run: |
          cd /github/workspace
          # 确保.nojekyll文件存在
          touch docs/build/html/.nojekyll
          # 添加目标子目录下的所有文件
          git add docs/build/html/**
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git commit -m 'deploy generated docs'

      - name: Push to target branch
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: rtd-pages
          force: true

方案2:使用git subtree推送子目录(适合单目录部署场景)

如果需要将rtd-pages分支的内容完全对应docs/build/html/的内容,可以用git subtree命令,直接将子目录推送到目标分支:

- name: Push docs via subtree
        run: |
          cd /github/workspace
          touch docs/build/html/.nojekyll
          git add docs/build/html/.nojekyll
          git commit -m 'add .nojekyll'
          # 将子目录推送到目标分支
          git subtree push --prefix docs/build/html origin rtd-pages

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 15:07:24