Build Go project in Jenkins with dependencies in private BitBucket repository using SSH keys

Viewed 594

I'm trying to set up automated build for Go projects. We have some internal dependencies however available on our private BitBucket. Credentials are needed however to have go access these. I'm able to read the main repo using option Git and SSH but I'm able to download the dependencies from BitBucket.

I already tried with:

git config --global url."git@bitbucket.org:".insteadOf "https://bitbucket.org/"
export 'GOPRIVATE=bitbucket.org/*'

however this doesn't seem work, since the output:

+ go version
22:33:27 go version go1.16.4 darwin/arm64

+ go test
22:33:29 go: missing Mercurial command. See https://golang.org/s/gogetcmd
22:33:30 go: bitbucket.org/repositorie_url: reading https://api.bitbucket.org/2.0/repositorie_url/dependency_repo 403 Forbidden
22:33:30    server response: Access denied. You must have write or admin access.

How could I make sure go get or go install gets access to our private repository in a secure way?

NOTE: go test sems to ignore git configuration and it's trying to reach dependencies from https, in addition I have some Mercurial errors.

2 Answers

Go private dependencies are a bit complicated to resolve. Try downloading the dependencies before you do go test or anything else. There are 2 solutions I can present, try and let me know which one worked for you:

1. Using ssh key
When you have a ssh key that has access to the private repos, try this (Assuming the ssh is stored and retrived as env var with name BITBUCKET_SSH_KEY) :

mkdir -p ~/.ssh
echo "$BITBUCKET_SSH_KEY" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keygen -F bitbucket.org || ssh-keyscan bitbucket.org >>~/.ssh/known_hosts
git config --global url."git@bitbucket.org:".insteadOf "https://bitbucket.org/"
go env -w GOPRIVATE=bitbucket.org
go mod download

2. Using .netrc
You can generate a login token from bitbucket. With this token, have two env vars BITBUCKET_LOGIN and BITBUCKET_TOKEN and then try following:

go env -w GOPRIVATE=bitbucket.org
echo "machine bitbucket.org login ${BITBUCKET_LOGIN} password ${BITBUCKET_TOKEN}" > ~/.netrc
go mod download

Hello I finally found the error and the issue was: the $PATH of the enviroment! Seams that the computer has a different path of the default path of jenkins. If you want to use a certain enviroment of your local computer you should add a new variable $PATH in the enviroment, print $PATH in the local cmd and compare the $PATH on jenkinsfile

the solution in jenkinsfile:

pipeline {
    agent {
        label 'macmini'
    }
    environment {
        PATH = "$HOME/go/bin:" +
                    "/usr/local/bin:/Library/Apple/usr/bin" +
                    "$PATH"...
    }
}

console:

echo $PATH

# overrite $PATH enviroment
$PATH = "$HOME/go/bin:" +
                    "$HOME/go/bin:" +
                    "/usr/local/bin:/Library/Apple/usr/bin" +
                    "$PATH"...
Related