Skip to content

Instantly share code, notes, and snippets.

@korchasa
Created December 29, 2023 16:58
Show Gist options
  • Save korchasa/a1d127206051d2f9a21df6a71b5f85fd to your computer and use it in GitHub Desktop.
Save korchasa/a1d127206051d2f9a21df6a71b5f85fd to your computer and use it in GitHub Desktop.
How to find current versions of alpine packages, to fix their version numbers in apk add
#!/usr/bin/env bash
# Check if at least two arguments are provided (Alpine version and one package)
if [ "$#" -lt 2 ]; then
echo "Usage: $0 <alpine_version> <package1> [package2 ...]"
exit 1
fi
alpine_version=$1
shift
docker run --rm alpine:$alpine_version sh -c "
apk update
output=''
for package in \$@
do
# Extract the exact match of the package name and its latest version
full_package=\$(apk search \${package} | grep -v -- '-doc' | grep -E \"^\${package}-[0-9]\" | sort -V | tail -n 1)
if [ ! -z \"\${full_package}\" ]; then
# Separate the package name from the version and suffix
package_version=\$(echo \${full_package} | sed 's/^'\${package}'-//')
output=\"\${output} \${package}=\${package_version}\"
else
echo \"Failed to find version for package \${package}\"
fi
done
echo \$output
" -- "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment