fej/build

92 lines
2.4 KiB
Plaintext
Raw Normal View History

2021-03-23 09:37:18 +01:00
#!/usr/bin/env bash
image="chewingbever/fej"
# Should be either dev or rel
mode="dev"
action=""
attach="--detach"
while getopts ":i:m:a:l" c; do
case $c in
i ) image="$OPTARG" ;;
m ) mode="$OPTARG" ;;
a ) action="$OPTARG" ;;
l ) attach="" ;;
? ) exit 1 ;;
esac
done
shift $((OPTIND-1))
2021-03-23 09:37:18 +01:00
# Extract current version from Cargo.toml & get current branch
2021-04-12 18:19:22 +02:00
patch_version=`grep -Po '(?<=version = ").*(?=")' Cargo.toml | head -n1`
major_version=`echo "$patch_version" | sed -E 's/([0-9]+)\.([0-9]+)\.([0-9]+)/\1/'`
minor_version=`echo "$patch_version" | sed -E 's/([0-9]+).([0-9]+).([0-9]+)/\1.\2/'`
branch=`git rev-parse --abbrev-ref HEAD`
2021-03-23 09:37:18 +01:00
if [[ "$branch" = "master" ]]; then
2021-03-23 10:55:00 +01:00
tags=("$patch_version" "$minor_version" "$major_version" "latest")
2021-03-23 09:37:18 +01:00
elif [[ "$branch" = "develop" ]]; then
2021-03-23 10:55:00 +01:00
tags=("$patch_version-dev" "$minor_version-dev" "$major_version-dev" "dev")
2021-03-23 09:37:18 +01:00
else
tags=("$branch")
fi
# First, we build the builder
DOCKER_BUILDKIT=1 docker build -f docker/Dockerfile.builder -t "$image-builder:latest" .
2021-03-23 09:37:18 +01:00
# Run the actual build command
if [ "$mode" = "rel" ]; then
DOCKER_BUILDKIT=1 docker build -t "$image:$tags" -f docker/Dockerfile.rel .
elif [[ "$mode" = "dev" ]]; then
DOCKER_BUILDKIT=1 docker build -t "$image-dev:$tags" -f docker/Dockerfile.dev .
else
>&2 echo "Invalid mode."
exit 1
2021-03-23 09:37:18 +01:00
fi
if [[ "$action" = push ]]; then
2021-03-23 10:42:20 +01:00
[[ "$branch" =~ ^develop|master$ ]] || {
>&2 echo "You can only push from develop or master."
exit 2
}
[[ "$mode" = "rel" ]] || {
>&2 echo "You can only push release builds."
exit 3
}
2021-03-23 09:37:18 +01:00
for tag in "${tags[@]}"; do
2021-03-23 10:55:00 +01:00
# Create the tag
docker tag "$image:$tags" "$image:$tag"
2021-03-23 10:55:00 +01:00
# Push the tag
docker push "$image:$tag"
2021-03-23 10:55:00 +01:00
# Remove the tag again, if it's not the main tag
[[ "$tag" != "$tags" ]] && docker rmi "$image:$tag"
2021-03-23 09:37:18 +01:00
done
elif [[ "$action" = run ]]; then
if [[ "$mode" = "dev" ]]; then
# Create caching volumes if needed (they need to be named)
docker volume create fej_build-cache
docker volume create fej_registry-cache
flags="-v fej_build-cache:/usr/src/app/target -v fej_registry-cache:/root/.cargo/registry"
fi
docker run $attach $flags \
2021-03-23 09:37:18 +01:00
--rm \
--interactive \
--tty \
--publish 8000:8000 \
--name fej \
"$image$([[ "$mode" != "rel" ]] && echo "-dev"):$tags" "$@"
2021-03-23 09:37:18 +01:00
fi