diff --git a/Dockerfile-full b/Dockerfile-full new file mode 100644 index 0000000..7873482 --- /dev/null +++ b/Dockerfile-full @@ -0,0 +1,31 @@ +FROM docker.io/library/rust:${RUST_VERSION}-slim +LABEL maintainer 'Jacob Kiers ' + +# Install docker +RUN apt-get update && \ + apt-get install -y ca-certificates curl gnupg && \ + mkdir -p /etc/apt/keyrings && \ + curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg && \ + echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian bullseye stable" >> /etc/apt/sources.list.d/docker.list && \ + apt-get update && apt-get install -y docker-ce-cli && \ + apt-get clean + + +# Install cross +RUN mkdir -p /usr/local/cargo/bin && curl -L \ + https://github.com/cross-rs/cross/releases/download/v${CROSS_VERSION}/cross-x86_64-unknown-linux-gnu.tar.gz \ + | tar -xz -C /usr/local/cargo/bin + + +# Install rust targets +RUN rustup target add \ + aarch64-unknown-linux-gnu \ + aarch64-unknown-linux-musl \ + x86_64-pc-windows-gnu \ + x86_64-unknown-linux-gnu \ + x86_64-unknown-linux-musl && \ + rustup component add --target aarch64-unknown-linux-gnu rust-src && \ + rustup component add --target aarch64-unknown-linux-musl rust-src && \ + rustup component add --target x86_64-pc-windows-gnu rust-src && \ + rustup component add --target x86_64-unknown-linux-gnu rust-src && \ + rustup component add --target x86_64-unknown-linux-musl rust-src diff --git a/Dockerfile-slim b/Dockerfile-slim new file mode 100644 index 0000000..41f49c1 --- /dev/null +++ b/Dockerfile-slim @@ -0,0 +1,18 @@ +FROM docker.io/library/rust:${RUST_VERSION}-slim +LABEL maintainer 'Jacob Kiers ' + +# Install docker +RUN apt-get update && \ + apt-get install -y ca-certificates curl gnupg && \ + mkdir -p /etc/apt/keyrings && \ + curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg && \ + echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian bullseye stable" >> /etc/apt/sources.list.d/docker.list && \ + apt-get update && apt-get install -y docker-ce-cli && \ + apt-get clean + + +# Install cross +RUN mkdir -p /usr/local/cargo/bin && curl -L \ + https://github.com/cross-rs/cross/releases/download/v${CROSS_VERSION}/cross-x86_64-unknown-linux-gnu.tar.gz \ + | tar -xz -C /usr/local/cargo/bin + diff --git a/README.md b/README.md index e7b0bb9..eb5896e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,25 @@ # toolchain-builder -Toolchain builder for Rust cross compilation \ No newline at end of file +Toolchain builder for Rust cross compilation. + +This project build the toolchain used for [newsletter2web][1]. + +By default, it uses the latest [Rust][2] and [`cross`][3] versions. + +## Usage + +``` +USAGE: ./build.sh [rust version] [cross version] + +Arguments: + Slim build (without targets) or Full build (with targets) + [rust version] Version of rust (https://github.com/rust-lang/rust) + [cross version] Version of cross (https://github.com/cross-rs/cross) + +Both versions default to the latest version. +``` + +[1]: https://code.kiers.eu/newsletter-to-web/newsletter-to-web +[2]: https://www.rust-lang.org/ +[3]: https://github.com/cross-rs/cross + diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..a75b751 --- /dev/null +++ b/build.sh @@ -0,0 +1,85 @@ +#!/usr/bin/env bash + +print_usage() +{ + echo "USAGE: $0 [rust version] [cross version]" + echo + echo "Arguments:" + echo -e "\t\tSlim build (without targets) or Full build (with targets)" + echo -e "\t[rust version]\tVersion of rust (https://github.com/rust-lang/rust)" + echo -e "\t[cross version]\tVersion of cross (https://github.com/cross-rs/cross)" + echo + echo "Both versions default to the latest version." +} + +get_latest_release() { + curl --silent "https://api.github.com/repos/$1/releases/latest" | # Get latest release from GitHub api + grep '"tag_name":' | # Get tag line + sed -E 's/.*"v?([^"]+)".*/\1/' # Pluck JSON value +} + + +if command -v podman &> /dev/null +then + BUILDER=podman +elif command -v docker &> /dev/null +then + BUILDER=docker +else + echo "Prerequisite failed: either podman or docker must be installed" + exit 1 +fi + +if ! command -v envsubst &> /dev/null +then + echo "Prerequisite failed: envsubst must be installed" + exit 1 +fi + +if [ $# -lt 1 ]; then + print_usage + exit 1 +fi + +case ${1} in + full | slim) + ;; + + *) + echo "ERROR: The first argument mus either be 'full' or 'slim'." + echo + print_usage + exit 1 + ;; +esac + +set -e -o pipefail + +if [ -z "$2" ]; then + RUST_VERSION=$(get_latest_release 'rust-lang/rust') +else + RUST_VERSION=$2 +fi + +if [ -z "$3" ]; then + CROSS_VERSION=$(get_latest_release 'cross-rs/cross') +else + CROSS_VERSION=$3 +fi + +echo "Creating a ${1} build with Rust ${RUST_VERSION} and cross ${CROSS_VERSION}" + +export RUST_VERSION +export CROSS_VERSION + +IMAGE="img.kie.rs/jjkiers/rust-dind-cross:rust${RUST_VERSION}-cross${CROSS_VERSION}-${1}" + +envsubst < Dockerfile-${1} | ${BUILDER} build -f - \ + -t ${IMAGE} \ + . + +echo "Built image ${IMAGE}" + +${BUILDER} push ${IMAGE} + +echo "Pushed image ${IMAGE}"