Dockerfile: switch to xx

This commit is contained in:
CrazyMax
2022-10-22 21:10:55 +02:00
parent 51bdafbc1d
commit f43e9895e6
2 changed files with 58 additions and 37 deletions

View File

@@ -1,59 +1,87 @@
# syntax=docker/dockerfile:1
ARG GO_VERSION="1.19"
ARG GORELEASER_XX_VERSION="1.2.5"
ARG ALPINE_VERSION="3.16"
ARG XX_VERSION="1.1.2"
FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx
FROM --platform=$BUILDPLATFORM crazymax/goreleaser-xx:${GORELEASER_XX_VERSION} AS goreleaser-xx
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine AS base
COPY --from=xx / /
ENV CGO_ENABLED=0
COPY --from=goreleaser-xx / /
RUN apk add --no-cache file git
WORKDIR /src
FROM base AS version
RUN --mount=target=. \
echo $(git describe --match 'v[0-9]*' --dirty='.m' --always --tags) | tee /tmp/.version
FROM base AS vendored
RUN --mount=type=bind,source=.,target=/src,rw \
--mount=type=cache,target=/go/pkg/mod \
go mod tidy && go mod download
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download
FROM vendored AS test
ENV CGO_ENABLED=1
RUN apk add --no-cache gcc linux-headers musl-dev
RUN --mount=type=bind,target=. \
--mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build <<EOT
go test -v -coverprofile=/tmp/coverage.txt -covermode=atomic -race ./...
go tool cover -func=/tmp/coverage.txt
--mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build <<EOT
set -ex
go test -v -coverprofile=/tmp/coverage.txt -covermode=atomic -race ./...
go tool cover -func=/tmp/coverage.txt
EOT
FROM scratch AS test-coverage
COPY --from=test /tmp/coverage.txt /coverage.txt
FROM vendored AS build
ARG GIT_REF
ARG TARGETPLATFORM
RUN --mount=type=bind,target=. \
--mount=type=cache,target=/root/.cache \
--mount=target=/go/pkg/mod,type=cache \
goreleaser-xx --debug \
--name "diun" \
--dist "/out" \
--main="./cmd" \
--flags="-trimpath" \
--ldflags="-s -w -X 'main.version={{.Version}}'" \
--files="CHANGELOG.md" \
--files="LICENSE" \
--files="README.md"
--mount=type=bind,from=version,source=/tmp/.version,target=/tmp/.version \
--mount=type=cache,target=/root/.cache \
--mount=type=cache,target=/go/pkg/mod <<EOT
set -ex
xx-go build -trimpath -ldflags "-s -w -X main.version=$(cat /tmp/.version)" -o /usr/bin/diun ./cmd
xx-verify --static /usr/bin/diun
EOT
FROM scratch AS binary-unix
COPY --link --from=build /usr/bin/diun /
FROM scratch AS binary-windows
COPY --link --from=build /usr/bin/diun /diun.exe
FROM binary-unix AS binary-darwin
FROM binary-unix AS binary-linux
FROM binary-$TARGETOS AS binary
FROM --platform=$BUILDPLATFORM alpine:${ALPINE_VERSION} AS build-artifact
RUN apk add --no-cache bash tar zip
WORKDIR /work
ARG TARGETOS
ARG TARGETARCH
ARG TARGETVARIANT
RUN --mount=type=bind,target=/src \
--mount=type=bind,from=binary,target=/build \
--mount=type=bind,from=version,source=/tmp/.version,target=/tmp/.version <<EOT
set -ex
mkdir /out
version=$(cat /tmp/.version)
cp /build/* /src/CHANGELOG.md /src/LICENSE /src/README.md .
if [ "$TARGETOS" = "windows" ]; then
zip -r "/out/diun_${version#v}_${TARGETOS}_${TARGETARCH}${TARGETVARIANT}.zip" .
else
tar -czvf "/out/diun_${version#v}_${TARGETOS}_${TARGETARCH}${TARGETVARIANT}.tar.tgz" .
fi
EOT
FROM scratch AS artifact
COPY --from=build /out/*.tar.gz /
COPY --from=build /out/*.zip /
COPY --link --from=build-artifact /out /
FROM scratch AS binary
COPY --from=build /usr/local/bin/diun* /
FROM alpine:3.16
FROM alpine:${ALPINE_VERSION}
RUN apk --update --no-cache add ca-certificates openssl
COPY --from=build /usr/local/bin/diun /usr/local/bin/diun
COPY --from=build /usr/bin/diun /usr/local/bin/diun
ENV PROFILER_PATH="/profiler" \
DIUN_DB_PATH="/data/diun.db"
VOLUME [ "/data" ]

View File

@@ -2,20 +2,13 @@ variable "GO_VERSION" {
default = "1.19"
}
// GITHUB_REF is the actual ref that triggers the workflow
// https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
variable "GITHUB_REF" {
default = ""
}
target "_common" {
args = {
GO_VERSION = GO_VERSION
GIT_REF = GITHUB_REF
}
}
// Special target: https://github.com/docker/metadata-action#bake-definition
# Special target: https://github.com/docker/metadata-action#bake-definition
target "docker-metadata-action" {
tags = ["diun:local"]
}