5.5 KiB · text History 3208487
# syntax=docker/dockerfile:1
#
# The `vgl` CLI, packaged as a .deb and an .rpm, as gitgud's `gg` is.
#
#   docker buildx build --target packages \
#     --output type=local,dest=./cli-dist --file cli/Dockerfile .
#   podman build --format docker --target packages \
#     --output type=local,dest=./cli-dist --file cli/Dockerfile .
#
# The build context is the REPOSITORY ROOT, not cli/: the copyright file
# in both packages is the repository's LICENSE.
#
# One source tree, two builds, because a Rust binary is linked against the
# glibc of whatever compiled it: the .deb on Debian trixie, the .rpm on
# EL9 (the oldest glibc in the rpm world worth shipping to). Building both
# on one base would make a package that installs and then dies with
# `version GLIBC_2.41 not found`.
#
# Each stage compiles the dependency tree from Cargo.toml and Cargo.lock
# alone before the source is copied in, so that layer survives in the
# registry build cache across commits.
#
# Both stages want a C toolchain and CMake: aws-lc-sys, under rustls, is
# compiled rather than vendored as a binary.

# The same toolchain as checks.yml and the server's Dockerfile; move them together.
ARG RUST_VERSION=1.92.0

# The version stamp, as CI computes it. The defaults are what a bare build
# (or `cargo deb` by hand) produces.
ARG PKG_VERSION=
ARG RPM_RELEASE=1
ARG MAINTAINER=
ARG SUITES="trixie forky"

# ── .deb, on Debian trixie ───────────────────────────────────────────

FROM docker.io/rust:${RUST_VERSION}-trixie AS deb-builder

# dpkg-dev provides dpkg-shlibdeps, which cargo-deb's `$auto` dependency
# resolution shells out to. Without it the package would declare no libc
# dependency at all, quietly; the build checks for one before it finishes.
RUN apt-get update \
  && apt-get install -y --no-install-recommends cmake perl pkg-config dpkg-dev \
  && rm -rf /var/lib/apt/lists/* \
  && rustup component add rustfmt clippy \
  && cargo install --locked cargo-deb

WORKDIR /src/cli

# Dependencies first, against a stub binary: everything but this crate.
COPY cli/Cargo.toml cli/Cargo.lock ./
RUN mkdir -p src \
  && echo 'fn main() {}' > src/main.rs \
  && cargo build --release --locked \
  && rm -rf src target/release/vgl target/release/deps/vgl-*

COPY LICENSE /src/LICENSE
COPY cli ./

ARG PKG_VERSION
ARG MAINTAINER
ARG SUITES
ARG SOURCE_DATE_EPOCH

# The CLI's checks run here too: a package isn't worth building from a
# tree that fails them.
#
# One artifact per suite: the same bytes with a different version suffix,
# so each suite's `Packages` index can name its own. Each is copied out
# inside the loop, because cargo-deb empties target/debian on every run.
RUN set -eux; \
    mkdir -p /out; \
    cargo fmt --check; \
    cargo clippy --all-targets --locked -- -D warnings; \
    cargo test --locked; \
    for suite in $SUITES; do \
      set --; \
      if [ -n "$PKG_VERSION" ]; then set -- --deb-version "${PKG_VERSION}+${suite}"; fi; \
      if [ -n "$MAINTAINER" ]; then set -- "$@" --maintainer "$MAINTAINER"; fi; \
      cargo deb "$@"; \
      cp target/debian/*.deb /out/; \
    done; \
    for package in /out/*.deb; do \
      dpkg-deb -f "$package" Depends | grep -q libc6 \
        || { echo "$package declares no library dependencies; is dpkg-dev installed?" >&2; exit 1; }; \
    done; \
    ls -l /out

# ── .rpm, on EL9 ─────────────────────────────────────────────────────

# The toolchain is copied in whole from the Debian image rather than
# fetched with the EL9 image's curl, which can't load a CA file on the
# forge's runner (see gitgud's cli/Dockerfile). The official
# x86_64-unknown-linux-gnu builds target glibc 2.17, so rustc and cargo
# run here unmodified.
FROM docker.io/rust:${RUST_VERSION}-trixie AS toolchain

FROM docker.io/rockylinux/rockylinux:9 AS rpm-builder

ENV RUSTUP_HOME=/usr/local/rustup \
    CARGO_HOME=/usr/local/cargo \
    PATH=/usr/local/cargo/bin:${PATH}
COPY --from=toolchain /usr/local/rustup /usr/local/rustup
COPY --from=toolchain /usr/local/cargo /usr/local/cargo

COPY --from=toolchain /etc/ssl/certs/ca-certificates.crt /etc/pki/ca-trust/vgl-ca-bundle.pem
ENV CURL_CA_BUNDLE=/etc/pki/ca-trust/vgl-ca-bundle.pem \
    SSL_CERT_FILE=/etc/pki/ca-trust/vgl-ca-bundle.pem \
    CARGO_HTTP_CAINFO=/etc/pki/ca-trust/vgl-ca-bundle.pem

RUN set -eux; \
    dnf -y install gcc gcc-c++ make cmake perl; \
    dnf clean all; \
    cargo install --locked cargo-generate-rpm

WORKDIR /src/cli

COPY cli/Cargo.toml cli/Cargo.lock ./
RUN mkdir -p src \
  && echo 'fn main() {}' > src/main.rs \
  && cargo build --release --locked \
  && rm -rf src target/release/vgl target/release/deps/vgl-*

COPY LICENSE /src/LICENSE
COPY cli ./

ARG RPM_RELEASE
# Read from the environment by cargo-generate-rpm, which clamps every
# file's mtime and the package's build time to it.
ARG SOURCE_DATE_EPOCH

# RPM has no `~` for pre-release ordering, so the stamp goes in the
# release: `0.ci…` sorts before `1`, and a later tagged 0.1.0-1 upgrades
# over it.
RUN set -eux; \
    cargo build --release --locked; \
    cargo generate-rpm --set-metadata "release = \"${RPM_RELEASE}\""; \
    mkdir -p /out; \
    cp target/generate-rpm/*.rpm /out/; \
    ls -l /out

# ── what CI collects ─────────────────────────────────────────────────

FROM scratch AS packages
COPY --from=deb-builder /out/ /
COPY --from=rpm-builder /out/ /