Go get private repo in gitlab CI

Viewed 876

When attempting to download private go dependencies in Gitlab CI, I am given the error:

go: gitlab.com/path/to/private/repo@tag: reading gitlab.com/path/to/private/repo/go.mod at revision repo/tag: git ls-remote -q origin in /go/pkg/mod/cache/vcs/b9c14ef4557b9e7a5cee451c4c1a3b7c3f05519199a2d30cb24ae253b6a6fa2b: exit status 128:
    remote: The project you were looking for could not be found or you don't have permission to view it.
    fatal: repository 'https://gitlab.com/path/to.git/' not found

The .netrc file is set with a known good value, but it doesn't appear to be working correctly in Gitlab CI. I have replaced my own .netrc file with the one I am attempting to use, deleted ~/go/pkg/mod, and ran go mod tidy in my repo which successfully downloaded the private dependencies. Additionally, if I inject the .netrc contents into a docker build, and have the docker file retrieve the dependencies, there are no issues.

This is the simplest .gitlab-ci.yml file that will replicate:

failingJob:
  image: golang:1.17-alpine
  script:
  - apk update && apk add --no-cache git
  - echo "${NETRC}" > /root/.netrc
  - go mod download
  stage: test
  variables:
    GOPRIVATE: gitlab.com/private-org
workingJob:
  image: docker:19.03.12
  services:
  - docker:19.03.12-dind
  script:
    - docker build -t my-docker-image --build-arg SHARE_NETRC="$NETRC" --build-arg GOPRIVATE=gitlab.com/private-org -f docker/Dockerfile .

Dockerfile:

FROM golang:1.17-alpine AS BUILDER

ARG SHARE_NETRC
ARG GOPRIVATE

RUN apk update && apk add --no-cache git ca-certificates

COPY . /app/src

ENV GOPRIVATE=${GOPRIVATE}
ENV SHARE_NETRC=${SHARE_NETRC}

RUN echo "${SHARE_NETRC}" > /root/.netrc
WORKDIR /app/src

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /go/bin/app

FROM scratch

COPY --from=BUILDER /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=BUILDER /go/bin/app /

ENTRYPOINT [ "/app" ]

As you can see, the image used in the Dockerfile is the same as the image used in Job 1. The values for the .netrc file are the same between the jobs, and the location of the file is the same. The only difference I can see between the two jobs is that one is running directly on the runner while the other is in a Docker build context.

1 Answers
Related