Skip to content

Commit

Permalink
Enhance dockerfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
crazy-max committed Dec 25, 2021
1 parent 7b9b01b commit 0261311
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 137 deletions.
12 changes: 0 additions & 12 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,7 @@ on:
- 'master'
tags:
- 'v*'
paths-ignore:
- '**.md'
- '.github/workflows/docs.yml'
- 'docs/**'
- 'mkdocs.yml'
pull_request:
branches:
- 'master'
paths-ignore:
- '**.md'
- '.github/workflows/docs.yml'
- 'docs/**'
- 'mkdocs.yml'

env:
DOCKERHUB_SLUG: crazymax/ftpgrab
Expand Down
12 changes: 0 additions & 12 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,7 @@ on:
push:
tags:
- 'v*'
paths:
- '.github/workflows/docs.yml'
- 'docs/**'
- 'CHANGELOG.md'
- 'mkdocs.yml'
pull_request:
branches:
- 'master'
paths:
- '.github/workflows/docs.yml'
- 'docs/**'
- 'CHANGELOG.md'
- 'mkdocs.yml'

jobs:
publish:
Expand Down
44 changes: 32 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,41 +1,61 @@
# syntax=docker/dockerfile:1.3
# syntax=docker/dockerfile:1.3-labs

ARG GO_VERSION
ARG GORELEASER_XX_VERSION="1.2.2"

FROM --platform=$BUILDPLATFORM crazymax/goreleaser-xx:latest AS goreleaser-xx
FROM --platform=$BUILDPLATFORM crazymax/goreleaser-xx:${GORELEASER_XX_VERSION} AS goreleaser-xx
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine3.14 AS base
ENV CGO_ENABLED=0
COPY --from=goreleaser-xx / /
RUN apk add --no-cache ca-certificates gcc file git linux-headers musl-dev tar
RUN apk add --no-cache file git
WORKDIR /src

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

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=/root/.cache \
--mount=type=cache,target=/go/pkg/mod <<EOT
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 base AS build
ARG TARGETPLATFORM
ARG GIT_REF
RUN --mount=type=bind,target=/src,rw \
--mount=type=cache,target=/root/.cache \
--mount=type=cache,target=/go/pkg/mod \
goreleaser-xx --debug \
--name "ftpgrab" \
--dist "/out" \
--hooks="go mod tidy" \
--hooks="go mod download" \
--main="./cmd/main.go" \
--main="./cmd" \
--flags="-trimpath" \
--ldflags="-s -w -X 'main.version={{.Version}}'" \
--files="CHANGELOG.md" \
--files="LICENSE" \
--files="README.md"

