2023-01-19 22:37:17 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
print_usage()
|
|
|
|
{
|
2024-02-22 15:36:20 +00:00
|
|
|
echo "USAGE: $0 <slim|full|zig> [rust version] [cross version] [zig version]"
|
2023-01-19 22:37:17 +00:00
|
|
|
echo
|
|
|
|
echo "Arguments:"
|
|
|
|
echo -e "\t<slim|full>\tSlim build (without targets) or Full build (with targets)"
|
2024-02-22 15:36:20 +00:00
|
|
|
echo
|
2023-01-19 22:37:17 +00:00
|
|
|
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)"
|
2024-02-22 15:36:20 +00:00
|
|
|
echo -e "\t[zig version]\tVersion of zig (https://ziglang.org/download)"
|
|
|
|
echo -e "\t\t\tUse 'master' for master version of zig"
|
2023-01-19 22:37:17 +00:00
|
|
|
echo
|
2024-02-22 15:36:20 +00:00
|
|
|
echo "All versions default to the latest release."
|
2023-01-19 22:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-02-22 15:36:20 +00:00
|
|
|
get_version() {
|
|
|
|
case $2 in
|
|
|
|
latest | "")
|
|
|
|
echo $(get_latest_release $1)
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo $2
|
|
|
|
;;
|
|
|
|
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2023-01-19 22:37:17 +00:00
|
|
|
|
|
|
|
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
|
2024-02-22 15:36:20 +00:00
|
|
|
full | slim | zig)
|
2023-01-19 22:37:17 +00:00
|
|
|
;;
|
|
|
|
|
|
|
|
*)
|
|
|
|
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
|
2024-02-22 15:36:20 +00:00
|
|
|
RUST_VERSION=$(get_version 'rust-lang/rust' $2)
|
2023-01-19 22:37:17 +00:00
|
|
|
else
|
|
|
|
RUST_VERSION=$2
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$3" ]; then
|
|
|
|
CROSS_VERSION=$(get_latest_release 'cross-rs/cross')
|
|
|
|
else
|
|
|
|
CROSS_VERSION=$3
|
|
|
|
fi
|
|
|
|
|
2024-02-22 15:36:20 +00:00
|
|
|
ZIG_VERSION=$(get_latest_release 'ziglang/zig')
|
2023-01-19 22:37:17 +00:00
|
|
|
|
|
|
|
export RUST_VERSION
|
|
|
|
export CROSS_VERSION
|
2024-02-22 15:36:20 +00:00
|
|
|
export ZIG_VERSION
|
2023-01-19 22:37:17 +00:00
|
|
|
|
2024-02-22 15:36:20 +00:00
|
|
|
echo "Creating a ${1} build with the following versions:"
|
|
|
|
echo -e "\tRust:\t${RUST_VERSION}"
|
|
|
|
echo -e "\tcross:\t${CROSS_VERSION}"
|
|
|
|
echo -e "\tzig:\t${ZIG_VERSION}"
|
|
|
|
|
|
|
|
case $1 in
|
|
|
|
full | slim)
|
|
|
|
IMAGE="img.kie.rs/jjkiers/rust-crossbuild:rust${RUST_VERSION}-cross${CROSS_VERSION}-${1}"
|
|
|
|
;;
|
|
|
|
zig)
|
|
|
|
IMAGE="img.kie.rs/jjkiers/rust-crossbuild:rust${RUST_VERSION}-zig${ZIG_VERSION}-${1}"
|
|
|
|
;;
|
|
|
|
esac
|
2023-01-19 22:37:17 +00:00
|
|
|
|
|
|
|
envsubst < Dockerfile-${1} | ${BUILDER} build -f - \
|
|
|
|
-t ${IMAGE} \
|
|
|
|
.
|
|
|
|
|
|
|
|
echo "Built image ${IMAGE}"
|
|
|
|
|
2024-02-22 15:36:20 +00:00
|
|
|
exit 0
|
|
|
|
|
2023-01-19 22:37:17 +00:00
|
|
|
${BUILDER} push ${IMAGE}
|
|
|
|
|
2024-02-22 15:36:20 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "Pushing failed: ${BUILDER} push ${IMAGE}"
|
|
|
|
else
|
|
|
|
echo "Pushed image ${IMAGE}"
|
|
|
|
fi
|