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

hb0*_*hb0 5 github git-submodules github-actions

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

Git 存储库设置:

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

目标:

  • 我想运行一个gradle buildrepository A > Github Actions

Github 操作工作流

name: Test
on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1
Run Code Online (Sandbox Code Playgroud)

问题:

  • actions/checkout@v1步骤访问子模块失败

.gitmodules

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

Run Code Online (Sandbox Code Playgroud)

Github 操作步骤Build with 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
Run Code Online (Sandbox Code Playgroud)

我试过的:


  1. 添加with: submodules: trueactions/checkout@v1
    - uses: actions/checkout@v1
      with: 
        submodules: true
Run Code Online (Sandbox Code Playgroud)

Github 操作步骤Run 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.
Run Code Online (Sandbox Code Playgroud)
  1. 使用第 3 方 Github 操作,例如 textbook/git-checkout-submodule-action@2.0.0

运行教科书/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
Run Code Online (Sandbox Code Playgroud)
  1. 个人访问令牌添加到actions/checkout

    • 令牌是从可以访问该存储库的 github 用户生成的
    • repoA 归一个组织所有
    • 令牌具有完全repo权限
    - uses: actions/checkout@v1
      with: 
        submodules: true
        token: ${{ secrets.GITHUB_REPO_TOKEN }}
Run Code Online (Sandbox Code Playgroud)

运行操作/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.
Run Code Online (Sandbox Code Playgroud)

即使用可以访问 repoA 和 repoB 的令牌,我什至无法检出父 repoA。

小智 10

现在您可以使用 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

Run Code Online (Sandbox Code Playgroud)


Hir*_*uri 6

您可以使用actions/checkout@v2而不需要额外的脚本。我在 Mac、Linux 和 Windows 上使用过这个。同时 v3 可用:

- uses: actions/checkout@v3
  with:
    submodules: true
Run Code Online (Sandbox Code Playgroud)

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

- uses: actions/checkout@v3
  with:
    submodules: recursive
Run Code Online (Sandbox Code Playgroud)


hb0*_*hb0 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 
Run Code Online (Sandbox Code Playgroud)