Draft: ci: Test docker build on fsinfo runner #979

Open
jfowl wants to merge 5 commits from jonas-test-ci-docker-building-quemu-on-fsinfo-runner into next
2 changed files with 103 additions and 81 deletions

View file

@ -17,28 +17,39 @@ variables:
.docker-shared-settings:
stage: "build docker image"
needs: []
tags: [ "docker" ]
tags: [ "docker", "tudo-fsinfo" ]
variables:
# Docker in Docker:
DOCKER_BUILDKIT: 1
DOCKER_TLS_CERTDIR: "/certs"
image:
name: docker.io/docker
services:
- name: docker.io/docker:dind
alias: docker
script:
- apk add openssh-client
- eval $(ssh-agent -s)
- mkdir -p ~/.ssh && chmod 700 ~/.ssh
- printf "Host *\n\tStrictHostKeyChecking no\n\n" >> ~/.ssh/config
- sh .gitlab/setup-buildx-remote-builders.sh
- free -h; lscpu || true
- docker context create ci-context
- >
docker buildx create
--name "multi"
--driver "docker-container"
--platform "linux/arm64,linux/amd64"
--use ci-context
# Authorize against this project's own image registry:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
# Build multiplatform image and push to temporary tag:
- >
docker buildx build
--platform "linux/arm/v7,linux/arm64,linux/amd64"
--platform "linux/arm64,linux/amd64"
--pull
--cache-from="type=registry,ref=$CI_REGISTRY_IMAGE/$TAG"
--cache-from="type=registry,ref=$CI_REGISTRY_IMAGE/matrix-conduit:next"
--cache-from="type=registry,ref=$CI_REGISTRY_IMAGE/temporary-ci-images:buildkit-cache-$CI_COMMIT_REF_NAME"
--cache-to="type=registry,mode=max,compression=zstd,ref=$CI_REGISTRY_IMAGE/temporary-ci-images:buildkit-cache-$CI_COMMIT_REF_NAME"
--cache-to type=local,mode=max,dest=path/tmp/conduit-docker-buildcache
--cache-from type=local,src=path/tmp/conduit-docker-buildcache
--ulimit nofile=2048
--tag "$CI_REGISTRY_IMAGE/temporary-ci-images:$CI_JOB_ID"
--push
--provenance=false
@ -46,7 +57,13 @@ variables:
# Build multiplatform image to deb stage and extract their .deb files:
- >
docker buildx build
--platform "linux/arm/v7,linux/arm64,linux/amd64"
--platform "linux/arm64,linux/amd64"
--pull
--cache-from="type=registry,ref=$CI_REGISTRY_IMAGE/$TAG"
--cache-from="type=registry,ref=$CI_REGISTRY_IMAGE/matrix-conduit:next"
--cache-from="type=registry,ref=$CI_REGISTRY_IMAGE/temporary-ci-images:buildkit-cache-$CI_COMMIT_REF_NAME"
--cache-from type=local,src=path/tmp/conduit-docker-buildcache
--ulimit nofile=2048
--target "packager-result"
--output="type=local,dest=/tmp/build-output"
--provenance=false
@ -54,7 +71,13 @@ variables:
# Build multiplatform image to binary stage and extract their binaries:
- >
docker buildx build
--platform "linux/arm/v7,linux/arm64,linux/amd64"
--platform "linux/arm64,linux/amd64"
--pull
--cache-from="type=registry,ref=$CI_REGISTRY_IMAGE/$TAG"
--cache-from="type=registry,ref=$CI_REGISTRY_IMAGE/matrix-conduit:next"
--cache-from="type=registry,ref=$CI_REGISTRY_IMAGE/temporary-ci-images:buildkit-cache-$CI_COMMIT_REF_NAME"
--cache-from type=local,src=path/tmp/conduit-docker-buildcache
--ulimit nofile=2048
--target "builder-result"
--output="type=local,dest=/tmp/build-output"
--provenance=false
@ -102,7 +125,6 @@ docker:tags:
variables:
TAG: "matrix-conduit:$CI_COMMIT_TAG"
docker build debugging:
extends: .docker-shared-settings
rules:
@ -116,7 +138,7 @@ docker build debugging:
cargo check:
stage: test
image: docker.io/rust:1.70.0-bullseye
image: docker.io/rust:1.73.0-bullseye
needs: []
interruptible: true
before_script:

View file