FROM scratch AS artifacts
FROM scratch AS artifact
COPY --from=build /out/*.tar.gz /
COPY --from=build /out/*.zip /

FROM scratch AS binary
COPY --from=build /usr/local/bin/ftpgrab* /

FROM alpine:3.14
RUN apk --update --no-cache add ca-certificates openssl
COPY --from=build /usr/local/bin/ftpgrab /usr/local/bin/ftpgrab

ENV FTPGRAB_DB_PATH="/db/ftpgrab.db" \
FTPGRAB_DOWNLOAD_OUTPUT="/download"

RUN apk --update --no-cache add ca-certificates openssl
COPY --from=build /usr/local/bin/ftpgrab /usr/local/bin/ftpgrab
RUN ftpgrab --version

VOLUME [ "/db", "/download" ]
ENTRYPOINT [ "ftpgrab" ]
3 changes: 3 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
comment: false
github_checks:
annotations: false
112 changes: 53 additions & 59 deletions docker-bake.hcl
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
// Go version
variable "GO_VERSION" {
default = "1.17"
}

target "go-version" {
target "_common" {
args = {
GO_VERSION = GO_VERSION
}
}

// GitHub reference as defined in GitHub Actions (eg. refs/head/master))
variable "GITHUB_REF" {
default = ""
}

target "git-ref" {
args = {
GIT_REF = GITHUB_REF
BUILDKIT_CONTEXT_KEEP_GIT_DIR = 1
}
}

Expand All @@ -29,54 +18,15 @@ group "default" {
targets = ["image-local"]
}

group "validate" {
targets = ["lint", "vendor-validate"]
}

target "lint" {
inherits = ["go-version"]
dockerfile = "./hack/lint.Dockerfile"
target = "lint"
output = ["type=cacheonly"]
}

target "vendor-validate" {
inherits = ["go-version"]
dockerfile = "./hack/vendor.Dockerfile"
target = "validate"
output = ["type=cacheonly"]
}

target "vendor-update" {
inherits = ["go-version"]
dockerfile = "./hack/vendor.Dockerfile"
target = "update"
output = ["."]
}

target "vendor-outdated" {
inherits = ["go-version"]
dockerfile = "./hack/vendor.Dockerfile"
target = "outdated"
output = ["type=cacheonly"]
}

target "test" {
inherits = ["go-version"]
dockerfile = "./hack/test.Dockerfile"
target = "test-coverage"
output = ["."]
}

target "docs" {
dockerfile = "./hack/docs.Dockerfile"
target = "release"
output = ["./site"]
target "binary" {
inherits = ["_common"]
target = "binary"
output = ["./bin"]
}

target "artifact" {
inherits = ["go-version", "git-ref"]
target = "artifacts"
inherits = ["_common"]
target = "artifact"
output = ["./dist"]
}

Expand All @@ -103,7 +53,7 @@ target "artifact-all" {
}

target "image" {
inherits = ["go-version", "git-ref", "docker-metadata-action"]
inherits = ["_common", "docker-metadata-action"]
}

target "image-local" {
Expand All @@ -122,3 +72,47 @@ target "image-all" {
"linux/ppc64le"
]
}

target "test" {
inherits = ["_common"]
target = "test-coverage"
output = ["."]
}

target "vendor" {
inherits = ["_common"]
dockerfile = "./hack/vendor.Dockerfile"
target = "update"
output = ["."]
}

target "docs" {
dockerfile = "./hack/docs.Dockerfile"
target = "release"
output = ["./site"]
}

group "validate" {
targets = ["lint", "vendor-validate"]
}

target "lint" {
inherits = ["_common"]
dockerfile = "./hack/lint.Dockerfile"
target = "lint"
output = ["type=cacheonly"]
}

target "vendor-validate" {
inherits = ["_common"]
dockerfile = "./hack/vendor.Dockerfile"
target = "validate"
output = ["type=cacheonly"]
}

target "vendor-outdated" {
inherits = ["_common"]
dockerfile = "./hack/vendor.Dockerfile"
target = "outdated"
output = ["type=cacheonly"]
}
8 changes: 5 additions & 3 deletions hack/docs.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# syntax=docker/dockerfile:1.3

FROM squidfunk/mkdocs-material:7.2.6 AS base
ARG MKDOCS_VERSION="7.2.6"

FROM squidfunk/mkdocs-material:${MKDOCS_VERSION} AS base
RUN apk add --no-cache \
git \
git-fast-import \
Expand All @@ -18,7 +20,7 @@ RUN apk add --no-cache \

FROM base AS generate
RUN --mount=type=bind,target=. \
mkdocs build --strict --site-dir /tmp/site
mkdocs build --strict --site-dir /out

FROM scratch AS release
COPY --from=generate /tmp/site/ /
COPY --from=generate /out /
10 changes: 5 additions & 5 deletions hack/lint.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# syntax=docker/dockerfile:1.3

ARG GO_VERSION
ARG GOLANGCI_LINT_VERSION="v1.37"

FROM golang:${GO_VERSION}-alpine AS base
RUN apk add --no-cache gcc musl-dev
RUN apk add --no-cache gcc linux-headers musl-dev
WORKDIR /src

FROM golangci/golangci-lint:v1.37-alpine AS golangci-lint

FROM golangci/golangci-lint:${GOLANGCI_LINT_VERSION}-alpine AS golangci-lint
FROM base AS lint
RUN --mount=type=bind,target=. \
--mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/root/.cache/golangci-lint \
--mount=type=cache,target=/root/.cache \
--mount=from=golangci-lint,source=/usr/bin/golangci-lint,target=/usr/bin/golangci-lint \
golangci-lint run --timeout 10m0s ./...
20 changes: 0 additions & 20 deletions hack/test.Dockerfile

This file was deleted.

38 changes: 24 additions & 14 deletions hack/vendor.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
# syntax=docker/dockerfile:1.3
# syntax=docker/dockerfile:1.3-labs

ARG GO_VERSION
ARG GOMOD_OUTDATED_VERSION="v0.8.0"

FROM golang:${GO_VERSION}-alpine AS base
RUN apk add --no-cache git
RUN apk add --no-cache git linux-headers musl-dev
WORKDIR /src

FROM base AS vendored
RUN --mount=type=bind,target=.,rw \
--mount=type=cache,target=/go/pkg/mod \
go mod tidy && go mod download && \
mkdir /out && cp go.mod go.sum /out
--mount=type=cache,target=/go/pkg/mod <<EOT
set -e
go mod tidy
go mod download
mkdir /out
cp go.mod go.sum /out
EOT

FROM scratch AS update
COPY --from=vendored /out /

FROM vendored AS validate
RUN --mount=type=bind,target=.,rw \
git add -A && cp -rf /out/* .; \
if [ -n "$(git status --porcelain -- go.mod go.sum)" ]; then \
echo >&2 'ERROR: Vendor result differs. Please vendor your package with "docker buildx bake vendor-update"'; \
git status --porcelain -- go.mod go.sum; \
exit 1; \
fi
RUN --mount=type=bind,target=.,rw <<EOT
set -e
git add -A
cp -rf /out/* .
diff=$(git status --porcelain -- go.mod go.sum)
if [ -n "$diff" ]; then
echo >&2 'ERROR: Vendor result differs. Please vendor your package with "docker buildx bake vendor"'
echo "$diff"
exit 1
fi
EOT

FROM psampaz/go-mod-outdated:v0.8.0 AS go-mod-outdated
FROM psampaz/go-mod-outdated:${GOMOD_OUTDATED_VERSION} AS go-mod-outdated
FROM base AS outdated
RUN --mount=type=bind,target=.,ro \
RUN --mount=type=bind,target=. \
--mount=type=cache,target=/go/pkg/mod \
--mount=from=go-mod-outdated,source=/home/go-mod-outdated,target=/usr/bin/go-mod-outdated \
go list -mod=readonly -u -m -json all | go-mod-outdated -update -direct

0 comments on commit 0261311

Please sign in to comment.