Files
minicalosim/docker/build.sh
T
2024-05-07 13:58:10 +02:00

69 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/bash
# a simple parser for the args <commit> and --no-cache
# if no commit is given, the latest commit is used
# if --no-cache is given, the --no-cache flag is passed to the docker build command
# if only --no-cache is given, the latest commit is used
# check if commit is given as argument, if not use the latest commit
# check if an argument is given at all
if [ -n "$1" ]; then
#check if the first arg is --no-cache
if [ "$1" == "--no-cache" ]; then
FORCE_NO_CACHE="--no-cache"
COMMIT=$(git rev-parse --short HEAD)
else
COMMIT=$1
#check if this is a valid commit
if ! git cat-file -e $COMMIT^{commit} 2>/dev/null; then
echo "ERROR: Commit $COMMIT not found in local repository."
exit 1
fi
fi
else #default to last commit
COMMIT=$(git rev-parse --short HEAD)
fi
# COMMIT is a valid commit at this point. If COMMIT is the last commit and changes are not commited, the user is warned.
#check if the commit is the last commit
if [ "$COMMIT" == "$(git rev-parse --short HEAD)" ]; then
# check if we have uncommitted changes and if so warn the user.
if [ -n "$(git status --porcelain)" ]; then
echo "WARNING: You are building the latest commit ${COMMIT}, but you have uncommitted changes."
echo " This means that the image will not be reproducible."
echo " If you want to build the latest commit, please commit your changes first."
read -p "Do you want to continue anyway? [y/N] " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
fi
# check if the commit has been pushed
REMOTE_COMMIT=$(git ls-remote https://gitlab.etp.kit.edu/jkiesele/minicalosim.git | grep $COMMIT)
if [ -z "$REMOTE_COMMIT" ]; then
echo "ERROR: Commit $COMMIT not found in remote repository."
exit 1
fi
# check if no-cache is given as second argument
if [ "$2" == "--no-cache" ]; then
FORCE_NO_CACHE="--no-cache"
fi
#switch to directory this script is located in
cd "$(dirname "$0")"
cd ../../ #switch to the root directory of the project
echo "Building minicalosim:$COMMIT"
#print the current working directory
echo "Current working directory: $(pwd)"
docker build $FORCE_NO_CACHE -t jkiesele/minicalosim:$COMMIT --build-arg USER=$USER --build-arg BUILD_DATE="$(date)" --build-arg COMMIT=$COMMIT -f minicalosim/docker/Dockerfile .
echo "sucessfully built container jkiesele/minicalosim:${COMMIT}"