Skip to content

Instantly share code, notes, and snippets.

@jesselang
Created February 19, 2025 22:55
Show Gist options
  • Save jesselang/97e747981b06001b4f1f9a7c759c5512 to your computer and use it in GitHub Desktop.
Save jesselang/97e747981b06001b4f1f9a7c759c5512 to your computer and use it in GitHub Desktop.
Package management wrapper for Alpine/Ubuntu suitable for container image builds
__package_detect_distribution() {
if [ -f /etc/alpine-release ]; then
return 1
elif [ -f /etc/debian_version ]; then
return 2
else
return -1
fi
}
package() {
__package_detect_distribution
case $? in
1) # alpine
case "$1" in
update)
(set -ex;
apk update \
&& apk upgrade --no-progress --no-cache
)
;;
install)
shift
(set -ex;
apk add --no-progress --no-cache "$@"
)
;;
esac
;;
2) # ubuntu
case "$1" in
update)
(set -ex;
apt-get -qq update \
&& apt-get upgrade -y -qq \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
)
;;
install)
shift
(set -ex;
apt-get -qq update \
&& apt-get install -y -qq --no-install-recommends "$@" \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get autoremove -y -qq \
&& apt-get clean
)
;;
esac
;;
*) # unsupported
echo "error: unsupported distribution" >&2
return 1
;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment