b4447bb15e
Inject the build version into the binary using the CI_COMMIT argument for better traceability of deployments. Update the Dockerfile to pass the commit hash to the build process, ensuring that each build contains version information tied to the specific commit.
33 lines
1.2 KiB
Docker
33 lines
1.2 KiB
Docker
FROM amd64/golang:1.24.4@sha256:3494bbe140127d12656113203ec91b8e3ff34e8a2b06a0a22bb0d8a41cc69e53 as modules
|
|
WORKDIR /build
|
|
ADD go.* /build
|
|
RUN go mod download
|
|
|
|
FROM modules as build
|
|
ARG CI_COMMIT
|
|
WORKDIR /build
|
|
ENV CGO_ENABLED=0
|
|
ADD . /build
|
|
RUN CGO_ENABLED=1 go test -mod=readonly -race -coverprofile=coverage.txt.tmp -covermode=atomic -coverpkg=$(go list ./... | tr '\n' , | sed 's/,$//') ./...
|
|
RUN ["/bin/bash", "-c", "cat coverage.txt.tmp | grep -v testing.go | grep -v -f <(find . -type f | xargs grep -l 'Code generated by github.com/99designs/gqlgen, DO NOT EDIT') > coverage.txt"]
|
|
RUN go tool cover -html=coverage.txt -o coverage.html
|
|
RUN go tool cover -func=coverage.txt
|
|
RUN rm coverage.txt.tmp
|
|
|
|
RUN GOOS=linux GOARCH=amd64 go build \
|
|
-a -installsuffix cgo \
|
|
-mod=readonly \
|
|
-o /release/service \
|
|
-ldflags "-w -s -X main.buildVersion=${CI_COMMIT}" \
|
|
./cmd/service/service.go
|
|
|
|
FROM scratch as export
|
|
COPY --from=build /build/coverage.txt /
|
|
|
|
FROM scratch
|
|
ENV TZ Europe/Stockholm
|
|
COPY --from=build /usr/share/zoneinfo /usr/share/zoneinfo
|
|
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
|
|
COPY --from=build /release/service /
|
|
CMD ["/service"]
|