1
0

192 lines
5.7 KiB
Bash
Executable File

#!/bin/bash
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Name: build.sh
# Author(s): Thomas Belway
# Source: https://gitea.belway.me/Public/foundryvtt_container.git
# Example: ./build.sh -U $FOUNDRY_URL -I -v $VERSION
# Description: Builds custom foundryvtt container and pushes to registry.
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
####################################################################
# Variables
####################################################################
# General Variables
REGISTRY="registry.belway.me"
REGISTRY_NAMESPACE="public/foundryvtt_container"
###############################################################################################################################
# Functions
###############################################################################################################################
# Informational Functions
usage() {
cat << EOF
usage: ${SCRIPT_NAME} [options]
This script exports data from one database to be compared with data from another database
OPTIONS:
-D Enable DEBUG mode
-f Custom Containerfile with absolute or relative directory
-F Format of containerfile
-h Show this message
-I Initialize the registry
-n Node version
-R For rebuild the image.
-S Scheduled build, should only be used by automation (gitea actions).
-U Foundry URL for downloading the version
-v Version to build, defaults to 'latest'
EOF
exit 1
}
get_parameters() {
while getopts "Df:F:hIRSU:v:" option_name; do
case "$option_name" in
"D")
DEBUG="YES"
;;
"f")
OPT_CONTAINERFILE="$OPTARG"
;;
"F")
OPT_FORMAT_TYPE="$OPTARG"
;;
"h")
usage
;;
"I")
INITIALIZE="TRUE"
;;
"n")
NODEJS_VERSION="$OPTARG"
;;
"R")
OPT_REBUILD="TRUE"
;;
"S")
OPT_SCHEDULED="TRUE"
;;
"U")
FOUNDRY_URL="$OPTARG"
;;
"v")
OPT_VERSION="$OPTARG"
;;
"?")
echo "Error: Unknown option $OPTARG"
usage
;;
":")
echo "Error: No argument value for option $OPTARG"
usage
;;
*)
echo "Error: Unknown error while processing options"
usage
;;
esac
done
if [[ -z $FOUNDRY_URL ]]; then
echo "Need the foundry URL, please try again..."
exit 1
fi
if [[ -z $OPT_CONTAINERFILE ]]; then
OPT_CONTAINERFILE="Containerfile"
fi
if [[ -z $OPT_FORMAT_TYPE ]]; then
OPT_FORMAT="--format docker"
elif [[ $OPT_FORMAT_TYPE == "podman" ]]; then
OPT_FORMAT=""
else
OPT_FORMAT="--format ${OPT_FORMAT_TYPE}"
fi
if [[ $DEBUG == "YES" ]]; then
SILENCE_OUTPUT=""
SILENCE_STOUT=" "
echo "Debug mode enabled."
fi
if [[ -z $OPT_REBUILD ]]; then
OPT_REBUILD="FALSE"
fi
if [[ -z $OPT_SCHEDULED ]]; then
OPT_SCHEDULED="FALSE"
fi
if [[ -z $OPT_VERSION ]]; then
OPT_VERSION="12.331"
fi
if [[ "${OPT_SCHEDULED}" == "TRUE" ]]; then
OPT_REBUILD="FALSE"
fi
if [[ -z $NODEJS_VERSION ]]; then
NODEJS_VERSION=$(curl https://foundryvtt.com/article/installation/ | grep nodesource | grep rpm | tr -d -c 0-9)
fi
TAG="$OPT_VERSION"
}
# Build Functions
container_initialize() {
echo "Initializing container build..."
podman login ${REGISTRY}
podman build -f ${OPT_CONTAINERFILE} -t ${REGISTRY}/${REGISTRY_NAMESPACE}:${TAG} -t ${REGISTRY}/${REGISTRY_NAMESPACE}:latest --build-arg foundry_url=${FOUNDRY_URL} --build-arg nodejs_version=${NODEJS_VERSION}
podman push ${REGISTRY}/${REGISTRY_NAMESPACE}:${TAG}
podman push ${REGISTRY}/${REGISTRY_NAMESPACE}:latest
podman logout ${REGISTRY}
echo "Container build initialized!"
}
container_scheduled_build() {
echoo "Scheduled build starting..."
if [[ "${CADDY_IMAGE_VERSION}" != "${CUSTOM_CADDY_IMAGE_VERSION}" ]]; then
podman login ${REGISTRY}
podman build -f ${OPT_CONTAINERFILE} -t ${REGISTRY}/${REGISTRY_NAMESPACE}:${TAG} -t ${REGISTRY}/${REGISTRY_NAMESPACE}:latest --build-arg foundry_url=${FOUNDRY_URL}
podman push ${REGISTRY}/${REGISTRY_NAMESPACE}:${TAG}
podman push ${REGISTRY}/${REGISTRY_NAMESPACE}:latest
podman logout ${REGISTRY}
else
echo "No new version, exiting..."
exit 1
fi
echo "Scheduled build complete!"
}
container_rebuild() {
echo "Rebuilding container..."
podman login ${REGISTRY}
podman build -f ${OPT_CONTAINERFILE} -t ${REGISTRY}/${REGISTRY_NAMESPACE}:${TAG} -t ${REGISTRY}/${REGISTRY_NAMESPACE}:latest --build-arg foundry_url=${FOUNDRY_URL}
podman push ${REGISTRY}/${REGISTRY_NAMESPACE}:${TAG}
podman push ${REGISTRY}/${REGISTRY_NAMESPACE}:latest
podman logout ${REGISTRY}
echo "Rebuild complete!"
}
###############################################################################################################################
# Main Script
###############################################################################################################################
get_parameters "$@"
if [[ "$OPT_SCHEDULED" == "TRUE" ]]; then
container_scheduled_build
fi
if [[ "$OPT_REBUILD" == "TRUE" ]]; then
container_rebuild
fi
if [[ "$INITIALIZE" == "TRUE" ]]; then
container_initialize
fi