如何在GitHub action中检出子模块?

27

我有一个带子模块的代码仓库,当我推送到特定分支或标签时,我会创建一个构建。但问题是我的检出步骤无法访问我的子模块。设置如下:

  1. 两个代码仓库都在GitHub上。
  2. 我是这两个代码仓库的所有者。
  3. 两个代码仓库都是私有的。
  4. 仅支持ssh协议访问这两个代码仓库(禁用https)。

我已经尝试使用GitHub Action actions/checkout@v2,但没有成功。如下所示,我已经尝试使用“ssh-key”选项,其中我将公钥添加到子模块代码仓库的部署密钥中,并将私钥添加到运行该Action的代码仓库的secrets中。我收到以下错误消息:

Fetching the repository
  /usr/bin/git -c protocol.version=2 fetch --no-tags --prune --progress --no-recurse-submodules --depth=1 origin __myrepo__
  ERROR: Repository not found.
  Error: fatal: Could not read from remote repository.
  
  Please make sure you have the correct access rights
  and the repository exists.
  The process '/usr/bin/git' failed with exit code 128
  Waiting 13 seconds before trying again
  /usr/bin/git -c protocol.version=2 fetch --no-tags --prune --progress --no-recurse-submodules --depth=1 origin __myrepo__
  ERROR: Repository not found.
  Error: fatal: Could not read from remote repository.
  
  Please make sure you have the correct access rights
  and the repository exists.
  The process '/usr/bin/git' failed with exit code 128
  Waiting 19 seconds before trying again
  /usr/bin/git -c protocol.version=2 fetch --no-tags --prune --progress --no-recurse-submodules --depth=1 origin __myrepo__
  ERROR: Repository not found.
  Error: fatal: Could not read from remote repository.
  
  Please make sure you have the correct access rights
  and the repository exists.
  Error: The process '/usr/bin/git' failed with exit code 128

我已经尝试了使用ssh-key,同时在submodules选项上也尝试了truerecursive,但仍无法成功。子模块的目标位置在一个名为src的目录中。我的工作流程中检出步骤如下:

Step-01:
  runs-on: ubuntu-18.04
  steps:
    - name: Checkout repository
      uses: actions/checkout@v2
      with:
        submodules: 'true'
        ssh-key: ${{ secrets.PRIVATE_KEY_FOR_DEPLOY_KEY_IN_SUBMODULE }}

.gitmodules 文件是:

[submodule "name"]
    path = src/name
    url = git@github.com:user/repository.git
    branch = master

我对GitHub Actions(包括CI/CD)非常陌生,对子模块也不是很熟悉,所以可能会犯一些基本错误。

4个回答

34

多亏了这个,我终于把它搞定了。

为了澄清情况,强制使用SAML SSO。因此,我使用个人访问令牌(PAT)来代替使用SSH尝试访问子模块,其中SSO已经得到授权。

我的做法:

  1. 在账户设置中生成了一个PAT。
  2. 启用并授权了SSO。
  3. 将令牌添加到想要访问子模块的存储库(repository)机密中。
  4. 将工作流程更新为以下内容(只需更改ssh-key行)。

Step-01:
  runs-on: ubuntu-18.04
  steps:
    - name: Checkout repository
      uses: actions/checkout@v2
      with:
        submodules: 'true'
        token: ${{ secrets.PAT_TOKEN }}

1
你应该将个人访问令牌添加到存储库中吗?那不会成为安全问题吗? - Avilio
已添加到私有存储库,所以应该没问题了吧? - hametbacon
是的,我不建议将其用于公共代码库,因为这可能存在安全问题。请保持私钥的私密性。 - Nelumbo

2

虽然我来晚了,但由于我的私有仓库是作为子模块存在的,所以我选择忽略子模块选项,因为它需要一个具有主仓库和子模块仓库权限的PAT。在我的情况下,我发现这太麻烦了,因为我有很多需要子模块的仓库,这将导致要么为所有不同的主仓库拥有一堆不同的PAT,要么是一个过于宽泛的PAT,它将封装所有的仓库和子模块。

相反,我创建了一个只有子模块权限的PAT,并使用以下工作流语法使其在所有主仓库中运行:

    steps:
      - uses: actions/checkout@v3

      - uses: actions/checkout@v3
        with:
          repository: org/submodule-repo
          path: submodule-repo
          token: ${{ secrets.SUBMODULE-PAT }}


2
我对 GitHub Actions 还不太熟悉,但这是我喜欢的解决方案。
steps:
- uses: actions/checkout@v3
  with:
    submodules: recursive
    token: ${{ secrets.PAT_TOKEN }}

设置 -> 开发者设置 -> 个人令牌(经典)

然后 生成新令牌(经典)。

然后使用 repo 复选框,这是我用过的。你也可以尝试更少的权限。创建完成后,复制 该令牌到剪贴板。将其粘贴到安全的位置以备查看。

接着,在具有子模块的存储库的操作选项卡中,在操作秘密中添加一个 PAT_TOKEN 秘密,其值为从 PAT 复制的值。


2
这对我来说失败了,显示fatal: could not read Username for 'https://github.com': No such device or address - J. Perkins
1
我调整了适应任何地方的工作。最初我在一个自己托管的系统上工作,它确实能够正常运行,但在其他地方无法工作,但现在可以了。 - hunterino

0

已经解决了,主要归功于@Nelumbo上面的答案

如果您使用新的细粒度个人访问令牌(PAT),请注意以下几点:

  1. 确保添加权限以访问父存储库,而不仅仅是子模块存储库,因为当token参数传递给checkout步骤时,此令牌将用于检出所有内容,而不仅仅是子模块。

  2. 该令牌最长有效期为一年,因此请准备好问题会再次出现,设置提醒并保持冷静。

  3. 该令牌只需要对内容和元数据(来自存储库权限)具有只读权限,而不需要任何帐户权限。


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