docker-node/update.sh

134 lines
3.6 KiB
Bash
Executable File

#!/bin/bash
set -ue
. functions.sh
cd "$(cd "${0%/*}" && pwd -P)"
IFS=' ' read -ra versions <<<"$(get_versions .)"
IFS=' ' read -ra update_versions <<<"$(get_versions . "$@")"
if [ ${#versions[@]} -eq 0 ]; then
fatal "No valid versions found!"
fi
# Global variables
# Get architecure and use this as target architecture for docker image
# See details in function.sh
# TODO: Should be able to specify target architecture manually
arch=$(get_arch)
yarnVersion="$(curl -sSL --compressed https://yarnpkg.com/latest-version)"
function in_versions_to_update() {
local version=$1
for version_to_update in "${update_versions[@]}"; do
if [ "${version_to_update}" = "${version}" ]; then
echo 0
return
fi
done
echo 1
}
function update_node_version() {
local baseuri=${1}
shift
local version=${1}
shift
local template=${1}
shift
local dockerfile=${1}
shift
local variant=""
if [ $# -eq 1 ]; then
variant=${1}
shift
fi
fullVersion="$(curl -sSL --compressed "${baseuri}" | grep '<a href="v'"${version}." | sed -E 's!.*<a href="v([^"/]+)/?".*!\1!' | cut -d'.' -f2,3 | sort -n | tail -1)"
(
cp "${template}" "${dockerfile}"
local fromprefix=""
if [ "${arch}" != "amd64" ] && [ "${variant}" != "onbuild" ]; then
fromprefix="${arch}\\/"
fi
sed -Ei -e 's/^FROM (.*)/FROM '"${fromprefix}"'\1/' "${dockerfile}"
sed -Ei -e 's/^(ENV NODE_VERSION |FROM .*node:).*/\1'"${version}.${fullVersion:-0}"'/' "${dockerfile}"
sed -Ei -e 's/^(ENV YARN_VERSION ).*/\1'"${yarnVersion}"'/' "${dockerfile}"
# shellcheck disable=SC1004
new_line=' \\\
'
# Add GPG keys
for key_type in "node" "yarn"; do
while read -r line; do
pattern="\"\\$\\{$(echo "${key_type}" | tr '[:lower:]' '[:upper:]')_KEYS\\[@\\]\\}\""
sed -Ei -e "s/([ \\t]*)(${pattern})/\\1${line}${new_line}\\1\\2/" "${dockerfile}"
done <"keys/${key_type}.keys"
sed -Ei -e "/${pattern}/d" "${dockerfile}"
done
if [ "${variant}" = "alpine" ]; then
alpine_version=$(get_config "./" "alpine_version")
sed -Ei "s/(alpine:)0.0/\\1${alpine_version}/" "${dockerfile}"
fi
)
}
function add_stage() {
local baseuri=${1}
shift
local version=${1}
shift
local variant=${1}
shift
echo '
- stage: Build
env:
- NODE_VERSION: "'"${version}"'"
- VARIANT: "'"${variant}"'"' >>.travis.yml
}
echo '#### DO NOT MODIFY. THIS FILE IS AUTOGENERATED ####
' | cat - travis.yml.template >.travis.yml
for version in "${versions[@]}"; do
# Skip "docs" and other non-docker directories
[ -f "${version}/Dockerfile" ] || continue
parentpath=$(dirname "${version}")
versionnum=$(basename "${version}")
baseuri=$(get_config "${parentpath}" "baseuri")
update=$(in_versions_to_update "${version}")
add_stage "${baseuri}" "${version}" "default"
if [ "${update}" -eq 0 ]; then
info "Updating version ${version}..."
update_node_version "${baseuri}" "${versionnum}" "${parentpath}/Dockerfile.template" "${version}/Dockerfile" &
fi
# Get supported variants according the target architecture
# See details in function.sh
IFS=' ' read -ra variants <<<"$(get_variants "${parentpath}")"
for variant in "${variants[@]}"; do
# Skip non-docker directories
[ -f "${version}/${variant}/Dockerfile" ] || continue
add_stage "${baseuri}" "${version}" "${variant}"
if [ "${update}" -eq 0 ]; then
update_node_version "${baseuri}" "${versionnum}" "${parentpath}/Dockerfile-${variant}.template" "${version}/${variant}/Dockerfile" "${variant}" &
fi
done
done
wait
info "Done!"