Building k6 binary with xk6 from private repository

Hello there!
For about few days I’m struggling with creating a docker image, which contains multiple extensions.
Thing is, I do have multiple modules with build with, 1st is in public repo, 2nd in private one.
More to add, I’m trying to reach private repo via SSH.
Already preconfigured stuff like

[url "ssh://git@gitlab.mycompany.com/"]
        insteadOf = https://gitlab.mycompany.com/

and set $GOPRIVATE

Sample dockerfile:

FROM golang:1.17-alpine as builder
WORKDIR $GOPATH/src/go.k6.io/k6
ADD . .
RUN apk --no-cache add openssh-client git
RUN CGO_ENABLED=0 go install -a -trimpath -ldflags "-s -w -X go.k6.io/k6/lib/consts.VersionDetails=$(date -u +"%FT%T%z")/$(git describe --always --long --dirty)"
RUN go install -trimpath go.k6.io/xk6/cmd/xk6@latest
RUN export GOPRIVATE=gitlab.mycompany.com/path/to/private/module
RUN --mount=type=ssh xk6 build \ 
--with github.com/path/to/module@latest \
--with gitlab.mycompany.com/path/to/module.git
RUN cp k6 $GOPATH/bin/k6

FROM alpine:3.14
RUN apk add --no-cache ca-certificates && \
    adduser -D -u 12345 -g 12345 k6
COPY --from=builder /go/bin/k6 /usr/bin/k6

USER 12345

ENTRYPOINT ["k6-2-ext"]

Unfortunatelly, I’m not really familiar with Go (this is a subject to change hopefully, considering how many questions are out there for me which require answers). I’ve read some docs about go get (which is basically xk6 build?), but those didn’t let me go far in my research.

So, what is happening now:
I have my ssh keys set, so I can clone that private repo with no issues on my machine.
I was wondering if this is a Docker issue I’m facing right now, so tried few times with just pure xk6 and k6 binaries, without containers.
It resulted in

Permission denied (publickey)

I’m just not sure, if I do have correct rights and can clone via SSH, how do I even get this sort of a message.

Could you please advise me how to create k6 binary with xk6 containing few modules, which are located in both public and private repos.
Cheers in advance.

Hi Nesodus,

Have you tried adding your keys to your Docker file? This answer and this post may help.

Please let me know afterward, thanks!

Hi @Nesodus,

I think the issue is with your RUN export GOPRIVATE=gitlab.mycompany.com/path/to/private/module instruction. This env var won’t be set for subsequent RUNs, since they’re executed in isolation.

You should either do:

RUN --mount=type=ssh \
  export GOPRIVATE=gitlab.mycompany.com/path/to/private/module && \
  xk6 build \ 
    --with github.com/path/to/module@latest \
    --with gitlab.mycompany.com/path/to/module.git

Or set it globally with:

ENV GOPRIVATE=gitlab.mycompany.com/path/to/private/module

Hello @inanc and @imiric
Thank you for your answers, I’m not passing keys directly, instead now using BuildKit with ssh mount.
And yea, I’ve tried to pass GOPRIVATE the same way @imiric told me to, yet unfortunately I’m facing same issue.
That’s why I’m not using docker at the moment, trying to figure out what is the problem on local machine.

I am able to clone from private repo, that’s what I’ve done today and it worked out.
But when I do

sudo xk6 build --with github.com/avitalique/xk6-file@latest --with gitlab.mycompany.com/path/to/private/module 

It gives me

go get: module gitlab.mycompany.com/path/to/private/module: git ls-remote -q origin in /Users/user/go/pkg/mod/cache/vcs/94b3303c7a35901013f532955a306590d23fdaff9dea6a90f27b370b135a82ad: exit status 128:
        git@gitlab.mycompany.com: Permission denied (publickey).
        fatal: Could not read from remote repository.

That is happening not in docker, keep in mind, I do have ssh keys and those are working, double checked my ~/.ssh/id_rsa.pub and the one in Gitlab.
I can do git clone and other git commands towards that repo, but I cannot do xk6 build or go get :frowning:

You can do them with the https:// repo url, right? Your original SSH redirection config should work, but just in case, maybe try something like this? :man_shrugging:

git config --global url."git@gitlab.mycompany.com:path/to/private/module.git".insteadOf "https://gitlab.mycompany.com/path/to/private/module"

I’ll try that, thank you!