Format shell scripts with shfmt -i 2 -ci -l -w -f

This commit is contained in:
Peter Dave Hello 2018-05-14 19:29:13 +08:00
parent 0d0dcfd379
commit f384c2b6f1
6 changed files with 410 additions and 398 deletions

View File

@ -3,14 +3,14 @@
# Utlity functions # Utlity functions
info() { info() {
printf "%s\\n" "$@" printf "%s\\n" "$@"
} }
fatal() { fatal() {
printf "**********\\n" printf "**********\\n"
printf "Fatal Error: %s\\n" "$@" printf "Fatal Error: %s\\n" "$@"
printf "**********\\n" printf "**********\\n"
exit 1 exit 1
} }
# Get system architecture # Get system architecture
@ -19,30 +19,30 @@ fatal() {
# For crossing building, we need a way to specify the target # For crossing building, we need a way to specify the target
# architecutre manually. # architecutre manually.
function get_arch() { function get_arch() {
local arch local arch
case $(uname -m) in case $(uname -m) in
x86_64) x86_64)
arch="amd64" arch="amd64"
;; ;;
ppc64le) ppc64le)
arch="ppc64le" arch="ppc64le"
;; ;;
s390x) s390x)
arch="s390x" arch="s390x"
;; ;;
aarch64) aarch64)
arch="arm64" arch="arm64"
;; ;;
armv7l) armv7l)
arch="arm32v7" arch="arm32v7"
;; ;;
*) *)
echo "$0 does not support architecture $arch ... aborting" echo "$0 does not support architecture $arch ... aborting"
exit 1 exit 1
;; ;;
esac esac
echo "$arch" echo "$arch"
} }
# Get corresponding variants based on the architecture. # Get corresponding variants based on the architecture.
@ -51,34 +51,34 @@ function get_arch() {
# <architecutre 1> <supported variant 1 >,<supported variant 2>... # <architecutre 1> <supported variant 1 >,<supported variant 2>...
# <architecutre 2> <supported variant 1 >,<supported variant 2>... # <architecutre 2> <supported variant 1 >,<supported variant 2>...
function get_variants() { function get_variants() {
local dir local dir
dir=${1:-.} dir=${1:-.}
shift shift
local arch local arch
local availablevariants local availablevariants
local variantsfilter local variantsfilter
local variants local variants
arch=$(get_arch) arch=$(get_arch)
variantsfilter=( "$@" ) variantsfilter=("$@")
IFS=' ' read -ra availablevariants <<< "$(grep "^$arch" "$dir/architectures" | sed -E 's/'"$arch"'[[:space:]]*//' | sed -E 's/,/ /g')" IFS=' ' read -ra availablevariants <<<"$(grep "^$arch" "$dir/architectures" | sed -E 's/'"$arch"'[[:space:]]*//' | sed -E 's/,/ /g')"
if [ ${#variantsfilter[@]} -gt 0 ]; then if [ ${#variantsfilter[@]} -gt 0 ]; then
for variant1 in "${availablevariants[@]}"; do for variant1 in "${availablevariants[@]}"; do
for variant2 in "${variantsfilter[@]}"; do for variant2 in "${variantsfilter[@]}"; do
if [[ "$variant1" = "$variant2" ]]; then if [[ "$variant1" == "$variant2" ]]; then
variants+=("$variant1") variants+=("$variant1")
fi fi
done done
done done
if [ ${#variants[@]} -gt 0 ]; then if [ ${#variants[@]} -gt 0 ]; then
echo "${variants[@]}" echo "${variants[@]}"
fi fi
else else
echo "${availablevariants[@]}" echo "${availablevariants[@]}"
fi fi
} }
# Get supported architectures for a specific version and variant # Get supported architectures for a specific version and variant
@ -88,45 +88,47 @@ function get_variants() {
# default architectures. This will give us some benefits: # default architectures. This will give us some benefits:
# - a specific version may or may not support some architectures # - a specific version may or may not support some architectures
# - if there is no specialization for a version, just don't provide local architectures # - if there is no specialization for a version, just don't provide local architectures
function get_supported_arches () { function get_supported_arches() {
local version local version
local variant local variant
local arches local arches
local lines local lines
local line local line
version="$1"; shift version="$1"
variant="$1"; shift shift
variant="$1"
shift
# Get default supported arches # Get default supported arches
lines=$( grep "$variant" "$(dirname "$version")"/architectures 2>/dev/null | cut -d' ' -f1 ) lines=$(grep "$variant" "$(dirname "$version")"/architectures 2>/dev/null | cut -d' ' -f1)
# Get version specific supported architectures if there is specialized information # Get version specific supported architectures if there is specialized information
if [ -a "$version"/architectures ]; then if [ -a "$version"/architectures ]; then
lines=$( grep "$variant" "$version"/architectures 2>/dev/null | cut -d' ' -f1 ) lines=$(grep "$variant" "$version"/architectures 2>/dev/null | cut -d' ' -f1)
fi fi
while IFS='' read -r line; do while IFS='' read -r line; do
arches+=( "$line" ) arches+=("$line")
done <<< "$lines" done <<<"$lines"
echo "${arches[@]}" echo "${arches[@]}"
} }
# Get configuration values from the config file # Get configuration values from the config file
# #
# The configuration entries are simple key/value pairs which are whitespace separated. # The configuration entries are simple key/value pairs which are whitespace separated.
function get_config () { function get_config() {
local dir local dir
dir=${1:-.} dir=${1:-.}
shift shift
local name local name
name=$1 name=$1
shift shift
local value local value
value=$(grep "^$name" "$dir/config" | sed -E 's/'"$name"'[[:space:]]*//') value=$(grep "^$name" "$dir/config" | sed -E 's/'"$name"'[[:space:]]*//')
echo "$value" echo "$value"
} }
# Get available versions for a given path # Get available versions for a given path
@ -136,102 +138,104 @@ function get_config () {
# chakracore entry and found it to be a fork rather than a complete version. # chakracore entry and found it to be a fork rather than a complete version.
# #
# The result is a list of valid versions. # The result is a list of valid versions.
function get_versions () { function get_versions() {
local prefix local prefix
prefix=${1:-.} prefix=${1:-.}
shift shift
local versions local versions
local dirs=( "$@" ) local dirs=("$@")
if [ ${#dirs[@]} -eq 0 ]; then if [ ${#dirs[@]} -eq 0 ]; then
IFS=' ' read -ra dirs <<< "$(echo "${prefix%/}/"*/)" IFS=' ' read -ra dirs <<<"$(echo "${prefix%/}/"*/)"
fi fi
for dir in "${dirs[@]}"; do for dir in "${dirs[@]}"; do
if [ -a "$dir/config" ]; then if [ -a "$dir/config" ]; then
local subdirs local subdirs
IFS=' ' read -ra subdirs <<< "$(get_versions "${dir#./}")" IFS=' ' read -ra subdirs <<<"$(get_versions "${dir#./}")"
for subdir in "${subdirs[@]}"; do for subdir in "${subdirs[@]}"; do
versions+=( "$subdir" ) versions+=("$subdir")
done done
elif [ -a "$dir/Dockerfile" ]; then elif [ -a "$dir/Dockerfile" ]; then
versions+=( "${dir#./}" ) versions+=("${dir#./}")
fi fi
done done
if [ ${#versions[@]} -gt 0 ]; then if [ ${#versions[@]} -gt 0 ]; then
echo "${versions[@]%/}" echo "${versions[@]%/}"
fi fi
} }
function get_fork_name () { function get_fork_name() {
local version local version
version=$1 version=$1
shift shift
IFS='/' read -ra versionparts <<< "$version" IFS='/' read -ra versionparts <<<"$version"
if [ ${#versionparts[@]} -gt 1 ]; then if [ ${#versionparts[@]} -gt 1 ]; then
echo "${versionparts[0]}" echo "${versionparts[0]}"
fi fi
} }
function get_full_version () { function get_full_version() {
local version local version
version=$1 version=$1
shift shift
grep -m1 'ENV NODE_VERSION ' "$version/Dockerfile" | cut -d' ' -f3 grep -m1 'ENV NODE_VERSION ' "$version/Dockerfile" | cut -d' ' -f3
} }
function get_major_minor_version () { function get_major_minor_version() {
local version local version
version=$1 version=$1
shift shift
local fullversion local fullversion
fullversion=$(get_full_version "$version") fullversion=$(get_full_version "$version")
echo "$(echo "$fullversion" | cut -d'.' -f1).$(echo "$fullversion" | cut -d'.' -f2)" echo "$(echo "$fullversion" | cut -d'.' -f1).$(echo "$fullversion" | cut -d'.' -f2)"
} }
function get_tag () { function get_tag() {
local version local version
version=$1 version=$1
shift shift
local versiontype local versiontype
versiontype=${1:-full} versiontype=${1:-full}
shift shift
local tagversion local tagversion
if [ "$versiontype" = full ]; then if [ "$versiontype" = full ]; then
tagversion=$(get_full_version "$version") tagversion=$(get_full_version "$version")
elif [ "$versiontype" = majorminor ]; then elif [ "$versiontype" = majorminor ]; then
tagversion=$(get_major_minor_version "$version") tagversion=$(get_major_minor_version "$version")
fi fi
local tagparts local tagparts
IFS=' ' read -ra tagparts <<< "$(get_fork_name "$version") $tagversion" IFS=' ' read -ra tagparts <<<"$(get_fork_name "$version") $tagversion"
IFS='-'; echo "${tagparts[*]}"; unset IFS IFS='-'
echo "${tagparts[*]}"
unset IFS
} }
function sort_versions () { function sort_versions() {
local versions=( "$@" ) local versions=("$@")
local sorted local sorted
local lines local lines
local line local line
IFS=$'\n' IFS=$'\n'
lines="${versions[*]}" lines="${versions[*]}"
unset IFS unset IFS
while IFS='' read -r line; do while IFS='' read -r line; do
sorted+=( "$line" ) sorted+=("$line")
done <<< "$(echo "$lines" | grep "^[0-9]" | sort -r)" done <<<"$(echo "$lines" | grep "^[0-9]" | sort -r)"
while IFS='' read -r line; do while IFS='' read -r line; do
sorted+=( "$line" ) sorted+=("$line")
done <<< "$(echo "$lines" | grep -v "^[0-9]" | sort -r)" done <<<"$(echo "$lines" | grep -v "^[0-9]" | sort -r)"
echo "${sorted[@]}" echo "${sorted[@]}"
} }

View File

@ -6,29 +6,29 @@ hash git 2>/dev/null || { echo >&2 "git not found, exiting."; }
# Used dynamically: print "$array_" $1 # Used dynamically: print "$array_" $1
# shellcheck disable=SC2034 # shellcheck disable=SC2034
array_6='6 boron'; array_6='6 boron'
# shellcheck disable=SC2034 # shellcheck disable=SC2034
array_8='8 carbon'; array_8='8 carbon'
# shellcheck disable=SC2034 # shellcheck disable=SC2034
array_9='9'; array_9='9'
# shellcheck disable=SC2034 # shellcheck disable=SC2034
array_10='10 latest'; array_10='10 latest'
# shellcheck disable=SC2034 # shellcheck disable=SC2034
array_chakracore_8='chakracore-8'; array_chakracore_8='chakracore-8'
# shellcheck disable=SC2034 # shellcheck disable=SC2034
array_chakracore_10='chakracore-10 chakracore'; array_chakracore_10='chakracore-10 chakracore'
cd "$(cd "${0%/*}" && pwd -P)"; cd "$(cd "${0%/*}" && pwd -P)"
self="$(basename "${BASH_SOURCE[0]}")" self="$(basename "${BASH_SOURCE[0]}")"
IFS=' ' read -ra versions <<< "$(get_versions)" IFS=' ' read -ra versions <<<"$(get_versions)"
IFS=' ' read -ra versions <<< "$(sort_versions "${versions[@]}")" IFS=' ' read -ra versions <<<"$(sort_versions "${versions[@]}")"
url='https://github.com/nodejs/docker-node' url='https://github.com/nodejs/docker-node'
# get the most recent commit which modified any of "$@" # get the most recent commit which modified any of "$@"
fileCommit() { fileCommit() {
git log -1 --format='format:%H' HEAD -- "$@" git log -1 --format='format:%H' HEAD -- "$@"
} }
echo "# this file is generated via ${url}/blob/$(fileCommit "$self")/$self" echo "# this file is generated via ${url}/blob/$(fileCommit "$self")/$self"
@ -39,57 +39,61 @@ echo
# prints "$2$1$3$1...$N" # prints "$2$1$3$1...$N"
join() { join() {
local sep="$1"; shift local sep="$1"
local out; printf -v out "${sep//%/%%}%s" "$@" shift
echo "${out#$sep}" local out
printf -v out "${sep//%/%%}%s" "$@"
echo "${out#$sep}"
} }
get_stub() { get_stub() {
local version="$1"; shift local version="$1"
IFS='/' read -ra versionparts <<< "$version" shift
local stub; eval stub="$(join '_' "${versionparts[@]}" | awk -F. '{ print "$array_" $1 }')"; IFS='/' read -ra versionparts <<<"$version"
echo "$stub" local stub
eval stub="$(join '_' "${versionparts[@]}" | awk -F. '{ print "$array_" $1 }')"
echo "$stub"
} }
for version in "${versions[@]}"; do for version in "${versions[@]}"; do
# Skip "docs" and other non-docker directories # Skip "docs" and other non-docker directories
[ -f "$version/Dockerfile" ] || continue [ -f "$version/Dockerfile" ] || continue
stub=$(get_stub "$version") stub=$(get_stub "$version")
commit="$(fileCommit "$version")" commit="$(fileCommit "$version")"
fullVersion="$(get_tag "$version" full)" fullVersion="$(get_tag "$version" full)"
majorMinorVersion="$(get_tag "$version" majorminor)" majorMinorVersion="$(get_tag "$version" majorminor)"
IFS=' ' read -ra versionAliases <<< "$fullVersion $majorMinorVersion $stub" IFS=' ' read -ra versionAliases <<<"$fullVersion $majorMinorVersion $stub"
# Get supported architectures for a specific version. See details in function.sh # Get supported architectures for a specific version. See details in function.sh
IFS=' ' read -ra supportedArches <<< "$(get_supported_arches "$version" "default")" IFS=' ' read -ra supportedArches <<<"$(get_supported_arches "$version" "default")"
echo "Tags: $(join ', ' "${versionAliases[@]}")" echo "Tags: $(join ', ' "${versionAliases[@]}")"
echo "Architectures: $(join ', ' "${supportedArches[@]}")" echo "Architectures: $(join ', ' "${supportedArches[@]}")"
echo "GitCommit: ${commit}" echo "GitCommit: ${commit}"
echo "Directory: ${version}" echo "Directory: ${version}"
echo echo
# Get supported variants according to the target architecture. # Get supported variants according to the target architecture.
# See details in function.sh # See details in function.sh
IFS=' ' read -ra variants <<< "$(get_variants "$(dirname "$version")")" IFS=' ' read -ra variants <<<"$(get_variants "$(dirname "$version")")"
for variant in "${variants[@]}"; do for variant in "${variants[@]}"; do
# Skip non-docker directories # Skip non-docker directories
[ -f "$version/$variant/Dockerfile" ] || continue [ -f "$version/$variant/Dockerfile" ] || continue
commit="$(fileCommit "$version/$variant")" commit="$(fileCommit "$version/$variant")"
slash='/' slash='/'
variantAliases=( "${versionAliases[@]/%/-${variant//$slash/-}}" ) variantAliases=("${versionAliases[@]/%/-${variant//$slash/-}}")
variantAliases=( "${variantAliases[@]//latest-/}" ) variantAliases=("${variantAliases[@]//latest-/}")
# Get supported architectures for a specific version and variant. # Get supported architectures for a specific version and variant.
# See details in function.sh # See details in function.sh
IFS=' ' read -ra supportedArches <<< "$(get_supported_arches "$version" "$variant")" IFS=' ' read -ra supportedArches <<<"$(get_supported_arches "$version" "$variant")"
echo "Tags: $(join ', ' "${variantAliases[@]}")" echo "Tags: $(join ', ' "${variantAliases[@]}")"
echo "Architectures: $(join ', ' "${supportedArches[@]}")" echo "Architectures: $(join ', ' "${supportedArches[@]}")"
echo "GitCommit: ${commit}" echo "GitCommit: ${commit}"
echo "Directory: ${version}/${variant}" echo "Directory: ${version}/${variant}"
echo echo
done done
done done

View File

@ -3,24 +3,24 @@ set -e
. functions.sh . functions.sh
if [ -z "$1" ]; then if [ -z "$1" ]; then
COMMIT_ID="$TRAVIS_COMMIT" COMMIT_ID="$TRAVIS_COMMIT"
COMMIT_MESSAGE="$TRAVIS_COMMIT_MESSAGE" COMMIT_MESSAGE="$TRAVIS_COMMIT_MESSAGE"
BRANCH_NAME="travis-$TRAVIS_BUILD_ID" BRANCH_NAME="travis-$TRAVIS_BUILD_ID"
GITHUB_USERNAME="nodejs-github-bot" GITHUB_USERNAME="nodejs-github-bot"
else else
COMMIT_ID="$1" COMMIT_ID="$1"
COMMIT_MESSAGE="$(git show -s --format=%B "$1")" COMMIT_MESSAGE="$(git show -s --format=%B "$1")"
BRANCH_NAME="travis-$(date +%s)" BRANCH_NAME="travis-$(date +%s)"
if [[ "$(git remote get-url origin)" =~ github.com/([^/]*)/docker-node.git ]]; then if [[ "$(git remote get-url origin)" =~ github.com/([^/]*)/docker-node.git ]]; then
GITHUB_USERNAME="${BASH_REMATCH[1]}" GITHUB_USERNAME="${BASH_REMATCH[1]}"
fi fi
fi fi
if [[ "$COMMIT_MESSAGE" =~ Merge\ pull\ request\ \#([0-9]*) ]]; then if [[ "$COMMIT_MESSAGE" =~ Merge\ pull\ request\ \#([0-9]*) ]]; then
# This is a merge from a pull request # This is a merge from a pull request
PR_NUMBER="${BASH_REMATCH[1]}" PR_NUMBER="${BASH_REMATCH[1]}"
COMMIT_MESSAGE="$(printf "%s" "$COMMIT_MESSAGE" | tail -n 1)" COMMIT_MESSAGE="$(printf "%s" "$COMMIT_MESSAGE" | tail -n 1)"
fi fi
IMAGES_FILE="library/node" IMAGES_FILE="library/node"
@ -31,134 +31,137 @@ DOCKER_SLUG="nodejs/docker-node"
gitpath="$REPO_NAME" gitpath="$REPO_NAME"
function updated() { function updated() {
local versions local versions
local images_changed local images_changed
IFS=' ' read -ra versions <<< "$(IFS=','; get_versions)" IFS=' ' read -ra versions <<<"$(
images_changed=$(git diff --name-only "$COMMIT_ID".."$COMMIT_ID"~1 "${versions[@]}") IFS=','
get_versions
)"
images_changed=$(git diff --name-only "$COMMIT_ID".."$COMMIT_ID"~1 "${versions[@]}")
if [ -z "$images_changed" ]; then if [ -z "$images_changed" ]; then
return 1 return 1
else else
return 0 return 0
fi fi
} }
function auth_header() { function auth_header() {
echo "Authorization: token $GITHUB_API_TOKEN" echo "Authorization: token $GITHUB_API_TOKEN"
} }
function permission_check() { function permission_check() {
if [ -z "$GITHUB_API_TOKEN" ]; then if [ -z "$GITHUB_API_TOKEN" ]; then
fatal "Environment variable \$GITHUB_API_TOKEN is missing or empty" fatal "Environment variable \$GITHUB_API_TOKEN is missing or empty"
fi fi
auth="$(curl -H "$(auth_header)" \ auth="$(curl -H "$(auth_header)" \
-s \ -s \
"https://api.github.com")" "https://api.github.com")"
if [ "$(echo "$auth" | jq .message)" = "\"Bad credentials\"" ]; then if [ "$(echo "$auth" | jq .message)" = "\"Bad credentials\"" ]; then
fatal "Authentication Failed! Invalid \$GITHUB_API_TOKEN" fatal "Authentication Failed! Invalid \$GITHUB_API_TOKEN"
fi fi
auth="$(curl -H "$(auth_header)" \ auth="$(curl -H "$(auth_header)" \
-s \ -s \
"https://api.github.com/repos/$ORIGIN_SLUG/collaborators/$GITHUB_USERNAME/permission")" "https://api.github.com/repos/$ORIGIN_SLUG/collaborators/$GITHUB_USERNAME/permission")"
if [ "$(echo "$auth" | jq .message)" != "null" ]; then if [ "$(echo "$auth" | jq .message)" != "null" ]; then
fatal "\$GITHUB_API_TOKEN can't push to https://github.com/$ORIGIN_SLUG.git" fatal "\$GITHUB_API_TOKEN can't push to https://github.com/$ORIGIN_SLUG.git"
fi fi
} }
function setup_git_author() { function setup_git_author() {
GIT_AUTHOR_NAME="$(git show -s --format="%aN" "$COMMIT_ID")" GIT_AUTHOR_NAME="$(git show -s --format="%aN" "$COMMIT_ID")"
GIT_AUTHOR_EMAIL="$(git show -s --format="%aE" "$COMMIT_ID")" GIT_AUTHOR_EMAIL="$(git show -s --format="%aE" "$COMMIT_ID")"
GIT_COMMITTER_NAME="$(git show -s --format="%cN" "$COMMIT_ID")" GIT_COMMITTER_NAME="$(git show -s --format="%cN" "$COMMIT_ID")"
GIT_COMMITTER_EMAIL="$(git show -s --format="%cN" "$COMMIT_ID")" GIT_COMMITTER_EMAIL="$(git show -s --format="%cN" "$COMMIT_ID")"
export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL
} }
function message() { function message() {
echo "Node: $COMMIT_MESSAGE" echo "Node: $COMMIT_MESSAGE"
} }
function pr_payload() { function pr_payload() {
local escaped_message local escaped_message
escaped_message="$(echo "$COMMIT_MESSAGE" | sed -E -e "s/\"/\\\\\"/g")" escaped_message="$(echo "$COMMIT_MESSAGE" | sed -E -e "s/\"/\\\\\"/g")"
echo '{ echo '{
"title": "Node: '"$escaped_message"'", "title": "Node: '"$escaped_message"'",
"body": "Commit: nodejs/docker-node@'"$COMMIT_ID"'", "body": "Commit: nodejs/docker-node@'"$COMMIT_ID"'",
"head": "'"$GITHUB_USERNAME"':'"$BRANCH_NAME"'", "head": "'"$GITHUB_USERNAME"':'"$BRANCH_NAME"'",
"base": "master" "base": "master"
}' }'
} }
function comment_payload() { function comment_payload() {
local pr_url local pr_url
pr_url="$1" pr_url="$1"
echo '{ echo '{
"body": "Created PR to the '"$REPO_NAME"' repo ('"$pr_url"')" "body": "Created PR to the '"$REPO_NAME"' repo ('"$pr_url"')"
}' }'
} }
if updated; then if updated; then
permission_check permission_check
# Set Git User Info # Set Git User Info
setup_git_author setup_git_author
info "Cloning..." info "Cloning..."
git clone --depth 50 "https://github.com/$UPSTREAM_SLUG.git" $gitpath 2> /dev/null git clone --depth 50 "https://github.com/$UPSTREAM_SLUG.git" $gitpath 2>/dev/null
stackbrew="$(./generate-stackbrew-library.sh)" stackbrew="$(./generate-stackbrew-library.sh)"
cd $gitpath cd $gitpath
echo "$stackbrew" > "$IMAGES_FILE" echo "$stackbrew" >"$IMAGES_FILE"
git checkout -b "$BRANCH_NAME" git checkout -b "$BRANCH_NAME"
git add "$IMAGES_FILE" git add "$IMAGES_FILE"
git commit -m "$(message)" git commit -m "$(message)"
info "Pushing..." info "Pushing..."
git push "https://$GITHUB_API_TOKEN:x-oauth-basic@github.com/$ORIGIN_SLUG.git" -f "$BRANCH_NAME" 2> /dev/null || fatal "Error pushing the updated stackbrew" git push "https://$GITHUB_API_TOKEN:x-oauth-basic@github.com/$ORIGIN_SLUG.git" -f "$BRANCH_NAME" 2>/dev/null || fatal "Error pushing the updated stackbrew"
cd - && rm -rf $gitpath cd - && rm -rf $gitpath
info "Creating Pull request" info "Creating Pull request"
pr_response_payload="$(curl -H "$(auth_header)" \ pr_response_payload="$(curl -H "$(auth_header)" \
-s \ -s \
-X POST \ -X POST \
-d "$(pr_payload)" \ -d "$(pr_payload)" \
"https://api.github.com/repos/$UPSTREAM_SLUG/pulls")" "https://api.github.com/repos/$UPSTREAM_SLUG/pulls")"
url="$(echo "$pr_response_payload" | jq -r .html_url)" url="$(echo "$pr_response_payload" | jq -r .html_url)"
if [ "$url" != "null" ]; then if [ "$url" != "null" ]; then
info "Pull request created at $url" info "Pull request created at $url"
if [ ! -z "$PR_NUMBER" ]; then if [ ! -z "$PR_NUMBER" ]; then
comment_endpoint="https://api.github.com/repos/$DOCKER_SLUG/issues/$PR_NUMBER/comments" comment_endpoint="https://api.github.com/repos/$DOCKER_SLUG/issues/$PR_NUMBER/comments"
else else
comment_endpoint="https://api.github.com/repos/$DOCKER_SLUG/commits/$COMMIT_ID/comments" comment_endpoint="https://api.github.com/repos/$DOCKER_SLUG/commits/$COMMIT_ID/comments"
fi fi
info "Creating Commit Comment" info "Creating Commit Comment"
commit_response_payload="$(curl -H "$(auth_header)" \ commit_response_payload="$(curl -H "$(auth_header)" \
-s \ -s \
-X POST \ -X POST \
-d "$(comment_payload "$url")" \ -d "$(comment_payload "$url")" \
"$comment_endpoint")" "$comment_endpoint")"
if [ "$(echo "$commit_response_payload" | jq .message)" != "null" ]; then if [ "$(echo "$commit_response_payload" | jq .message)" != "null" ]; then
fatal "Error linking the pull request ($error_message)" fatal "Error linking the pull request ($error_message)"
else else
comment_url="$(echo "$commit_response_payload" | jq -r .html_url)" comment_url="$(echo "$commit_response_payload" | jq -r .html_url)"
info "Created comment at $comment_url" info "Created comment at $comment_url"
fi fi
else else
error_message=$(echo "$pr_response_payload" | jq .message) error_message=$(echo "$pr_response_payload" | jq .message)
fatal "Error creating pull request ($error_message)" fatal "Error creating pull request ($error_message)"
fi fi
else else
info "No change!" info "No change!"
fi fi

View File

@ -9,18 +9,21 @@ set -uo pipefail
# Convert comma delimited cli arguments to arrays # Convert comma delimited cli arguments to arrays
# E.g. ./test-build.sh 8,10 slim,onbuild # E.g. ./test-build.sh 8,10 slim,onbuild
# "8,10" becomes "8 10" and "slim,onbuild" becomes "slim onbuild" # "8,10" becomes "8 10" and "slim,onbuild" becomes "slim onbuild"
IFS=',' read -ra versions_arg <<< "${1:-}" IFS=',' read -ra versions_arg <<<"${1:-}"
IFS=',' read -ra variant_arg <<< "${2:-}" IFS=',' read -ra variant_arg <<<"${2:-}"
function build () { function build() {
local version local version
local tag local tag
local variant local variant
local full_tag local full_tag
local path local path
version="$1"; shift version="$1"
variant="$1"; shift shift
tag="$1"; shift variant="$1"
shift
tag="$1"
shift
if [ -z "$variant" ]; then if [ -z "$variant" ]; then
full_tag="$tag" full_tag="$tag"
@ -41,9 +44,9 @@ function build () {
docker run --rm -v "$PWD/test-image.sh:/usr/local/bin/test.sh" node:"$full_tag" test.sh "$full_version" docker run --rm -v "$PWD/test-image.sh:/usr/local/bin/test.sh" node:"$full_tag" test.sh "$full_version"
} }
cd "$(cd "${0%/*}" && pwd -P)" || exit; cd "$(cd "${0%/*}" && pwd -P)" || exit
IFS=' ' read -ra versions <<< "$(get_versions . "${versions_arg[@]}")" IFS=' ' read -ra versions <<<"$(get_versions . "${versions_arg[@]}")"
if [ ${#versions[@]} -eq 0 ]; then if [ ${#versions[@]} -eq 0 ]; then
fatal "No valid versions found!" fatal "No valid versions found!"
fi fi
@ -57,10 +60,10 @@ for version in "${versions[@]}"; do
# Get supported variants according to the target architecture. # Get supported variants according to the target architecture.
# See details in function.sh # See details in function.sh
IFS=' ' read -ra variants <<< "$(get_variants "$(dirname "$version")" "${variant_arg[@]}")" IFS=' ' read -ra variants <<<"$(get_variants "$(dirname "$version")" "${variant_arg[@]}")"
# Only build the default Dockerfile if "default" is in the variant list # Only build the default Dockerfile if "default" is in the variant list
if [[ "${variants[*]}" =~ "default" ]] || [[ "${variants[*]}" =~ "onbuild" ]] ; then if [[ "${variants[*]}" =~ "default" ]] || [[ "${variants[*]}" =~ "onbuild" ]]; then
build "$version" "" "$tag" build "$version" "" "$tag"
fi fi

View File

@ -1,18 +1,18 @@
#!/bin/sh #!/bin/sh
if [ "$(node -e "process.stdout.write(process.versions.node)")" != "$1" ]; then if [ "$(node -e "process.stdout.write(process.versions.node)")" != "$1" ]; then
echo "Test for node failed!" echo "Test for node failed!"
exit 1 exit 1
fi fi
echo "Test for node succeeded." echo "Test for node succeeded."
if ! npm --version > /dev/null; then if ! npm --version >/dev/null; then
echo "Test for npm failed!" echo "Test for npm failed!"
exit 2 exit 2
fi fi
echo "Test for npm succeeded." echo "Test for npm succeeded."
if ! yarn --version > /dev/null; then if ! yarn --version >/dev/null; then
echo "Test of yarn failed!" echo "Test of yarn failed!"
exit 3 exit 3
fi fi
echo "Test for yarn succeeded." echo "Test for yarn succeeded."

146
update.sh
View File

@ -3,11 +3,11 @@ set -ue
. functions.sh . functions.sh
cd "$(cd "${0%/*}" && pwd -P)"; cd "$(cd "${0%/*}" && pwd -P)"
IFS=' ' read -ra versions <<< "$(get_versions . "$@")" IFS=' ' read -ra versions <<<"$(get_versions . "$@")"
if [ ${#versions[@]} -eq 0 ]; then if [ ${#versions[@]} -eq 0 ]; then
fatal "No valid versions found!" fatal "No valid versions found!"
fi fi
# Global variables # Global variables
@ -18,100 +18,98 @@ arch=$(get_arch)
yarnVersion="$(curl -sSL --compressed https://yarnpkg.com/latest-version)" yarnVersion="$(curl -sSL --compressed https://yarnpkg.com/latest-version)"
function update_node_version { function update_node_version() {
local baseuri=$1 local baseuri=$1
shift shift
local version=$1 local version=$1
shift shift
local template=$1 local template=$1
shift shift
local dockerfile=$1 local dockerfile=$1
shift shift
local variant= local variant
if [[ $# -eq 1 ]]; then if [[ $# -eq 1 ]]; then
variant=$1 variant=$1
shift shift
fi 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)" 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" cp "$template" "$dockerfile"
local fromprefix= local fromprefix
if [[ "$arch" != "amd64" && "$variant" != "onbuild" ]]; then if [[ "$arch" != "amd64" && "$variant" != "onbuild" ]]; then
fromprefix="$arch\\/" fromprefix="$arch\\/"
fi fi
sed -E -i.bak 's/^FROM (.*)/FROM '"$fromprefix"'\1/' "$dockerfile" && rm "$dockerfile".bak sed -E -i.bak 's/^FROM (.*)/FROM '"$fromprefix"'\1/' "$dockerfile" && rm "$dockerfile".bak
sed -E -i.bak 's/^(ENV NODE_VERSION |FROM .*node:).*/\1'"$version.${fullVersion:-0}"'/' "$dockerfile" && rm "$dockerfile".bak sed -E -i.bak 's/^(ENV NODE_VERSION |FROM .*node:).*/\1'"$version.${fullVersion:-0}"'/' "$dockerfile" && rm "$dockerfile".bak
sed -E -i.bak 's/^(ENV YARN_VERSION ).*/\1'"$yarnVersion"'/' "$dockerfile" && rm "$dockerfile".bak sed -E -i.bak 's/^(ENV YARN_VERSION ).*/\1'"$yarnVersion"'/' "$dockerfile" && rm "$dockerfile".bak
# shellcheck disable=SC1004 # shellcheck disable=SC1004
new_line=' \\\ new_line=' \\\
' '
# Add GPG keys # Add GPG keys
for key_type in "node" "yarn" for key_type in "node" "yarn"; do
do while read -r line; do
while read -r line pattern="\"\\$\\{$(echo "$key_type" | tr '[:lower:]' '[:upper:]')_KEYS\\[@\\]\\}\""
do sed -E -i.bak -e "s/([ \\t]*)($pattern)/\\1${line}${new_line}\\1\\2/" "$dockerfile" && rm "$dockerfile".bak
pattern="\"\\$\\{$(echo "$key_type" | tr '[:lower:]' '[:upper:]')_KEYS\\[@\\]\\}\"" done <"keys/$key_type.keys"
sed -E -i.bak -e "s/([ \\t]*)($pattern)/\\1${line}${new_line}\\1\\2/" "$dockerfile" && rm "$dockerfile".bak sed -E -i.bak "/$pattern/d" "$dockerfile" && rm "$dockerfile".bak
done < "keys/$key_type.keys" done
sed -E -i.bak "/$pattern/d" "$dockerfile" && rm "$dockerfile".bak
done
if [[ "${version/.*/}" -ge 10 ]]; then if [[ "${version/.*/}" -ge 10 ]]; then
sed -E -i.bak 's/FROM (.*)alpine:3.4/FROM \1alpine:3.7/' "$dockerfile" sed -E -i.bak 's/FROM (.*)alpine:3.4/FROM \1alpine:3.7/' "$dockerfile"
rm "$dockerfile.bak" rm "$dockerfile.bak"
elif [[ "${version/.*/}" -ge 8 || "$arch" = "ppc64le" || "$arch" = "s390x" || "$arch" = "arm64" || "$arch" = "arm32v7" ]]; then elif [[ "${version/.*/}" -ge 8 || "$arch" == "ppc64le" || "$arch" == "s390x" || "$arch" == "arm64" || "$arch" == "arm32v7" ]]; then
sed -E -i.bak 's/FROM (.*)alpine:3.4/FROM \1alpine:3.6/' "$dockerfile" sed -E -i.bak 's/FROM (.*)alpine:3.4/FROM \1alpine:3.6/' "$dockerfile"
rm "$dockerfile.bak" rm "$dockerfile.bak"
fi fi
) )
} }
function add_stage { function add_stage() {
local baseuri=$1 local baseuri=$1
shift shift
local version=$1 local version=$1
shift shift
local variant=$1 local variant=$1
shift shift
echo ' - stage: Build echo ' - stage: Build
env: env:
- NODE_VERSION: "'"$version"'" - NODE_VERSION: "'"$version"'"
- VARIANT: "'"$variant"'" - VARIANT: "'"$variant"'"
' >> .travis.yml ' >>.travis.yml
} }
echo '#### DO NOT MODIFY. THIS FILE IS AUTOGENERATED #### echo '#### DO NOT MODIFY. THIS FILE IS AUTOGENERATED ####
' | cat - travis.yml.template > .travis.yml ' | cat - travis.yml.template >.travis.yml
for version in "${versions[@]}"; do for version in "${versions[@]}"; do
# Skip "docs" and other non-docker directories # Skip "docs" and other non-docker directories
[ -f "$version/Dockerfile" ] || continue [ -f "$version/Dockerfile" ] || continue
info "Updating version $version..." info "Updating version $version..."
parentpath=$(dirname "$version") parentpath=$(dirname "$version")
versionnum=$(basename "$version") versionnum=$(basename "$version")
baseuri=$(get_config "$parentpath" "baseuri") baseuri=$(get_config "$parentpath" "baseuri")
add_stage "$baseuri" "$version" "default" add_stage "$baseuri" "$version" "default"
update_node_version "$baseuri" "$versionnum" "$parentpath/Dockerfile.template" "$version/Dockerfile" & update_node_version "$baseuri" "$versionnum" "$parentpath/Dockerfile.template" "$version/Dockerfile" &
# Get supported variants according the target architecture # Get supported variants according the target architecture
# See details in function.sh # See details in function.sh
IFS=' ' read -ra variants <<< "$(get_variants "$parentpath")" IFS=' ' read -ra variants <<<"$(get_variants "$parentpath")"
for variant in "${variants[@]}"; do for variant in "${variants[@]}"; do
# Skip non-docker directories # Skip non-docker directories
[ -f "$version/$variant/Dockerfile" ] || continue [ -f "$version/$variant/Dockerfile" ] || continue
add_stage "$baseuri" "$version" "$variant" add_stage "$baseuri" "$version" "$variant"
update_node_version "$baseuri" "$versionnum" "$parentpath/Dockerfile-$variant.template" "$version/$variant/Dockerfile" "$variant" & update_node_version "$baseuri" "$versionnum" "$parentpath/Dockerfile-$variant.template" "$version/$variant/Dockerfile" "$variant" &
done done
done done
wait wait