'Go Get' Private Repo from Bitbucket
Asked Answered
L

4

43

So basically, I have an Openshift Project that on Git push, downloads all libraries with 'Go get' and builds the project on-the-fly and so, I have some code I don't want people to see from my own library, and for it to compile properly, the code needs to be taken from github.com or another repo, so I created a private bitbucket.org repo, now, as a public repo it works fine, but when I try to 'Go Get' from my private repo, it gives me 'Forbidden 403'

How can I avoid this occurency? Thank you for reading and have a nice day!

Larimor answered 29/7, 2016 at 22:36 Comment(2)
you are using pipelines from BitBucket right? same problem I added an SSH key pair and added bitbucket.org as a know host but I still get 403. I think there is something missing in confluence.atlassian.com/x/DBuDMg since we can't add the PubKey to the remote hosts on Bitbucket.Bowerman
Since [email protected] you need to follow this solution.Isaak
P
83

go get uses git internally. The following one liners will make git and consequently go get clone your package via SSH.

Github:

git config --global url."[email protected]:".insteadOf "https://github.com/"

BitBucket:

git config --global url."[email protected]:".insteadOf "https://bitbucket.org/"

Photic answered 30/7, 2016 at 9:38 Comment(7)
Still getting 403 error for bitbucket after adding to global config, what other reasons could there be for this and how to solve?Compendious
i'm able to push to and pull from my repo using ssh, not sure how why go get is not seeing this config change, and i'm also getting same error when i put this config change in my local .git/configCompendious
btw, i'm doing the go get while building the docker image for my go app, not sure if it could be related to that tooCompendious
I faced the same issue @Compendious faced: even after adding global config was getting 403 error. Solved it by doing git config --system: git config --system url."[email protected]:".insteadOf "https://github.com/".Metathesize
Still didn't work after the command above, but the error mentioned that it goes to api.bitbucket.org, so I used: git config --global url."[email protected]:".insteadOf "https://api.bitbucket.org/"Chanell
Both tipps in combination helped me, when I tried to use go modules with a public bitbucket repository on windows: git config --system url."[email protected]:".insteadOf "https://api.bitbucket.org/"Flyblown
I was using my personal private bit bucket repo. follow helped me --> export GOPRIVATE=bitbucket.org/companyNameORuserName --> git config --global url."[email protected]:".insteadOf "https://bitbucket.org/"Filberto
E
20

Adition to setting git config described by others, I had to set GOPRIVATE.

Step 1. git config --global url."git@bitbucket.<YOUR-COMPANY>".insteadOf "https://bitbucket.org/<YOUR-COMPANY>"

Step 2. export GOPRIVATE=bitbucket.org/<YOUR-COMPANY>

the GOPRIAVTE is documented here: https://golang.org/cmd/go/#hdr-Module_configuration_for_non_public_modules

Earthwork answered 24/6, 2020 at 15:8 Comment(2)
With private modules hosted on GitHub setting GOPRIVATE as advised worked but differently from your example I had to modify git configuration with a specific authentication url e.g. git config --global url."https://${GITHUB_USER}:${GITHUB_TOKEN}@github.com".insteadOf "https://github.com". If the repository is private how do you authenticate?Vienna
Thank you @MarcoDufal, fixed!Earthwork
W
2

Ammar Bandukwala's answer is correct, but if you find yourself behind a firewall with SSH outgoing port (22) closed, you can use SSH over HTTPS port (443) instead:

git config --global url."ssh://[email protected]:443/<account_name>".insteadOf "https://bitbucket.org/<account_name>"

source: https://confluence.atlassian.com/bitbucket/troubleshoot-ssh-issues-271943403.html#TroubleshootSSHissues-Ifport22isblocked

Washy answered 19/11, 2019 at 9:39 Comment(0)
S
1

For anyone coming here after trying all of the above (and getting a 404 instead of a 403) this is because BB changed the response code from 403 to 404 when you're fetching a private repo. Golang has a fallback to HTTP when the repo cannot be resolved through SSH. This was fixed in golang version 1.16+.

Sc answered 14/9, 2022 at 11:41 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Megasporangium

© 2022 - 2024 — McMap. All rights reserved.