Skip to content

Commit

Permalink
chore: deb and rpm packages
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Nov 26, 2021
1 parent 5c2b9bb commit 27bbea1
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ jobs:
name: Build binaries
run: |
make release
-
name: Build packages
run: |
make pkg
-
name: Upload artifacts
uses: actions/upload-artifact@v2
Expand Down
53 changes: 44 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# syntax=docker/dockerfile:1.3
# syntax=docker/dockerfile:1.3-labs

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

FROM docker:$DOCKERD_VERSION AS dockerd-release

# xx is a helper for cross-compilation
FROM --platform=$BUILDPLATFORM tonistiigi/xx:1.0.0 AS xx
FROM --platform=$BUILDPLATFORM tonistiigi/xx@sha256:77c7c6034382cb2a2ccf23f6f01c14813c2d9dc680c0020984f90e397db34365 AS xx

FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine AS golatest

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,46 @@ 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
ARG VENDOR
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 march)
[[ "$pkg" = "apk" ]] && arch=$(xx-info alpine-arch)
[[ "$pkg" = "deb" ]] && arch=$(xx-info debian-arch)
[[ "$pkg" = "rpm" ]] && arch=$(xx-info rhel-arch)
ARCH="${arch}" VERSION="${version}" VENDOR="${VENDOR}" 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
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ binaries:
binaries-cross:
$(BUILDX_CMD) bake binaries-cross

pkg:
$(BUILDX_CMD) bake pkg

install: binaries
mkdir -p ~/.docker/cli-plugins
install bin/buildx ~/.docker/cli-plugins/docker-buildx

release:
./hack/release

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

validate-all: lint test validate-vendor validate-docs

lint:
Expand Down Expand Up @@ -59,4 +65,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 install release release-pkg validate-all lint validate-vendor validate-docs validate-authors vendor docs authors
32 changes: 31 additions & 1 deletion docker-bake.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ variable "BIN_OUT" {
variable "RELEASE_OUT" {
default = "./release-out"
}
variable "VENDOR" {
default = "Docker, Inc"
}
variable "DOCS_FORMATS" {
default = "md"
}
Expand Down Expand Up @@ -122,12 +125,39 @@ target "binaries-cross" {
]
}

target "release" {
target "pkg" {
inherits = ["binaries"]
args = {
PKG = "deb,rpm"
VENDOR = VENDOR
}
target = "pkg"
platforms = [
"linux/amd64",
"linux/arm/v6",
"linux/arm/v7",
"linux/arm64",
"linux/ppc64le",
"linux/riscv64",
"linux/s390x"
]
}

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

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

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

target "image" {
inherits = ["meta-helper", "binaries"]
output = ["type=image"]
Expand Down
30 changes: 30 additions & 0 deletions hack/nfpm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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}
epoch: 0

section: default

maintainer: Docker <support@docker.com>
description: Docker Buildx plugin extends build capabilities with BuildKit.
vendor: ${VENDOR}
homepage: https://github.com/docker/buildx
license: Apache-2.0
disable_globbing: true

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

rpm:
group: Tools/Docker
compression: xz

deb:
compression: xz

0 comments on commit 27bbea1

Please sign in to comment.