如何将私有 git 存储库添加为另一个 git 存储库中的子模块

问题描述 投票:0回答:1

如何将私有 git 存储库添加为另一个 git 存储库中的子模块?

在本地,当我这样做时

git submodule update
它工作正常,但在 github 操作运行程序中,它失败并在结账步骤中出现错误,指出 url not found

这是我配置工作流程文件的方式。

jobs:
  update_submodules:
    runs-on: ubuntu-latest

    steps:
      # Checkout the repository to the GitHub Actions runner
      - name: Checkout
        uses: actions/checkout@v3
        with:
          submodules: true

      # - name: Clone submodule
      #   run: git submodule update --init --recursive

      - name: Pull & update submodules recursively
        run: |
          git submodule update --init --recursive
          git submodule update --recursive --remote

第二步失败。

git github-actions git-submodules private-repository
1个回答
3
投票

您不必对

submodule update init
进行编码,因为它是
action/checkout
本身的一个选项。

例如,

actions/checkout
第116期显示:

  • 生成PAT:https://github.com/settings/tokens
  • 并添加秘密:
    https://github.com/<-- username -->/<-- repo -->/settings/secrets/new
    ,给它一个独特的名称,比如
    MY_REPO_PAT
  • 配置操作如下:
   steps:
   - name: Checkout
     uses: actions/checkout@v2
     with:
       token: ${{ secrets.MY_REPO_PAT }}
       submodules: recursive

但是

在一个 GitHub 操作中使用 PAT 显然会影响其他 GitHub 操作。
例如,如果您有一个标记/版本控制步骤,通过标记它来提交到同一分支,则默认值

GITHUB_TOKEN
会阻止递归管道触发器。
在尝试此处提倡使用 PAT 下载子模块的修复后,在我的例子中,PAT 保留在提交标签的步骤中。 这会导致管道进入递归构建,反复标记和释放


或者(替代),第 287 期

我有一个不需要个人访问令牌但将对子存储库提交的引用保留在一个地方的解决方案(使用 git 子模块)

   - name: clone submodule
     uses: actions/checkout@v2
     with:
       repository: <org name>/<repo name>
       path: path
       ssh-key: ${{ secrets.SSH_KEY }}
       persist-credentials: true

   - name: checkout submodule
     run: |
       git submodule init
       git submodule update

虽然操作会检查

master
,但
git submodule
命令会检查正确的提交,这避免了必须在 github 操作中保留引用。

关于上一期(287),Matthijs Kooijman还描述了如何设置和管理 Github 应用程序

Štěpán Jákl 添加

我还意识到,您可以简单地使用签出操作来包含多个子模块部署密钥来完成此操作。
我很惊讶这里还没有提到它。

...
    steps:
      - uses: actions/checkout@v3
         with:
           ssh-key: |
             ${{ secrets.SSH_PRIVATE_KEY_SUBMODULE_1 }}
             ${{ secrets.SSH_PRIVATE_KEY_SUBMODULE_2 }}
          submodules: 'recursive'
...

请记住,密钥必须使用存储库的链接/注释来生成,例如

ssh-keygen -t ed25519 -C "[email protected]:owner/repo.git"

GH checkout 然后可以将密钥与正确的存储库连接起来。


仍在第 287 期,第 4 季度 0223:

有处理 GH App 代币的官方行动。
我的工作流程是这样结束的:

jobs:
  test-submodules:
    runs-on: ubuntu-latest
    steps:
    - name: Get token from Github App
      uses: actions/create-github-app-token@v1
      id: app_token
      with:
        app-id: ${{ secrets.APP_ID }}
        private-key: ${{ secrets.APP_PEM }}
        # owner is required, otherwise the creds will fail the checkout step
        owner: ${{ github.repository_owner }}

    - name: Checkout from GitHub
      uses: actions/checkout@v4
      with:
        submodules: true
        token: ${{ steps.app_token.outputs.token }}
    
    - name: Print .gitmodules
      run: cat .gitmodules

在 GH 应用程序端:

  • 为其提供内容许可 - 最低限度
  • 将应用程序安装在子模块存储库中以及工作流程所在的存储库中(否则签出将无法克隆主存储库)
© www.soinside.com 2019 - 2024. All rights reserved.