如何在“Github Actions”中克隆公共子模块

12

这个问题被设计为问答式,原因是我花了几个小时才让它运行,因为我有一些拼写错误并认为解决方案更复杂。如果我在谷歌或Stackoverflow上找到像这样的教程,我会检查拼写错误。

Git仓库设置:

  • 私有仓库A-名称:repoA
  • 子模块B(公共仓库)-名称:repoB

目标:

  • 我想在repository A > Github Actions中运行gradle build

Github Action工作流程

name: Test
on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1

问题:

  • actions/checkout@v1 步骤无法访问子模块。

.gitmodules

[submodule "library"]
    path = library
    url = git@github.com:organization/repoB.git

Github Actions步骤使用Gradle构建出错

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':repoA:compileReleaseAidl'.
> Could not resolve all task dependencies for configuration ':repoA:releaseCompileClasspath'.
   > Could not resolve project :repoBSubmodule1.
     Required by:
         project :repoA

我尝试的方法:


  1. with: submodules: true添加到actions/checkout@v1中。
    - uses: actions/checkout@v1
      with: 
        submodules: true

Github Actions步骤运行actions/checkout@v1错误

(...)
git submodule sync
git -c http.https://github.com.extraheader="AUTHORIZATION: basic ***" submodule update --init --force
Submodule 'repoB' (git@github.com:organization/repoB.git) registered for path 'repoB'
Cloning into '/home/runner/work/repoA/repoA/repoB'...
Host key verification failed.
##[error]fatal: Could not read from remote repository.

  1. 使用第三方Github Actions,例如textbook/git-checkout-submodule-action@2.0.0

运行 textbook/git-checkout-submodule-action@2.00 错误:

fatal: Not a git repository (or any parent up to mount point /github/workspace)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
##[error]Docker run failed with exit code 128

  1. actions/checkout 中添加一个个人访问令牌

    • 该令牌由具有对该存储库访问权限的Github用户生成
    • repoA是由一家组织拥有
    • 该令牌具有完整的repo权限
    - uses: actions/checkout@v1
      with: 
        submodules: true
        token: ${{ secrets.GITHUB_REPO_TOKEN }}

运行 actions/checkout@v1 时出现错误:

git submodule sync
git -c http.https://github.com.extraheader="AUTHORIZATION: basic ***" submodule update --init --force
Submodule 'repoB' (git@github.com:organization/repoB.git) registered for path 'repoB'
Cloning into '/home/runner/work/repoA/repoA/repoB'...
Host key verification failed.
##[error]fatal: Could not read from remote repository.

也就是说,使用那个能够访问repoA和repoB的令牌,我甚至无法检出父级repoA。

4个回答

14

现在您可以使用actions/checkout@v2

https://github.com/actions/checkout#checkout-submodules所说:

- uses: actions/checkout@v2 # checkout root
- name: Checkout submodules # checkout rest
  shell: bash
  run: |
    # If your submodules are configured to use SSH instead of HTTPS please uncomment the following line
    # git config --global url."https://github.com/".insteadOf "git@github.com:"
    auth_header="$(git config --local --get http.https://github.com/.extraheader)"
    git submodule sync --recursive
    git -c "http.extraheader=$auth_header" -c protocol.version=2 submodule update --init --force --recursive --depth=1


1
这里的顶部两行都应该以 - 开头吗?我在尝试在Windows上使其工作。 - Macke
1
@Macke 抱歉,我的错误。这是两个步骤,每个步骤都需要一个“-”。 - riQQ
该操作可以执行子模块而无需额外的脚本。 - Queeg

6

您可以使用actions/checkout@v2而无需额外脚本。我在Mac、Linux和Windows上都使用过这个。同时,v3也已经推出:

- uses: actions/checkout@v3
  with:
    submodules: true

对于递归子模块(其中一个子模块需要另一个子模块),请使用

- uses: actions/checkout@v3
  with:
    submodules: recursive

3

将子模块的URL从SSH格式更改为HTTPS格式解决了这个问题:

.gitmodules

[submodule "repoB"]
    path = repoB
#Before:
    url = git@github.com:organization/repoB.git
# After:
    url = https://github.com/organization/repoB.git 

0

对于基于 Windows 的运行程序,以下步骤是有效的: 创建一个机器人用户,生成访问令牌,将该用户纳入私有(子模块)仓库,并在 actions.yml 文件中添加以下内容:

  - name: Checkout submodules using a PAT
    shell: bash
    run: |
      git config --file .gitmodules --get-regexp url | while read url; do
      git config --file=.gitmodules $(echo "$url" | sed -E "s/git@github.com:|https:\/\/github.com\//https:\/\/${{ secrets.GHACTIONS_PAT }}:${{ secrets.GHACTIONS_PAT }}@github.com\//")
      done
      git submodule sync
      git submodule update --init --recursive

你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community
这个代码块让我遇到了认证错误,例如: fatal: Cannot prompt because user interactivity has been disabled. bash: /dev/tty: No such device or address error: failed to execute prompt script (exit code 1) fatal: could not read Username for 'https://github.com': No such file or directory - Queeg
@Hiran Chaudhuri 不确定为什么你会遇到这个问题,你是否像我一样在存储库机密中提供了个人访问令牌(PAT),例如GHACTIONS_PAT? - va_vk_va
我使用 actions/checkout@v2 并启用子模块来解决了这个问题。 - Queeg

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