Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clone public submodule in "Github Actions"

Reason for this Q&A-Style question: It took me a few hours to get this to run because I had some typos and thought the solution is more complicated. If I would have found a tutorial like this on google or Stackoverflow I would have checked for typos.

Git Repository Setup:

  • Private repository A - name: repoA
  • with submodule B (public repository) - name: repoB

Goal:

  • I want to run a gradle build in repository A > Github Actions

Github Action Workflow

name: Test
on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1

Problem:

  • The actions/checkout@v1 step fails to access the submodule

.gitmodules

[submodule "library"]
    path = library
    url = [email protected]:organization/repoB.git

Github Actions Step Build with Gradle error

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

What I tried:


  1. Add with: submodules: true to actions/checkout@v1
    - uses: actions/checkout@v1
      with: 
        submodules: true

Github Actions Step Run actions/checkout@v1 error

(...)
git submodule sync
git -c http.https://github.com.extraheader="AUTHORIZATION: basic ***" submodule update --init --force
Submodule 'repoB' ([email protected]: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. Use 3rd party Github Actions like textbook/[email protected]

Run textbook/[email protected] error:

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. Add a personal access token to the actions/checkout

    • The token was generated from a github user which has access to that repository
    • The repoA is owned by an organization
    • the token has full repo permissions
    - uses: actions/checkout@v1
      with: 
        submodules: true
        token: ${{ secrets.GITHUB_REPO_TOKEN }}

Run actions/checkout@v1 error:

git submodule sync
git -c http.https://github.com.extraheader="AUTHORIZATION: basic ***" submodule update --init --force
Submodule 'repoB' ([email protected]: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.

I.e. with the that token which has access to both, repoA and repoB I was not even able to checkout the parent repoA.

like image 391
hb0 Avatar asked Dec 10 '19 16:12

hb0


People also ask

How do I clone a project with submodules?

Cloning a Project with SubmodulesIf you pass --recurse-submodules to the git clone command, it will automatically initialize and update each submodule in the repository, including nested submodules if any of the submodules in the repository have submodules themselves.

How do I pull a submodule in GitHub?

Pulling with submodules. Once you have set up the submodules you can update the repository with fetch/pull like you would normally do. To pull everything including the submodules, use the --recurse-submodules and the --remote parameter in the git pull command .


3 Answers

Now you can use actions/checkout@v2

As https://github.com/actions/checkout#checkout-submodules said:

- 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 "[email protected]:"
    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

like image 187
Bryan Avatar answered Oct 11 '22 17:10

Bryan


You can use actions/checkout@v2 without additional scripting. I used this on Mac, Linux and Windows. Meanwhile v3 is available:

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

For recursive submodules (where a submodule requires another submodule), use

- uses: actions/checkout@v3
  with:
    submodules: recursive
like image 27
Hiran Chaudhuri Avatar answered Oct 11 '22 18:10

Hiran Chaudhuri


Changing the submodule URL from the SSH to the HTTPS format fixed it:

.gitmodules

[submodule "repoB"]
    path = repoB
#Before:
    url = [email protected]:organization/repoB.git
# After:
    url = https://github.com/organization/repoB.git 
like image 1
hb0 Avatar answered Oct 11 '22 16:10

hb0