调用私有仓库GitHub Actions时第二步执行失败求助
同组织私有仓库复用GitHub Actions问题解决方案
问题背景
我有两个同组织下的私有仓库,分别存储API代码和基础设施(infra)代码,其中infra仓库包含可复用的GitHub Actions,希望在API仓库的工作流中调用这些Actions。已完成以下配置:
- 创建并配置了Personal Token权限
- 在仓库Settings > Actions > General中开启「Allow all actions and reusable workflows」,并设置为「Accessible from repositories owned by the user 'org'」
API仓库的Workflow配置如下:
name: Deploy to AWS ECS on: push: branches: - main jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout Repository uses: actions/checkout@v4 - name: Checkout Infra Repository uses: actions/checkout@v4 with: repository: myapp/myapp-infra token: ${{ secrets.GH_TOKEN }} path: myapp-infra ref: main - name: List Infra Actions1 run: ls -la myapp-infra/.github/actions/ #This lists all actions - name: Show Current Directory run: pwd # /home/runner/work/myapp-api/myapp-api - name: Setup Node.js uses: ./myapp-infra/.github/actions/setup-node #runs fine - name: Show Current Directory run: pwd # /home/runner/work/myapp-api/myapp-api - name: List Infra Actions2 run: ls -la myapp-infra/.github/actions/ # fails - name: Install NPM Dependencies uses: ./myapp-infra/.github/actions/npm-install #fails
问题现象
setup-node Action执行正常,但npm-install Action执行失败,报错信息:
Error: Can't find 'action.yml', 'action.yaml' or 'Dockerfile' under '/home/runner/work/myapp-api/myapp-api/myapp-infra/.github/actions/npm-install'. Did you forget to run actions/checkout before running your local action?
排查发现:执行任意一个来自infra仓库的Action后,checkout生成的myapp-infra文件夹会被移除,但当前工作目录未发生变化。从执行日志可见,setup-node Action内部调用了actions/checkout@v4,该操作默认会清理主仓库目录,导致额外checkout的myapp-infra被删除。
问题解决方案
1. 是否需要在每个Action执行后重新checkout基础设施仓库?
不需要每次重新checkout,这不是最优解(会降低工作流效率)。问题根源是主仓库的checkout操作清理了外部目录,解决这个核心问题即可避免重复checkout。
2. 是否可以将基础设施仓库checkout到特定路径以避免被移除?
完全可以,将infra仓库checkout到GitHub Runner的工作目录之外的路径(如/tmp目录),这样主仓库的checkout清理操作不会影响到它。
修改checkout步骤:
- name: Checkout Infra Repository uses: actions/checkout@v4 with: repository: myapp/myapp-infra token: ${{ secrets.GH_TOKEN }} path: /tmp/myapp-infra ref: main
调用Action时使用绝对路径:
- name: Setup Node.js uses: /tmp/myapp-infra/.github/actions/setup-node - name: Install NPM Dependencies uses: /tmp/myapp-infra/.github/actions/npm-install
另外,也可以给主仓库的checkout步骤添加clean: false参数禁止清理,但可能残留冗余文件,风险较高,不如使用外部路径可靠。
3. 除了checkout之外,还有其他复用方法吗?
有三种更优的复用方式:
- 发布为私有GitHub Marketplace Actions:将infra仓库中的Actions打包发布为私有Actions(仅限同组织访问),调用时直接使用
uses: myapp/setup-node@v1格式,无需checkout整个仓库。 - 使用Reusable Workflows:将infra中的逻辑封装为可复用工作流,在API仓库工作流中通过
uses: myapp/myapp-infra/.github/workflows/setup-node.yml@main调用,GitHub会自动拉取复用的工作流,无需手动checkout。 - Git子模块:将infra仓库设为API仓库的子模块,每次checkout API仓库时会自动同步infra代码,但需要手动维护子模块版本,适合版本稳定的场景。
内容的提问来源于stack exchange,提问作者Rohith Poyyeri
相关产品推荐
相关产品推荐

