Skip to content

Commit

Permalink
release apk, deb and rpm packages
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Nov 14, 2021
1 parent f002608 commit 2db685f
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 10 deletions.
49 changes: 41 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# syntax=docker/dockerfile:1.3
# syntax=docker/dockerfile:1.3-labs

ARG GO_VERSION=1.17
ARG NFPM_VERSION=v2.9.1
ARG DOCKERD_VERSION=20.10.8

FROM docker:$DOCKERD_VERSION AS dockerd-release
Expand All @@ -16,24 +17,24 @@ RUN apk add --no-cache file git
ENV GOFLAGS=-mod=vendor
WORKDIR /src

FROM gobase AS buildx-version
FROM gobase AS version
RUN --mount=target=. \
PKG=github.com/docker/buildx VERSION=$(git describe --match 'v[0-9]*' --dirty='.m' --always --tags) REVISION=$(git rev-parse HEAD)$(if ! git diff --no-ext-diff --quiet --exit-code; then echo .m; fi); \
echo "-X ${PKG}/version.Version=${VERSION} -X ${PKG}/version.Revision=${REVISION} -X ${PKG}/version.Package=${PKG}" | tee /tmp/.ldflags; \
echo -n "${VERSION}" | tee /tmp/.version;

FROM gobase AS buildx-build
FROM gobase AS build
ENV CGO_ENABLED=0
ARG LDFLAGS="-w -s"
ARG TARGETPLATFORM
RUN --mount=type=bind,target=. \
--mount=type=cache,target=/root/.cache \
--mount=type=cache,target=/go/pkg/mod \
--mount=type=bind,source=/tmp/.ldflags,target=/tmp/.ldflags,from=buildx-version \
--mount=type=bind,source=/tmp/.ldflags,target=/tmp/.ldflags,from=version \
set -x; xx-go build -ldflags "$(cat /tmp/.ldflags) ${LDFLAGS}" -o /usr/bin/buildx ./cmd/buildx && \
xx-verify --static /usr/bin/buildx

FROM buildx-build AS test
FROM build AS test
RUN --mount=type=bind,target=. \
--mount=type=cache,target=/root/.cache \
--mount=type=cache,target=/go/pkg/mod \
Expand All @@ -44,13 +45,13 @@ FROM scratch AS test-coverage
COPY --from=test /tmp/coverage.txt /coverage.txt

FROM scratch AS binaries-unix
COPY --from=buildx-build /usr/bin/buildx /
COPY --from=build /usr/bin/buildx /

FROM binaries-unix AS binaries-darwin
FROM binaries-unix AS binaries-linux

FROM scratch AS binaries-windows
COPY --from=buildx-build /usr/bin/buildx /buildx.exe
COPY --from=build /usr/bin/buildx /buildx.exe

FROM binaries-$TARGETOS AS binaries

Expand All @@ -59,12 +60,44 @@ FROM --platform=$BUILDPLATFORM alpine AS releaser
WORKDIR /work
ARG TARGETPLATFORM
RUN --mount=from=binaries \
--mount=type=bind,source=/tmp/.version,target=/tmp/.version,from=buildx-version \
--mount=type=bind,source=/tmp/.version,target=/tmp/.version,from=version \
mkdir -p /out && cp buildx* "/out/buildx-$(cat /tmp/.version).$(echo $TARGETPLATFORM | sed 's/\//-/g')$(ls buildx* | sed -e 's/^buildx//')"

FROM scratch AS release
COPY --from=releaser /out/ /