@ -1,81 +1,79 @@
# syntax=docker/dockerfile:1
FROM docker.io/rust:1.70-bullseye AS base
# Platforms might be linux/amd64, linux/arm64
FROM base AS builder
WORKDIR /usr/src/conduit
FROM --platform=$BUILDPLATFORM docker.io/rust:1.75-bookworm AS builder
# Install required packages to build Conduit and it's dependencies
RUN apt-get update && \
apt-get -y --no-install-recommends install libclang-dev=1:11.0-51+nmu5
apt-get -y --no-install-recommends install libclang-dev
# == Build dependencies without our own code separately for caching ==
#
# Need a fake main.rs since Cargo refuses to build anything otherwise.
#
# See https://github.com/rust-lang/cargo/issues/2644 for a Cargo feature
# request that would allow just dependencies to be compiled, presumably
# regardless of whether source files are available.
RUN mkdir src && touch src/lib.rs && echo 'fn main() {}' > src/main.rs
COPY Cargo.toml Cargo.lock ./
RUN cargo build --release && rm -r src
# Install Zig
ARG ZIG_VERSION=0.11.0
RUN curl -L "https://ziglang.org/download/${ZIG_VERSION}/zig-linux-$(uname -m)-${ZIG_VERSION}.tar.xz" | tar -J -x -C /usr/local && \
ln -s "/usr/local/zig-linux-$(uname -m)-${ZIG_VERSION}/zig" /usr/local/bin/zig
ARG TARGETARCH
# Install zigbuild
RUN cargo install cargo-zigbuild
RUN <<EOL
case $TARGETARCH in \
(amd64) rustarch="x86_64-unknown-linux-gnu";; \
(arm64) rustarch="aarch64-unknown-linux-gnu";; \
(arm) rustarch="armv7-unknown-linux-gnueabihf";; \
esac
echo "$TARGETARCH"
rustup target add "$rustarch"
EOL
## # == Build dependencies without our own code separately for caching ==
## #
## # Need a fake main.rs since Cargo refuses to build anything otherwise.
## #
## # See https://github.com/rust-lang/cargo/issues/2644 for a Cargo feature
## # request that would allow just dependencies to be compiled, presumably
## # regardless of whether source files are available.
## COPY Cargo.toml Cargo.lock ./
##
## RUN <<EOL
## case $TARGETARCH in \
## (amd64) rustarch="x86_64-unknown-linux-gnu";; \
## (arm64) rustarch="aarch64-unknown-linux-gnu";; \
## (arm) rustarch="armv7-unknown-linux-gnueabihf";; \
## esac
##
## mkdir src && touch src/lib.rs && echo 'fn main() {}' > src/main.rs
## cargo zigbuild --target --release "$rustarch"
## rm -r src
## EOL
# Copy over actual Conduit sources
COPY Cargo.toml Cargo.lock ./
COPY src src
# main.rs and lib.rs need their timestamp updated for this to work correctly since
# otherwise the build with the fake main.rs from above is newer than the
# source files (COPY preserves timestamps).
#
# Builds conduit and places the binary at /usr/src/conduit/target/release/conduit
RUN touch src/main.rs && touch src/lib.rs && cargo build --release
# Builds conduit and places the binary at /conduit
RUN <<EOL
case $TARGETARCH in \
(amd64) rustarch="x86_64-unknown-linux-gnu";; \
(arm64) rustarch="aarch64-unknown-linux-gnu";; \
(arm) rustarch="armv7-unknown-linux-gnueabihf";; \
esac
# ONLY USEFUL FOR CI: target stage to extract build artifacts
FROM scratch AS builder-result
COPY --from=builder /usr/src/conduit/target/release/conduit /conduit
touch src/main.rs
touch src/lib.rs
cargo zigbuild --release --target "$rustarch"
mv "target/$rustarch/debug/conduit" /conduit
EOL
# ---------------------------------------------------------------------------------------------------------------
# Build cargo-deb, a tool to package up rust binaries into .deb packages for Debian/Ubuntu based systems:
# ---------------------------------------------------------------------------------------------------------------
FROM base AS build-cargo-deb
RUN apt-get update && \
apt-get install -y --no-install-recommends \
dpkg \
dpkg-dev \
liblzma-dev
RUN cargo install cargo-deb
# => binary is in /usr/local/cargo/bin/cargo-deb
# ---------------------------------------------------------------------------------------------------------------
# Package conduit build-result into a .deb package:
# ---------------------------------------------------------------------------------------------------------------
FROM builder AS packager
WORKDIR /usr/src/conduit
COPY ./LICENSE ./LICENSE
COPY ./README.md ./README.md
COPY debian ./debian
COPY --from=build-cargo-deb /usr/local/cargo/bin/cargo-deb /usr/local/cargo/bin/cargo-deb
# --no-build makes cargo-deb reuse already compiled project
RUN cargo deb --no-build
# => Package is in /usr/src/conduit/target/debian/<project_name>_<version>_<arch>.deb
# ONLY USEFUL FOR CI: target stage to extract build artifacts
FROM scratch AS packager-result
COPY --from=packager /usr/src/conduit/target/debian/*.deb /conduit.deb
# ---------------------------------------------------------------------------------------------------------------
# Stuff below this line actually ends up in the resulting docker image
# ---------------------------------------------------------------------------------------------------------------
FROM docker.io/debian:bullseye-slim AS runner
# On the target arch:
FROM docker.io/debian:bookworm-slim AS runtime
# Standard port on which Conduit launches.
# You still need to map the port when using the docker command or docker-compose.
@ -89,24 +87,19 @@ ENV CONDUIT_PORT=6167 \
CONDUIT_CONFIG=''
# └─> Set no config file to do all configuration with env vars
# Test if Conduit is still alive, uses the same endpoint as Element
COPY ./docker/healthcheck.sh /srv/conduit/healthcheck.sh
HEALTHCHECK --start-period=5s --interval=5s CMD ./healthcheck.sh
# Conduit needs:
# dpkg: to install conduit.deb
# ca-certificates: for https
# iproute2 & wget: for the healthcheck script
RUN apt-get update && apt-get -y --no-install-recommends install \
dpkg \
ca-certificates \
iproute2 \
wget \
&& rm -rf /var/lib/apt/lists/*
# Test if Conduit is still alive, uses the same endpoint as Element
COPY ./docker/healthcheck.sh /srv/conduit/healthcheck.sh
HEALTHCHECK --start-period=5s --interval=5s CMD ./healthcheck.sh
# Install conduit.deb:
COPY --from=packager /usr/src/conduit/target/debian/*.deb /srv/conduit/
RUN dpkg -i /srv/conduit/*.deb
# Improve security: Don't run stuff as root, that does not need to run as root
# Most distros also use 1000:1000 for the first real user, so this should resolve volume mounting problems.
@ -130,3 +123,10 @@ WORKDIR /srv/conduit
# Run Conduit and print backtraces on panics
ENV RUST_BACKTRACE=1
ENTRYPOINT [ "/usr/sbin/matrix-conduit" ]
FROM runtime AS final
# Actually copy over conduit binary
COPY --from=builder /conduit /usr/sbin/matrix-conduit