#!/bin/sh # Copyright (C) 2019 Richard Mortier # Licensed under the MIT License, https://opensource.org/licenses/MIT set -e die () { printf "%s\n" "$*" >&2 exit 1 } ## debug if [ "$DABUILD_DEBUG" = "true" ]; then set -x PS4='$LINENO: ' fi ## check running from within an `aports` tree if [ "${PWD%*/aports*}" = "$PWD" ]; then die "Error: expecting to be run from within an aports tree!" \ "Could not find '/aports' in the current path ($PWD)" exit 1 fi ## allow setting of arch by env variable [ ! "$DABUILD_ARCH" ] && DABUILD_ARCH=$(uname -m) case "$DABUILD_ARCH" in x86|x86_64|aarch64|armhf|armv7) ;; *) die "Unsupported arch \"$DABUILD_ARCH\" detected." ;; esac ## use branch to figure out most appropriate alpine version if [ "$DABUILD_VERSION" ]; then ABUILD_VERSION=$DABUILD_VERSION else APORTS_BRANCH=$(git status | head -1) APORTS_BRANCH="${APORTS_BRANCH##*[ /]}" case $APORTS_BRANCH in [[:digit:]].[[:digit:]]-stable ) ABUILD_VERSION=${APORTS_BRANCH%-stable} ;; * ) ABUILD_VERSION=edge ;; esac fi ## check $DABUILD_PACKAGES is a writable directory ABUILD_PACKAGES=${DABUILD_PACKAGES:-${PWD%/aports*}/packages/${ABUILD_VERSION}} mkdir -p $ABUILD_PACKAGES if [ ! \( -d "$ABUILD_PACKAGES" -a -w "$ABUILD_PACKAGES" \) ]; then die "Error: invalid or unwritable packages directory specified!" \ "ABUILD_PACKAGES = '$ABUILD_PACKAGES'" \ "DABUILD_PACKAGES = '$DABUILD_PACKAGES'" fi ## setup volumes; use named volumes as cache if desired ABUILD_VOLUMES="-v ${HOME}/.abuild:/home/builder/.abuild \ -v ${PWD%/aports*}/aports:/home/builder/aports \ -v ${ABUILD_PACKAGES}:/home/builder/packages" if [ -f "/etc/abuild.conf" ]; then ABUILD_VOLUMES="$ABUILD_VOLUMES -v /etc/abuild.conf:/etc/abuild.conf:ro" fi if [ -w "/var/cache/distfiles" ]; then ABUILD_VOLUMES="$ABUILD_VOLUMES -v /var/cache/distfiles:/var/cache/distfiles" fi if [ "$DABUILD_CACHE" = "true" ]; then for v in %%ABUILD_VOLUMES%% ; do vol=abuild-$ABUILD_VERSION-$DABUILD_ARCH-${v//\//_} if [ "$DABUILD_CLEAN" = "true" ]; then ## clean the cache if requested docker rm -f $(docker ps -qaf "volume=$vol") >/dev/null 2>&1 || true docker volume rm $vol >/dev/null fi if docker volume ls -f 'name=$v' | grep $v >/dev/null ; then docker volume create $vol >/dev/null fi ABUILD_VOLUMES="$ABUILD_VOLUMES -v $vol:/$v" done fi ## go! ABUILD_WORKDIR=/home/builder/aports${PWD#*/aports} DOCKER="docker run -ti $ABUILD_VOLUMES -e DABUILD_DEBUG $DABUILD_ARGS" $DOCKER --workdir $ABUILD_WORKDIR \ %%ABUILD_IMAGE%%:$ABUILD_VERSION-$DABUILD_ARCH "$@"