From 678ce0abc6932630fb97409d9ef7aeaff6b97d80 Mon Sep 17 00:00:00 2001 From: Jonas Zohren Date: Mon, 19 Jul 2021 17:18:25 +0200 Subject: [PATCH 1/3] CI: Create docker image with musl binary --- .gitlab-ci.yml | 41 ++++++++++++++ docker/ci-binaries-packaging.Dockerfile | 73 +++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 docker/ci-binaries-packaging.Dockerfile diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 0924ee3..60b8833 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,5 +1,6 @@ stages: - build + - build docker image - test - upload artifacts @@ -212,6 +213,46 @@ build:cargo-deb:x86_64-unknown-linux-gnu: +# --------------------------------------------------------------------- # +# Create and publish docker image # +# --------------------------------------------------------------------- # + +.docker-shared-settings: + stage: "build docker image" + needs: [] + interruptible: true + image: + name: "gcr.io/kaniko-project/executor:debug" + entrypoint: [""] + tags: ["docker"] + variables: + # Configure Kaniko Caching: https://cloud.google.com/build/docs/kaniko-cache + KANIKO_CACHE_ARGS: "--cache=true --cache-copy-layers=true --cache-ttl=120h --cache-repo $CI_REGISTRY_IMAGE/kaniko-ci-cache" + before_script: + - "mkdir -p /kaniko/.docker" + - 'echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json' + + +# Build a docker image by packaging up the x86_64-unknown-linux-musl binary into an alpine image +build:docker:main: + extends: .docker-shared-settings + needs: + - "build:cargo:x86_64-unknown-linux-musl" + script: + - > + /kaniko/executor + $KANIKO_CACHE_ARGS + --context $CI_PROJECT_DIR + --build-arg CREATED=$(date -u +'%Y-%m-%dT%H:%M:%SZ') + --build-arg VERSION=$(grep -m1 -o '[0-9].[0-9].[0-9]' Cargo.toml) + --build-arg "GIT_REF=$CI_COMMIT_REF_NAME" + --dockerfile "$CI_PROJECT_DIR/docker/ci-binaries-packaging.Dockerfile" + --destination "$CI_REGISTRY_IMAGE/conduit:latest" + --destination "$CI_REGISTRY_IMAGE/conduit:alpine" + --destination "$CI_REGISTRY_IMAGE/conduit:commit-$CI_COMMIT_SHORT_SHA" + rules: + - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH' + # --------------------------------------------------------------------- # # Store binaries as package so they have download urls # diff --git a/docker/ci-binaries-packaging.Dockerfile b/docker/ci-binaries-packaging.Dockerfile new file mode 100644 index 0000000..0122797 --- /dev/null +++ b/docker/ci-binaries-packaging.Dockerfile @@ -0,0 +1,73 @@ +# --------------------------------------------------------------------------------------------------------- +# This Dockerfile is intended to be built as part of Conduit's CI pipeline. +# It does not build Conduit in Docker, but just copies the matching build artifact from the build job. +# As a consequence, this is not a multiarch capable image. It always expects and packages a x86_64 binary. +# +# It is mostly based on the normal Conduit Dockerfile, but adjusted in a few places to maximise caching. +# Credit's for the original Dockerfile: Weasy666. +# --------------------------------------------------------------------------------------------------------- + +FROM alpine:3.12 + +ARG CREATED +ARG VERSION +ARG GIT_REF + +ENV CONDUIT_CONFIG="/srv/conduit/conduit.toml" + +# Labels according to https://github.com/opencontainers/image-spec/blob/master/annotations.md +# including a custom label specifying the build command +LABEL org.opencontainers.image.created=${CREATED} \ + org.opencontainers.image.authors="Conduit Contributors" \ + org.opencontainers.image.title="Conduit" \ + org.opencontainers.image.version=${VERSION} \ + org.opencontainers.image.vendor="Conduit Contributors" \ + org.opencontainers.image.description="A Matrix homeserver written in Rust" \ + org.opencontainers.image.url="https://conduit.rs/" \ + org.opencontainers.image.revision=${GIT_REF} \ + org.opencontainers.image.source="https://gitlab.com/famedly/conduit.git" \ + org.opencontainers.image.licenses="Apache-2.0" \ + org.opencontainers.image.documentation="" \ + org.opencontainers.image.ref.name="" + +# Standard port on which Conduit launches. You still need to map the port when using the docker command or docker-compose. +EXPOSE 6167 + +# create data folder for database +RUN mkdir -p /srv/conduit/.local/share/conduit + +# Add www-data user and group with UID 82, as used by alpine +# https://git.alpinelinux.org/aports/tree/main/nginx/nginx.pre-install +RUN set -x ; \ + addgroup -Sg 82 www-data 2>/dev/null ; \ + adduser -S -D -H -h /srv/conduit -G www-data -g www-data www-data 2>/dev/null ; \ + addgroup www-data www-data 2>/dev/null && exit 0 ; exit 1 + +# Change ownership of Conduit files to www-data user and group +RUN chown -cR www-data:www-data /srv/conduit + +# Install packages needed to run Conduit +RUN apk add --no-cache \ + ca-certificates \ + curl \ + libgcc + +# Create a volume for the database, to persist its contents +VOLUME ["/srv/conduit/.local/share/conduit"] + +# Test if Conduit is still alive, uses the same endpoint as Element +HEALTHCHECK --start-period=5s \ + CMD curl --fail -s "http://localhost:$(grep -m1 -o 'port\s=\s[0-9]*' conduit.toml | grep -m1 -o '[0-9]*')/_matrix/client/versions" || \ + curl -k --fail -s "https://localhost:$(grep -m1 -o 'port\s=\s[0-9]*' conduit.toml | grep -m1 -o '[0-9]*')/_matrix/client/versions" || \ + exit 1 + +# Set user to www-data +USER www-data +# Set container home directory +WORKDIR /srv/conduit +# Run Conduit +ENTRYPOINT [ "/srv/conduit/conduit" ] + + +# Copy the COnduit binary into the image at the latest possible moment to maximise caching: +COPY ./conduit-x86_64-unknown-linux-musl /srv/conduit/conduit From 0a8dadb79799450952ba18b0d3c1d2df3ef1d95c Mon Sep 17 00:00:00 2001 From: Jonas Zohren Date: Mon, 19 Jul 2021 19:42:25 +0200 Subject: [PATCH 2/3] Add hardcoded artifacts.expose_as to show them in MRs --- .gitlab-ci.yml | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 60b8833..6f9b78b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -121,11 +121,6 @@ test:register:element-web-stable: - "export CARGO_PROFILE_RELEASE_LTO=thin" - time cargo build --target $TARGET --release - 'mv "target/$TARGET/release/conduit" "conduit-$TARGET"' - artifacts: - name: "conduit-$TARGET" - expose_as: "Binary" - paths: - - "conduit-$TARGET" build:cargo:x86_64-unknown-linux-gnu: extends: .build-cargo-shared-settings @@ -133,6 +128,11 @@ build:cargo:x86_64-unknown-linux-gnu: TARGET: "x86_64-unknown-linux-gnu" rules: - if: "$CI_COMMIT_BRANCH" + artifacts: + name: "conduit-x86_64-unknown-linux-gnu" + paths: + - "conduit-x86_64-unknown-linux-gnu" + expose_as: "Release binary x86_64-unknown-linux-gnu" build:cargo:armv7-unknown-linux-gnueabihf: extends: .build-cargo-shared-settings @@ -142,6 +142,11 @@ build:cargo:armv7-unknown-linux-gnueabihf: CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER: arm-linux-gnueabihf-gcc CC_armv7_unknown_linux_gnueabihf: arm-linux-gnueabihf-gcc CXX_armv7_unknown_linux_gnueabihf: arm-linux-gnueabihf-g++ + artifacts: + name: "conduit-armv7-unknown-linux-gnueabihf" + paths: + - "conduit-armv7-unknown-linux-gnueabihf" + expose_as: "Release binary armv7-unknown-linux-gnueabihf" build:cargo:aarch64-unknown-linux-gnu: extends: .build-cargo-shared-settings @@ -153,6 +158,11 @@ build:cargo:aarch64-unknown-linux-gnu: CXX_aarch64_unknown_linux_gnu: aarch64-linux-gnu-g++ TARGET_CC: "/usr/bin/aarch64-linux-gnu-gcc-8" TARGET_AR: "/usr/bin/aarch64-linux-gnu-gcc-ar-8" + artifacts: + name: "conduit-aarch64-unknown-linux-gnu" + paths: + - "conduit-aarch64-unknown-linux-gnu" + expose_as: "Release binary aarch64-unknown-linux-gnu" build:cargo:x86_64-unknown-linux-musl: extends: .build-cargo-shared-settings @@ -167,6 +177,11 @@ build:cargo:x86_64-unknown-linux-musl: - "cat /etc/*-release && rustc --version && cargo --version" # Print version info for debugging - "rustup target add $TARGET" - "apk add libc-dev" + artifacts: + name: "conduit-x86_64-unknown-linux-musl" + paths: + - "conduit-x86_64-unknown-linux-musl" + expose_as: "Release binary x86_64-unknown-linux-musl" # --------------------------------------------------------------------- # @@ -199,17 +214,17 @@ build:cargo:x86_64-unknown-linux-musl: script: - time cargo deb --target $TARGET - 'mv target/$TARGET/debian/*.deb "conduit-$TARGET.deb"' - artifacts: - name: "conduit-$TARGET.deb" - expose_as: "Debian Package" - paths: - - "conduit-$TARGET.deb" build:cargo-deb:x86_64-unknown-linux-gnu: extends: .build-cargo-deb-shared-settings variables: TARGET: "x86_64-unknown-linux-gnu" NEEDED_PACKAGES: "" + artifacts: + name: "conduit-x86_64-unknown-linux-gnu.deb" + paths: + - "conduit-x86_64-unknown-linux-gnu.deb" + expose_as: "Debian Package x86_64" From 167e903a56f614e4f161bbe171a3e41a1fc70df0 Mon Sep 17 00:00:00 2001 From: Jonas Zohren Date: Mon, 19 Jul 2021 22:02:10 +0200 Subject: [PATCH 3/3] Fix typo --- docker/ci-binaries-packaging.Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/ci-binaries-packaging.Dockerfile b/docker/ci-binaries-packaging.Dockerfile index 0122797..43ebc98 100644 --- a/docker/ci-binaries-packaging.Dockerfile +++ b/docker/ci-binaries-packaging.Dockerfile @@ -69,5 +69,5 @@ WORKDIR /srv/conduit ENTRYPOINT [ "/srv/conduit/conduit" ] -# Copy the COnduit binary into the image at the latest possible moment to maximise caching: +# Copy the Conduit binary into the image at the latest possible moment to maximise caching: COPY ./conduit-x86_64-unknown-linux-musl /srv/conduit/conduit