fej/build

56 lines
1.4 KiB
Plaintext
Raw Normal View History

2021-03-23 09:37:18 +01:00
#!/usr/bin/env bash
# Simple guard to check input args
[[ $# -eq 1 ]] || [[ $# -eq 2 ]] || {
>&2 echo "Usage: ./build IMAGE [ACTION]"
exit 1
}
# Extract current version from Cargo.toml & get current branch
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 branch --show-current)"
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
# Run the actual build command
2021-03-23 10:55:00 +01:00
docker build -t "$1:$tags" .
2021-03-23 09:37:18 +01:00
if [[ "$2" = 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
}
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 "$1:$tags" "$1:$tag"
# Push the tag
2021-03-23 09:37:18 +01:00
docker push "$1:$tag"
2021-03-23 10:55:00 +01:00
# Remove the tag again, if it's not the main tag
[[ "$tag" != "$tags" ]] && docker rmi "$1:$tag"
2021-03-23 09:37:18 +01:00
done
elif [[ "$2" = run ]]; then
docker run \
--rm \
--interactive \
--tty \
--publish 8000:8000 \
"$1:$tags"
fi