# Pkg
FROM --platform=$BUILDPLATFORM goreleaser/nfpm:${NFPM_VERSION} AS nfpm
FROM --platform=$BUILDPLATFORM alpine AS build-pkg
RUN apk add --no-cache bash file git
WORKDIR /build
COPY --from=xx / /
COPY --from=build /usr/bin/buildx /usr/bin/buildx
ARG TARGETPLATFORM
ARG PKG=deb
RUN --mount=type=bind,source=./hack/nfpm.yml,target=/tmp/nfpm.yml \
--mount=from=nfpm,source=/usr/local/bin/nfpm,target=/usr/bin/nfpm \
--mount=type=bind,source=/tmp/.version,target=/tmp/.version,from=version <<EOT
#!/usr/bin/env bash
set -e
version=$(cat /tmp/.version)
if [[ $version =~ ^[a-f0-9]{7}$ ]]; then
version="v0.0.0+${version}"
fi
for pkg in ${PKG//,/ }
do
arch=$(xx-info arch)
[[ "$pkg" = "apk" ]] && arch=$(xx-info alpine-arch)
[[ "$pkg" = "deb" ]] && arch=$(xx-info debian-arch)
ARCH="${arch}" VERSION="${version}" nfpm package --config /tmp/nfpm.yml --packager $pkg --target .
done
EOT

FROM scratch AS pkg
COPY --from=build-pkg /build/*.apk /
COPY --from=build-pkg /build/*.deb /
COPY --from=build-pkg /build/*.rpm /

# Shell
FROM docker:$DOCKERD_VERSION AS dockerd-release
FROM alpine AS shell
Expand Down
14 changes: 13 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ binaries:
binaries-cross:
$(BUILDX_CMD) bake binaries-cross

pkg-apk:
$(BUILDX_CMD) bake pkg-apk

pkg-deb:
$(BUILDX_CMD) bake pkg-deb

pkg-rpm:
$(BUILDX_CMD) bake pkg-rpm

pkg:
$(BUILDX_CMD) bake pkg

install: binaries
mkdir -p ~/.docker/cli-plugins
install bin/buildx ~/.docker/cli-plugins/docker-buildx
Expand Down Expand Up @@ -59,4 +71,4 @@ authors:
mod-outdated:
$(BUILDX_CMD) bake mod-outdated

.PHONY: shell binaries binaries-cross install release validate-all lint validate-vendor validate-docs validate-authors vendor docs authors
.PHONY: shell binaries binaries-cross pkg-deb pkg-rpm pkg-apk pkg install release validate-all lint validate-vendor validate-docs validate-authors vendor docs authors
53 changes: 52 additions & 1 deletion docker-bake.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,63 @@ target "binaries-cross" {
]
}

target "release" {
target "_pkg_common" {
inherits = ["binaries"]
target = "pkg"
platforms = [
"linux/amd64",
"linux/arm/v6",
"linux/arm/v7",
"linux/arm64",
"linux/ppc64le",
"linux/riscv64",
"linux/s390x"
]
}

target "pkg-apk" {
inherits = ["_pkg_common"]
args = {
PKG = "apk"
}
}

target "pkg-deb" {
inherits = ["_pkg_common"]
args = {
PKG = "deb"
}
}

target "pkg-rpm" {
inherits = ["_pkg_common"]
args = {
PKG = "rpm"
}
}

target "pkg" {
inherits = ["_pkg_common"]
args = {
PKG = "apk,deb,rpm"
}
}

group "release" {
targets = ["release-binaries", "release-packages"]
}

target "release-binaries" {
inherits = ["binaries-cross"]
target = "release"
output = [RELEASE_OUT]
}

target "release-packages" {
inherits = ["pkg"]
output = [RELEASE_OUT]
}

target "image" {
inherits = ["meta-helper", "binaries"]
output = ["type=image"]
Expand Down
53 changes: 53 additions & 0 deletions hack/nfpm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: docker-buildx

# The architecture is specified using Go nomenclature (GOARCH) and translated
# to the platform specific equivalent. In order to manually set the architecture
# to a platform specific value, use deb_arch, rpm_arch and apk_arch.
arch: ${ARCH}
platform: linux

version: ${VERSION}
#version_schema: none
#version_metadata: git
epoch: 0
#prerelease: beta1
#release: 1

section: default
#priority: extra

maintainer: Docker <[email protected]>
description: Docker Buildx plugin extends build capabilities with BuildKit.
vendor: Docker, Inc
homepage: https://github.com/docker/buildx
license: Apache-2.0
#changelog: "changelog.yaml"
disable_globbing: true

#depends:
# - docker-ce-cli

contents:
- src: /usr/bin/buildx
dst: /usr/libexec/docker/cli-plugins/docker-buildx

rpm:
group: Tools/Docker
#summary: Explicit Summary for Sample Package
compression: xz
#signature:
#key_file: key.gpg
#key_id: bc8acdd415bd80b3

deb:
compression: xz
#signature:
#key_file: key.gpg
#type: origin
#key_id: bc8acdd415bd80b3

apk:
#signature:
#key_file: key.gpg
#key_name: origin
#key_id: ignored

0 comments on commit 2db685f

Please sign in to comment.