PKGBUILDs/devel/container/makecontainerpkg

103 lines
3.1 KiB
Bash
Executable File

#!/bin/bash
set -e
# make tool to invoke configurable via environment variable to be able to invoke
# other tools like `repo-add` or `updpkgsums` as well
tool=${TOOL:-makepkg}
[[ $tool == 'makepkg' ]] && single_run= no_sync= || single_run=1 no_sync=1
# make basic arguments for cre
if [[ $CONTAINER ]]; then
name=$CONTAINER
else
if [[ $PKGNAME ]]; then
pkgname=$PKGNAME
else
source PKGBUILD
pkgname=$(echo "$pkgname" | tr '+' 'p')
fi
uuid=$(cat /proc/sys/kernel/random/uuid)
name=$tool-$pkgname-$uuid
fi
cre_args=(--name "$name" --workdir "/startdir" -v "$PWD":/startdir -it)
cre_rm_args=(--force)
# use "builduser" that has been created via the Dockerfile
uid=1000 gid=1000
if [[ $CRE == 'podman' ]]; then
cre_args+=(--userns="keep-id:uid=$uid,gid=$gid")
cre_rm_args+=(--time 0)
else
cre_args+=(--user="$uid:$gid")
# caveat: In contrast to Podman this gives a wrong gid for newly created files by
# default. Maybe it would help to pass `--userns-remap=…` to `dockerd`.
fi
# parse arguments
script_args= read_script_args=
for arg in "$@"; do
if [[ $read_script_args ]]; then
if [[ $arg == '--nodeps' ]] || [[ $arg == '-d' ]]; then
no_sync=1
elif [[ $arg == '--printsrcinfo' ]] || [[ $arg == '--help' ]] || [[ $arg == '-h' ]]; then
no_sync=1
single_run=1
fi
script_args+=" '$arg'"
else
if [[ $arg == '--' ]]; then
read_script_args=1
else
cre_args+=("$arg")
fi
fi
done
# add arguments from environment
if [[ $PACMAN_PKG_CACHE_DIR ]]; then
cre_args+=(-v "$PACMAN_PKG_CACHE_DIR":/var/cache/pacman/pkg)
fi
if [[ $CONTAINER_BUILD_CFG_DIR ]]; then
cre_args+=(-v "$CONTAINER_BUILD_CFG_DIR":/cfg)
fi
if [[ $CONTAINER_BUILD_CCACHE_DIR ]]; then
cre_args+=(-v "$CONTAINER_BUILD_CCACHE_DIR":/ccache)
script_args+=' CCACHE_DIR=/ccache'
fi
# load "containerbuild" and "containersync" script
bindir=$(dirname "$0")
script=$(cat "$bindir/containerbuild")
if ! [[ $no_sync ]]; then
script_sync=$(cat "$bindir/containersync")
fi
# invoke containerized build
ec=0 cre=${CRE:-docker}
if ! [[ $CONTAINER ]]; then
image=${CRE_IMAGE:-archlinux-base-devel}
if [[ $single_run ]]; then
"$cre" container run "${cre_args[@]}" "$image" bash \
-c "$script_sync $script '$tool' $script_args" || ec=$?
exit $ec
fi
"$cre" container create "${cre_args[@]}" "$image"
"$cre" container start "$name"
fi
"$cre" container exec "$name" bash \
-c "$script_sync $script '$tool' $script_args" || ec=$?
# stop and remove container unless we want to keep it for debugging
[[ $ec == 0 && $DEBUG == on-failure || -z $DEBUG ]] && \
"$cre" container rm "${cre_rm_args[@]}" "$name"
# log message about commands to use for investigation
[[ $ec != 0 ]] && [[ $DEBUG ]] && echo \
"You may investigate the failure via:
$cre exec -it '$name' bash
$cre mount '$name'
You may retry the build using the same container again via:
CONTAINER='$name' DEBUG='$DEBUG' $0 $@"
exit $ec