diff --git a/.gitignore b/.gitignore index 0626c9d..1817e3c 100644 --- a/.gitignore +++ b/.gitignore @@ -23,7 +23,7 @@ var/ .installed.cfg *.egg **/_version.py - +.dockerignore # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. diff --git a/deployment/build_and_push.sh b/deployment/build_and_push.sh index e64654b..099bbd2 100755 --- a/deployment/build_and_push.sh +++ b/deployment/build_and_push.sh @@ -1,25 +1,56 @@ #!/bin/bash +TAG="dev" +PUSH=0 +RUN_CONTAINER=0 +REBUILD_CONTAINER=0 -DEV=0 -PUSH=1 +cd -- "$( dirname -- "$BASH_SOURCE[0]" )" +cd .. +# make dockerignore from .gitignore +if [ ! -f .gitignore ]; then + echo "No .gitignore file found, exiting." + exit 1 +fi +cp .gitignore .dockerignore for option in "$@"; do case $option in - -d|--dev) - DEV=1 + -t|--tag) + TAG="$2" + shift + echo "$TAG" + ;; + -p|--push) + PUSH=1 + shift + echo "Push the container to GCR." + ;; + -r|--run) + shift + echo "Run the container after building it." + RUN_CONTAINER=1 ;; - -n|--no-push) - PUSH=0 + -b|--rebuild) + shift + echo "Rebuild the container even if it already exists." + REBUILD_CONTAINER=1 + ;; - --help|--info|--h) - echo "Build and push the current repository state into containers and publish them to" - echo "gcr.io/diamond-privreg/daq-config-server/ ready for deployment." + --help|--info|-h) + echo "Build the current repository state as a container and optionally publish them to" + echo "gcr.io/diamond-privreg/daq-config-server/." echo " " - echo " -d, --dev Creates -dev:latest tagged containers for testing on argus." - echo " -n, --no-push Don't push containers to GCR." + echo " -t, --tag Specify the tag for the container. Default is 'dev'." + echo " -p, --push Push the container to GCR. Requires a GitHub token, followed by" + echo " podman login ghcr.io --username --password-stdin" + echo " -r, --run Run the container after building it." + echo " -b, --rebuild Rebuild the container even if it already exists." + echo " -h, --help, --info Show this help message." echo " " + shift exit 0 ;; + -*|--*) echo "Unknown option ${option}. Use --help for info on option usage." exit 1 @@ -30,33 +61,36 @@ done # set container and tag names: BASE_CONTAINER_NAME="daq-config-server" BASE_REPO_ADDR="gcr.io/diamond-privreg/daq-config-server/" -if [ $DEV -gt 0 ]; then - echo "Building dev-mode containers..." - MAIN_CONTAINER_NAME="${BASE_CONTAINER_NAME}-dev" -else - echo "Building prod-mode containers..." - MAIN_CONTAINER_NAME="${BASE_CONTAINER_NAME}" -fi -echo " " -MAIN_CONTAINER_TAG="${BASE_REPO_ADDR}${MAIN_CONTAINER_NAME}" -# set env vars which will be used by build process: -if [ $DEV -gt 0 ]; then - export REACT_APP_BACKEND_ADDR="http://localhost:8555" +MAIN_CONTAINER_NAME="${BASE_CONTAINER_NAME}" + + +MAIN_CONTAINER_TAG="${BASE_REPO_ADDR}${MAIN_CONTAINER_NAME}:${TAG}" + +if [ -z "$(podman images -q $MAIN_CONTAINER_NAME:$TAG 2> /dev/null)" ] || [ $REBUILD_CONTAINER -gt 0 ]; then + echo " " + echo "=========================================" + echo "==== Building ====" + echo "=========================================" + echo " " + echo "Building ${MAIN_CONTAINER_NAME}:${TAG}" + echo " " + podman build -t "${MAIN_CONTAINER_NAME}:${TAG}" . else - export REACT_APP_BACKEND_ADDR="https://daq-config.diamond.ac.uk/api" + echo "Local image found, using existing image." fi - -echo " " -echo "=========================================" -echo "==== Building and pushing main app ====" -echo "=========================================" -echo " " -echo "Building ${MAIN_CONTAINER_NAME}" -echo " " -podman build --build-arg -t $MAIN_CONTAINER_NAME . +rm .dockerignore if [ $PUSH -gt 0 ]; then podman tag $MAIN_CONTAINER_NAME $MAIN_CONTAINER_TAG - podman push $MAIN_CONTAINER_NAME $MAIN_CONTAINER_TAG + podman push $MAIN_CONTAINER_TAG $BASE_REPO_ADDR:$MAIN_CONTAINER_TAG +fi +if [ $RUN_CONTAINER -gt 0 ]; then + echo "Running container ${MAIN_CONTAINER_NAME}:${TAG}..." + # if the container is already running, stop it first + if [ -n "$(podman ps -q --filter "name=$MAIN_CONTAINER_NAME")" ] ; then + echo "Container $MAIN_CONTAINER_NAME is already running, stopping it first..." + podman stop $MAIN_CONTAINER_NAME + fi + podman run -d -v ./tests/test_data:/tests/test_data:z --replace --name $MAIN_CONTAINER_NAME -p 8555:8555 "${MAIN_CONTAINER_NAME}:${TAG}" fi