使用多个私有子模块的GitHub Actions

4

我正在尝试创建一个GH Actions作业,该作业将从私有存储库下载两个子模块。我希望它们能够使用我已经生成的SSH密钥进行下载。

我一直在尝试这样做:

      - uses: actions/checkout@v2
        with:
          submodules: repo_1
          ssh-key: ${{ secrets.REPO_1 }}

      - uses: actions/checkout@v2
        with:
          submodules: repo_2
          ssh-key: ${{ secrets.REPO_2 }}

这段代码将创建名称为repo_1repo_2的文件夹,但是它们将为空。我目前还没有找到解决方法。有人知道如何使用不同的SSH密钥下载多个私有子模块吗?
3个回答

1
发现一个解决方法:
steps:
      - name: Switch to HTTPS git
        run: |
          rm -f ~/.gitconfig
          git config --local --unset url."git@github.com".insteadOf https://github.com || echo "OK"
          git config --local --unset url."git://".insteadOf https:// || echo "OK"

      - uses: actions/checkout@v2

      - name: Switch to SSH git
        run: |
          git config --local --replace-all url."git@github.com".insteadOf https://github.com
          git config --local --add url."git://".insteadOf https://

      - name: Checkout submodules
        env:
          GIT_SSH_COMMAND: "ssh -o StrictHostKeyChecking=no"
        run: |
          eval `ssh-agent -s`

          echo "${{secrets.REPO_1}}" | ssh-add -
          git submodule update --init repo_1
          ssh-add -D


          echo "${{secrets.REPO_2}}" | ssh-add -
          git submodule update --init repo_2
          ssh-add -D

          eval `ssh-agent -k`

1
文档提到:

# Whether to checkout submodules: `true` to checkout submodules or `recursive` to
    # recursively checkout submodules.
    #
    # When the `ssh-key` input is not provided, SSH URLs beginning with
    # `git@github.com:` are converted to HTTPS.
    #
    # Default: false
    submodules: ''

因此,submodules: repo_2 不应该是正确的。

例如,这是一个具有子模块递归检出的工作流程(在现有存储库引用内)。

      # Submodules recursive
      - name: Checkout submodules recursive
        uses: ./
        with:
          ref: test-data/v2/submodule-ssh-url
          path: submodules-recursive
          submodules: recursive
      - name: Verify submodules recursive
        run: __test__/verify-submodules-recursive.sh

它将检出存储库 github.com/actions/checkout 分支 test-data/v2%2Fsubmodule-ssh-url,其中包括一个带有子模块名称和SSH URL的 .gitmodules
回答您最初的问题:
  • 使用以下内容更改您的 .gitmodules URL:
repo1:org1/repo1
repo2:org2/repo2
  • 将GIT_SSH_COMMAND环境变量添加到ssh -F config中,其中config是一个文件,其内容为:
Host repo2
  Hostname github.com
  User git
  IdentityFile key2

Host repo2
  Hostname github.com
  User git
  IdentityFile key2

我不知道是否可以引用使用正确的secrets.REPO_x生成的那个文件,但是从检出操作中可以看到,您没有本地方式来为多个子模块存储库指定多个密钥。

它并没有真正回答问题。除了错误使用 submodules: 之外,您没有提供如何从私有存储库下载多个子模块的答案。 - This-is-patriiick
@This-is-patriiick,我已经编辑了答案以回答你的问题。 - VonC
这样做会出现错误: 错误:致命错误:未找到存储库'https://github.com/org/second-repo/' - This-is-patriiick
@This-is-patriiick 我猜你已经将 org 和 second repo 替换为正确的值了吧?文档指定如果未提供 ssh-key,则 URL 会更改为 HTTPS。ssh-key 是否已经正确声明和赋值了呢? - VonC
这是正确的。将orgsecond-repo替换为正确的值。.gitmodules中子模块的URL应写为git@github.com:org/second-repo.git - This-is-patriiick
@This-is-patriiick 我没有看到使用两个不同密钥进行子模块递归克隆的一次检出的解决方案。我已经编辑了答案,建议探索可能的选项。 - VonC

1
      - uses: actions/checkout@v3
        with:
          token: ${{ secrets.GH_PAT }}
          submodules: recursive

您需要创建一个名为GH_PAT加密秘钥来存储个人访问令牌。该令牌需要至少具有对主存储库及其所有子模块中的"内容"类别的只读访问权限。

我使用了检出操作的v3版本进行了测试,但我认为它也适用于v2版本。

相关文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接