#!/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)) # 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 rev-parse --abbrev-ref HEAD` if [[ "$branch" = "master" ]]; then tags=("$patch_version" "$minor_version" "$major_version" "latest") elif [[ "$branch" = "develop" ]]; then tags=("$patch_version-dev" "$minor_version-dev" "$major_version-dev" "dev") else tags=("$branch") fi # First, we build the builder DOCKER_BUILDKIT=1 docker build -f docker/Dockerfile.builder -t "$image-builder:latest" . # 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 fi if [[ "$action" = push ]]; then [[ "$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 } for tag in "${tags[@]}"; do # Create the tag docker tag "$image:$tags" "$image:$tag" # Push the tag docker push "$image:$tag" # Remove the tag again, if it's not the main tag [[ "$tag" != "$tags" ]] && docker rmi "$image:$tag" done elif [[ "$action" = run ]]; then # Create the network & start the database container docker network create fej docker volume create fej_db-data docker run --rm \ --detach \ --name fej_db \ --network fej \ -e "POSTGRES_DB=fej" \ -e "POSTGRES_USER=fej" \ -e "POSTGRES_PASSWORD=fej" \ -v 'fej_db-data:/var/lib/postgresql/data' \ postgres:13-alpine 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 \ --rm \ --interactive \ --tty \ --publish 8000:8000 \ --name fej \ --env-file .env \ --network fej \ "$image$([[ "$mode" != "rel" ]] && echo "-dev"):$tags" "$@" fi