1

I have a bitbucket hosted git repository, which has a git submodule repo. I'd like to use a relative url, to allow both https and ssh usage, in the .gitmodules file:

[submodule "sm-ourlib"]
path = sm-ourlib
# url = git@bitbucket.org:companyname/ourlib.git
# Use relative url to allow for both HTTP and SSH access, see https://stackoverflow.com/a/44630028/50899
url = ../ourlib.git

However, it seems bitbucket pipelines can't handle the relative url as expected, since it doesn't use ssh for the subrepo for relative urls, but instead uses https and fails. (for the absolute url, it uses ssh correctly).

The pipeline run fails in the git submodule update --init step with:

+ git submodule update --init
Submodule 'sm-ourlib' (http://bitbucket.org/companyname/ourlib.git) registered for path 'sm-ourlib'
Cloning into '/opt/atlassian/pipelines/agent/build/sm-ourlib'...
fatal: could not read Username for 'https://bitbucket.org': No such device or address
fatal: clone of 'http://bitbucket.org/companyname/ourlib.git' into submodule path '/opt/atlassian/pipelines/agent/build/sm-ourlib' failed
Failed to clone 'sm-ourlib'. Retry scheduled
Cloning into '/opt/atlassian/pipelines/agent/build/sm-ourlib'...
fatal: could not read Username for 'https://bitbucket.org': No such device or address
fatal: clone of 'http://bitbucket.org/companyname/ourlib.git' into submodule path '/opt/atlassian/pipelines/agent/build/sm-ourlib' failed
Failed to clone 'sm-ourlib' a second time, aborting

Is this a known issue, or am I doing something wrong?

Rabarberski
  • 23,854
  • 21
  • 74
  • 96

2 Answers2

2

I was facing the same issue. Bitbucket is using https during the checkout hence the relative URLs are resolved with https, too. With the following hack the build pipeline can be patched to rewrite the .gitmodules files:

          script:
            - sed -i 's/url = \.\.\(.*\)/url = git@bitbucket.org:<your org>\1\.git/g' .gitmodules
            - git submodule update --init
k_o_
  • 5,143
  • 1
  • 34
  • 43
  • 1
    Great! Clever trick, and it worked for me. I did have to change it slightly to get it working, i.e. removed the "\.git" part so it became `sed -i 's/url = \.\.\(.*\)/url = git@bitbucket.org:\1/g' .gitmodules` I also had to use the `sed -e` option to test it locally on my mac. – Rabarberski Jan 20 '22 at 08:38
1

Note: The accepted answer doesn't work.

Here's the fixed and improved version of @k_o_'s answer:
Just keep the relative urls in the repo:

[submodule "sm-ourlib"]
    path = sm-ourlib
    url = ../ourlib.git

Edit the BitBucket pipeline to replace the HTTPS url with SSH on the go.

- sed -i 's/url = \.\.\(.*\)/url = git@bitbucket.org:'"$BITBUCKET_WORKSPACE"'\1/g' .gitmodules
- git submodule update --init --recursive

This way you'll be able to clone the code with both SSH and HTTPS on the developer side, and use SSH on the pipeline side (that is required from the BitBucket).

Details:

  1. Removed the \.git part as suggested by @rabarberski
  2. Added $BITBUCKET_WORKSPACE so we could automatically fetch the company/workspace name instead of hard-coding
  3. Added --recursive to make sure the submodules are also loaded
Just Shadow
  • 10,860
  • 6
  • 57
  • 75