3

I am trying to build docker image for go app that is using different go packages from different private gitlab repos. When I was doing it on my local machine, I had to add the following commands in order to make it work;

git config --global url.git@gitlab.com:.insteadOf https://gitlab.com/
export GOPRIVATE=*gitlab*

In this case, my ssh key will be used and it worked fine.

Now, I want to do the same thing when building a Docker image but without using ssh keys so I though of using deployment token (the token has access for all repos in the gitlab group).

dockerfile:

FROM golang:1.14 AS builder

...

ARG USER
ARG KEY

RUN git config --global url."https://${USER}:${KEY}@gitlab.com/".insteadOf "https://gitlab.com/"
RUN export GOPRIVATE=*gitlab*

apparently, this didn't work and I become the following error:

Fatal: could not read Username for 'https://gitlab.com': terminal prompts disabled

Can you please share your ideas on how I could make it work using deployment tokens?

Kingindanord
  • 1,504
  • 1
  • 14
  • 36
  • are `${USER}` and `${KEY}` set as build-args during build ? – invad0r Apr 19 '20 at 17:42
  • yes, I build it like this: ```docker build --build-arg USER=$GITLAB_DOCKER_USER --build-arg KEY=$GITLAB_DOCKER_KEY -t go-dns . ``` – Kingindanord Apr 19 '20 at 17:43
  • is `GIT_TERMINAL_PROMPT` set ? see https://stackoverflow.com/a/38237165/2087704 – invad0r Apr 19 '20 at 17:47
  • yes, like this ```RUN export GIT_TERMINAL_PROMPT=1``` – Kingindanord Apr 19 '20 at 17:51
  • What does `RUN git config credential.helper` returns on `docker build`? – VonC Apr 19 '20 at 18:44
  • @VonC I am getting an error ```Step 8/24 : RUN git config credential.helper ---> Running in 956bb736901d The command '/bin/sh -c git config credential.helper' returned a non-zero code: 1``` – Kingindanord Apr 19 '20 at 18:48
  • @Kingindanord Sorry, try `RUN git config --global credential.helper` – VonC Apr 19 '20 at 18:53
  • @VonC same result ```Step 8/23 : RUN git config --global credential.helper ---> Running in 5412c2440f1e The command '/bin/sh -c git config --global credential.helper' returned a non-zero code: 1``` – Kingindanord Apr 19 '20 at 18:56

1 Answers1

3

As workaround, I clone the repo(s) manually and then edit go mod file

RUN git clone https://${USER}:${KEY}@gitlab.com/mygroup/myproject
RUN go mod init gitlab.com/mygroup/go-dns
RUN go mod edit -replace=gitlab.com/mygroup/myproject=./myproject
Kingindanord
  • 1,504
  • 1
  • 14
  • 36