git - How to handle both SSH and Personal Access Token authentication in a single Dockerfile for private Go modules? - Stack Ove

admin2025-04-18  3

I have a Dockerfile that needs to handle two different authentication methods for private Go modules:

  1. Local development (using ssh)
  2. Github Actions (using a Personal Access Token)

Currently these are two separate dockerfiles, but I would like to be able to combine them into one. Perhaps my approach here is wrong and there is a simpler solution, so I request your help, because googling and using LLM's is not getting me any further.

This is currently my Dockerfile for local development:

FROM docker.io/golang:1.23 AS builder
WORKDIR /go/src/

RUN apt install openssh-client git
RUN mkdir -p -m 0600 ~/.ssh && ssh-keyscan github >> ~/.ssh/known_hosts

COPY go.mod go.sum ./

RUN go env -w GOPRIVATE=github/my-organization
RUN go env -w GONOPROXY=github/my-organization
RUN git config --global [email protected]:.insteadOf 

RUN --mount=type=ssh go mod download && go mod verify

Which I build with docker build . --ssh default

And my Dockerfile for Github Actions:

RUN go env -w GOPRIVATE=github/my-organization
RUN go env -w GONOPROXY=github/my-organization
ARG CICD_PERSONAL_ACCESS
ENV CICD_PERSONAL_ACCESS="${CICD_PERSONAL_ACCESS}"
RUN git config --global url."https://x-access-token:${CICD_PERSONAL_ACCESS}@github".insteadOf https://github

RUN go mod download && go mod verify

Which I build with docker build -f cicd.Dockerfile . --build-arg CICD_PERSONAL_ACCESS=${{ secrets.CICD_PERSONAL_ACCESS }}

转载请注明原文地址:http://anycun.com/QandA/1744906085a89291.html