Created
May 27, 2024 09:03
-
-
Save ulexxander/2c031e119fbb0074aa72c259e97c147d to your computer and use it in GitHub Desktop.
Cache and secret mounts in Dockerfile for Go projects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com".insteadOf "https://gitlab.com" | |
docker build -t the-app --build-arg "VERSION=${VERSION}" --secret id=gitconfig,src=${HOME}/.gitconfig . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM golang:1.20-bookworm AS builder | |
WORKDIR /build | |
ENV \ | |
GOOS=linux \ | |
GOARCH=amd64 \ | |
CGO_ENABLED=0 \ | |
GOCACHE=/go-build-cache \ | |
GOMODCACHE=/go-mod-cache | |
COPY . . | |
ARG VERSION | |
RUN \ | |
--mount=type=secret,id=gitconfig,target=/root/.gitconfig \ | |
--mount=type=cache,target=/go-mod-cache \ | |
--mount=type=cache,target=/go-build-cache \ | |
go build -ldflags "-X 'main.version=${VERSION}'" ./cmd/service | |
FROM buildpack-deps:bookworm-curl | |
WORKDIR /app | |
COPY --from=builder /build/service . | |
EXPOSE 80 | |
CMD ["./service"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment