You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

149 lines
3.8 KiB

#!/bin/bash -e
cd "$(dirname "$(realpath "$0")")"
if command -v podman &>/dev/null; then
export BUILDAH_FORMAT=docker
runtime=podman
elif command -v docker &>/dev/null; then
if ! docker ps &>/dev/null; then
echo "No privileges with Docker." >&2
exit 1
fi
runtime=docker
else
echo "No container runtime detected." >&2
exit 2
fi
DEFAULT_REGISTRY=harbor.jharmison.com
DEFAULT_REPOSITORY=akaunting
DEFAULT_IMAGE=akaunting
print_usage() {
prog=$(basename $0)
echo "usage: $prog [-h|--help] | [-r|--registry REGISTRY] [-R|--repository REPO]"
echo " [-i|--image IMAGE] [-b|--build-tag TAG] [-t|--tag TAG] [-d|--dev] [-p|--publish]"
}
print_help() {
print_usage
cat << EOF
OPTIONS:
-h|--help Print this help page and exit
-r|--registry Set the registry to use (default: $DEFAULT_REGISTRY)
-R|--repostory Set the repository to use (default: $DEFAULT_REPOSITORY)
-i|--image Set the image name to use (default: $DEFAULT_IMAGE)
-b|--build-tag Set the default tag name to use (default: <git branch>)
-t|--tag Additional tags for image (default: [latest])
-d|--dev Append every tag built with "-dev"
-p|--publish Push all built tags
EXAMPLES:
# To build only tag :dev
$prog -b dev -t "" # note that \`-t ""\` is required to not tag latest
# To build this branch and push to the default registry/repo/image:tag
$prog -p
# To build a development release without overwriting the release image
$prog -d
# note that for convenience, you can assume this to build :latest-dev
# To build and push to a local docker v2 registry
$prog --registry localhost:5000 --publish
# To build, including a tag for the version of Akaunting
akaunting_release=\$(curl -sH "Accept: application/vnd.github.v3+json" \\
https://api.github.com/repos/akaunting/akaunting/releases/latest \\
| jq -r '.tag_name')
$prog --tag \$akaunting_release
# note that this will _NOT_ tag :latest
EOF
}
tags=()
registry=""
repository=""
image=""
build_tag=""
tag_suffix=""
do_push=""
while [ $# -gt 0 ]; do
case "$1" in
-r|--registry)
shift
registry="$1"
;;
-R|--repository)
shift
repository="$1"
;;
-i|--image)
shift
image="$1"
;;
-b|--build-tag)
shift
build_tag="$1"
;;
-t|--tag)
shift
tags+=("$1")
;;
-d|--dev)
tag_suffix="-dev"
;;
-p|--publish)
do_push=true
;;
-h|--help)
print_help
exit 0
;;
*)
print_usage >&2
exit 1
;;
esac; shift
done
this_branch=$(git branch --show-current) || this_branch=none
this_branch_clean=$(echo "$this_branch" | tr '/' '_')
this_commit=$(git rev-parse HEAD) || this_commit=unknown
build_date=$(date -Iseconds)
registry="${registry:-$DEFAULT_REGISTRY}"
repository="${repository:-$DEFAULT_REPOSITORY}"
image="${image:-$DEFAULT_IMAGE}"
build_tag="${build_tag:-$this_branch_clean}"
if [ ${#tags[@]} -eq 0 ]; then
tags=(latest)
fi
akaunting_build_img="${registry}/${repository}/${image}"
akaunting_build_tag="${akaunting_build_img}:${build_tag}${tag_suffix}"
if [ "$do_push" ]; then
$runtime login "$registry"
fi
$runtime build \
--build-arg GIT_BRANCH="$this_branch" \
--build-arg GIT_COMMIT="$this_commit" \
. -t "$akaunting_build_tag"
if [ "$do_push" ]; then
$runtime push "$akaunting_build_tag"
fi
for tag in "${tags[@]}"; do
if [ "$tag" ]; then
new_tag="${akaunting_build_img}:${tag}${tag_suffix}"
$runtime tag "$akaunting_build_tag" "$new_tag"
if [ "$do_push" ]; then
$runtime push "$new_tag"
fi
fi
done