How to git clone a private repo with a private submodule in a GitHub Action?

Viewed 486

I have two private GitHub repositories within my organisation (or my user). One of them contains the other as a submodule. How can I clone this other private repository with its containing submodule?

I tried

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

However this failed with an error message on the submodule part, is there some permission I should add or other?

Fetching submodules
  /usr/bin/git submodule sync
  /usr/bin/git -c protocol.version=2 submodule update --init --force --depth=1
  Submodule '.github/workflows/MYPROJECT1' (https://github.com/MYUSER/MYPROJECT1.git) registered for path '.github/workflows/MYPROJECT1'
  Cloning into '/home/runner/work/MYPROJECT2/MYPROJECT2/.github/workflows/MYPROJECT1'...
  remote: Repository not found.
  Error: fatal: repository 'https://github.com/MYUSER/MYPROJECT1.git/' not found
  Error: fatal: clone of 'https://github.com/MYUSER/MYPROJECT1.git' into submodule path '/home/runner/work/MYPROJECT2/MYPROJECT2/.github/workflows/MYPROJECT1' failed
  Failed to clone '.github/workflows/MYPROJECT1'. Retry scheduled
  ... more errors
1 Answers

You can try and use an SSH URL instead.

The issue 116 "private submodule checkout fails" now (July 2022) illustrates that as an alternative:

This solution works when you want to keep flexibility of URL repos and still use GitHub Actions with Deploy Keys to access private submodules:

  - name: Checkout
    uses: actions/checkout@v3

  - name: Clone Submodule
    run: |
        mkdir -p $HOME/.ssh
        echo '${{ secrets.SUBMODULE_REPO_DEPLOY_KEY }}' > $HOME/.ssh/ssh.key
        chmod 600 $HOME/.ssh/ssh.key
        export GIT_SSH_COMMAND="ssh -i $HOME/.ssh/ssh.key"
        git submodule set-url <path-to-submodule> git@github.com:<organization/submodule>.git
        git submodule update --init --recursive
        git submodule set-url <path-to-submodule> https://github.com/<organization/submodule>.git
        unset GIT_SSH_COMMAND
Related