diff --git a/.travis.yml b/.travis.yml index 18f169a47..270ceb480 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,6 +32,14 @@ jobs: doctoc --title='## Table of Contents' --github README.md && diff -q README.md README.md.tmp + - stage: Test + env: + - TEST: shfmt check + script: + - docker run -it --rm -v "$(pwd)":/sh -w /sh jamesmstone/shfmt -i 2 -l -w -ci . + - git diff --color + - git diff --stat=220 --color --exit-code + - stage: Test env: - TEST: Shell Check diff --git a/functions.sh b/functions.sh index 86eb30621..c54e84790 100755 --- a/functions.sh +++ b/functions.sh @@ -3,14 +3,14 @@ # Utlity functions info() { - printf "%s\\n" "$@" + printf "%s\\n" "$@" } fatal() { - printf "**********\\n" - printf "Fatal Error: %s\\n" "$@" - printf "**********\\n" - exit 1 + printf "**********\\n" + printf "Fatal Error: %s\\n" "$@" + printf "**********\\n" + exit 1 } # Get system architecture @@ -19,30 +19,30 @@ fatal() { # For crossing building, we need a way to specify the target # architecutre manually. function get_arch() { - local arch - case $(uname -m) in - x86_64) - arch="amd64" - ;; - ppc64le) - arch="ppc64le" - ;; - s390x) - arch="s390x" - ;; - aarch64) - arch="arm64" - ;; - armv7l) - arch="arm32v7" - ;; - *) - echo "$0 does not support architecture $arch ... aborting" - exit 1 - ;; - esac - - echo "$arch" + local arch + case $(uname -m) in + x86_64) + arch="amd64" + ;; + ppc64le) + arch="ppc64le" + ;; + s390x) + arch="s390x" + ;; + aarch64) + arch="arm64" + ;; + armv7l) + arch="arm32v7" + ;; + *) + echo "$0 does not support architecture $arch ... aborting" + exit 1 + ;; + esac + + echo "$arch" } # Get corresponding variants based on the architecture. @@ -51,34 +51,34 @@ function get_arch() { # ,... # ,... function get_variants() { - local dir - dir=${1:-.} - shift - - local arch - local availablevariants - local variantsfilter - local variants - - arch=$(get_arch) - variantsfilter=( "$@" ) - IFS=' ' read -ra availablevariants <<< "$(grep "^$arch" "$dir/architectures" | sed -E 's/'"$arch"'[[:space:]]*//' | sed -E 's/,/ /g')" - - if [ ${#variantsfilter[@]} -gt 0 ]; then - for variant1 in "${availablevariants[@]}"; do - for variant2 in "${variantsfilter[@]}"; do - if [[ "$variant1" = "$variant2" ]]; then - variants+=("$variant1") - fi - done - done - - if [ ${#variants[@]} -gt 0 ]; then - echo "${variants[@]}" - fi - else - echo "${availablevariants[@]}" - fi + local dir + dir=${1:-.} + shift + + local arch + local availablevariants + local variantsfilter + local variants + + arch=$(get_arch) + variantsfilter=("$@") + IFS=' ' read -ra availablevariants <<<"$(grep "^$arch" "$dir/architectures" | sed -E 's/'"$arch"'[[:space:]]*//' | sed -E 's/,/ /g')" + + if [ ${#variantsfilter[@]} -gt 0 ]; then + for variant1 in "${availablevariants[@]}"; do + for variant2 in "${variantsfilter[@]}"; do + if [[ "$variant1" == "$variant2" ]]; then + variants+=("$variant1") + fi + done + done + + if [ ${#variants[@]} -gt 0 ]; then + echo "${variants[@]}" + fi + else + echo "${availablevariants[@]}" + fi } # Get supported architectures for a specific version and variant @@ -88,45 +88,47 @@ function get_variants() { # default architectures. This will give us some benefits: # - a specific version may or may not support some architectures # - if there is no specialization for a version, just don't provide local architectures -function get_supported_arches () { - local version - local variant - local arches - local lines - local line - version="$1"; shift - variant="$1"; shift - - # Get default supported arches - lines=$( grep "$variant" "$(dirname "$version")"/architectures 2>/dev/null | cut -d' ' -f1 ) - - # Get version specific supported architectures if there is specialized information - if [ -a "$version"/architectures ]; then - lines=$( grep "$variant" "$version"/architectures 2>/dev/null | cut -d' ' -f1 ) - fi - - while IFS='' read -r line; do - arches+=( "$line" ) - done <<< "$lines" - - echo "${arches[@]}" +function get_supported_arches() { + local version + local variant + local arches + local lines + local line + version="$1" + shift + variant="$1" + shift + + # Get default supported arches + lines=$(grep "$variant" "$(dirname "$version")"/architectures 2>/dev/null | cut -d' ' -f1) + + # Get version specific supported architectures if there is specialized information + if [ -a "$version"/architectures ]; then + lines=$(grep "$variant" "$version"/architectures 2>/dev/null | cut -d' ' -f1) + fi + + while IFS='' read -r line; do + arches+=("$line") + done <<<"$lines" + + echo "${arches[@]}" } # Get configuration values from the config file # # The configuration entries are simple key/value pairs which are whitespace separated. -function get_config () { - local dir - dir=${1:-.} - shift - - local name - name=$1 - shift - - local value - value=$(grep "^$name" "$dir/config" | sed -E 's/'"$name"'[[:space:]]*//') - echo "$value" +function get_config() { + local dir + dir=${1:-.} + shift + + local name + name=$1 + shift + + local value + value=$(grep "^$name" "$dir/config" | sed -E 's/'"$name"'[[:space:]]*//') + echo "$value" } # 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. # # The result is a list of valid versions. -function get_versions () { - local prefix - prefix=${1:-.} - shift - - local versions - local dirs=( "$@" ) - if [ ${#dirs[@]} -eq 0 ]; then - IFS=' ' read -ra dirs <<< "$(echo "${prefix%/}/"*/)" - fi - - for dir in "${dirs[@]}"; do - if [ -a "$dir/config" ]; then - local subdirs - IFS=' ' read -ra subdirs <<< "$(get_versions "${dir#./}")" - for subdir in "${subdirs[@]}"; do - versions+=( "$subdir" ) - done - elif [ -a "$dir/Dockerfile" ]; then - versions+=( "${dir#./}" ) - fi - done - - if [ ${#versions[@]} -gt 0 ]; then - echo "${versions[@]%/}" - fi +function get_versions() { + local prefix + prefix=${1:-.} + shift + + local versions + local dirs=("$@") + if [ ${#dirs[@]} -eq 0 ]; then + IFS=' ' read -ra dirs <<<"$(echo "${prefix%/}/"*/)" + fi + + for dir in "${dirs[@]}"; do + if [ -a "$dir/config" ]; then + local subdirs + IFS=' ' read -ra subdirs <<<"$(get_versions "${dir#./}")" + for subdir in "${subdirs[@]}"; do + versions+=("$subdir") + done + elif [ -a "$dir/Dockerfile" ]; then + versions+=("${dir#./}") + fi + done + + if [ ${#versions[@]} -gt 0 ]; then + echo "${versions[@]%/}" + fi } -function get_fork_name () { - local version - version=$1 - shift +function get_fork_name() { + local version + version=$1 + shift - IFS='/' read -ra versionparts <<< "$version" - if [ ${#versionparts[@]} -gt 1 ]; then - echo "${versionparts[0]}" - fi + IFS='/' read -ra versionparts <<<"$version" + if [ ${#versionparts[@]} -gt 1 ]; then + echo "${versionparts[0]}" + fi } -function get_full_version () { - local version - version=$1 - shift +function get_full_version() { + local version + version=$1 + 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 () { - local version - version=$1 - shift +function get_major_minor_version() { + local version + version=$1 + shift - local fullversion - fullversion=$(get_full_version "$version") + local fullversion + 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 () { - local version - version=$1 - shift - - local versiontype - versiontype=${1:-full} - shift - - local tagversion - if [ "$versiontype" = full ]; then - tagversion=$(get_full_version "$version") - elif [ "$versiontype" = majorminor ]; then - tagversion=$(get_major_minor_version "$version") - fi - - local tagparts - IFS=' ' read -ra tagparts <<< "$(get_fork_name "$version") $tagversion" - IFS='-'; echo "${tagparts[*]}"; unset IFS +function get_tag() { + local version + version=$1 + shift + + local versiontype + versiontype=${1:-full} + shift + + local tagversion + if [ "$versiontype" = full ]; then + tagversion=$(get_full_version "$version") + elif [ "$versiontype" = majorminor ]; then + tagversion=$(get_major_minor_version "$version") + fi + + local tagparts + IFS=' ' read -ra tagparts <<<"$(get_fork_name "$version") $tagversion" + IFS='-' + echo "${tagparts[*]}" + unset IFS } -function sort_versions () { - local versions=( "$@" ) - local sorted - local lines - local line +function sort_versions() { + local versions=("$@") + local sorted + local lines + local line - IFS=$'\n' - lines="${versions[*]}" - unset IFS + IFS=$'\n' + lines="${versions[*]}" + unset IFS - while IFS='' read -r line; do - sorted+=( "$line" ) - done <<< "$(echo "$lines" | grep "^[0-9]" | sort -r)" + while IFS='' read -r line; do + sorted+=("$line") + done <<<"$(echo "$lines" | grep "^[0-9]" | sort -r)" - while IFS='' read -r line; do - sorted+=( "$line" ) - done <<< "$(echo "$lines" | grep -v "^[0-9]" | sort -r)" + while IFS='' read -r line; do + sorted+=("$line") + done <<<"$(echo "$lines" | grep -v "^[0-9]" | sort -r)" - echo "${sorted[@]}" + echo "${sorted[@]}" } diff --git a/generate-stackbrew-library.sh b/generate-stackbrew-library.sh index a5609fa12..f40879d10 100755 --- a/generate-stackbrew-library.sh +++ b/generate-stackbrew-library.sh @@ -6,29 +6,29 @@ hash git 2>/dev/null || { echo >&2 "git not found, exiting."; } # Used dynamically: print "$array_" $1 # shellcheck disable=SC2034 -array_6='6 boron'; +array_6='6 boron' # shellcheck disable=SC2034 -array_8='8 carbon'; +array_8='8 carbon' # shellcheck disable=SC2034 -array_9='9'; +array_9='9' # shellcheck disable=SC2034 -array_10='10 latest'; +array_10='10 latest' # shellcheck disable=SC2034 -array_chakracore_8='chakracore-8'; +array_chakracore_8='chakracore-8' # 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]}")" -IFS=' ' read -ra versions <<< "$(get_versions)" -IFS=' ' read -ra versions <<< "$(sort_versions "${versions[@]}")" +IFS=' ' read -ra versions <<<"$(get_versions)" +IFS=' ' read -ra versions <<<"$(sort_versions "${versions[@]}")" url='https://github.com/nodejs/docker-node' # get the most recent commit which modified any of "$@" 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" @@ -39,57 +39,61 @@ echo # prints "$2$1$3$1...$N" join() { - local sep="$1"; shift - local out; printf -v out "${sep//%/%%}%s" "$@" - echo "${out#$sep}" + local sep="$1" + shift + local out + printf -v out "${sep//%/%%}%s" "$@" + echo "${out#$sep}" } get_stub() { - local version="$1"; shift - IFS='/' read -ra versionparts <<< "$version" - local stub; eval stub="$(join '_' "${versionparts[@]}" | awk -F. '{ print "$array_" $1 }')"; - echo "$stub" + local version="$1" + shift + IFS='/' read -ra versionparts <<<"$version" + local stub + eval stub="$(join '_' "${versionparts[@]}" | awk -F. '{ print "$array_" $1 }')" + echo "$stub" } for version in "${versions[@]}"; do - # Skip "docs" and other non-docker directories - [ -f "$version/Dockerfile" ] || continue - - stub=$(get_stub "$version") - commit="$(fileCommit "$version")" - fullVersion="$(get_tag "$version" full)" - majorMinorVersion="$(get_tag "$version" majorminor)" - - IFS=' ' read -ra versionAliases <<< "$fullVersion $majorMinorVersion $stub" - # Get supported architectures for a specific version. See details in function.sh - IFS=' ' read -ra supportedArches <<< "$(get_supported_arches "$version" "default")" - - echo "Tags: $(join ', ' "${versionAliases[@]}")" - echo "Architectures: $(join ', ' "${supportedArches[@]}")" - echo "GitCommit: ${commit}" - echo "Directory: ${version}" - echo - - # Get supported variants according to the target architecture. - # See details in function.sh - IFS=' ' read -ra variants <<< "$(get_variants "$(dirname "$version")")" - for variant in "${variants[@]}"; do - # Skip non-docker directories - [ -f "$version/$variant/Dockerfile" ] || continue - - commit="$(fileCommit "$version/$variant")" - - slash='/' - variantAliases=( "${versionAliases[@]/%/-${variant//$slash/-}}" ) - variantAliases=( "${variantAliases[@]//latest-/}" ) - # Get supported architectures for a specific version and variant. - # See details in function.sh - IFS=' ' read -ra supportedArches <<< "$(get_supported_arches "$version" "$variant")" - - echo "Tags: $(join ', ' "${variantAliases[@]}")" - echo "Architectures: $(join ', ' "${supportedArches[@]}")" - echo "GitCommit: ${commit}" - echo "Directory: ${version}/${variant}" - echo - done + # Skip "docs" and other non-docker directories + [ -f "$version/Dockerfile" ] || continue + + stub=$(get_stub "$version") + commit="$(fileCommit "$version")" + fullVersion="$(get_tag "$version" full)" + majorMinorVersion="$(get_tag "$version" majorminor)" + + IFS=' ' read -ra versionAliases <<<"$fullVersion $majorMinorVersion $stub" + # Get supported architectures for a specific version. See details in function.sh + IFS=' ' read -ra supportedArches <<<"$(get_supported_arches "$version" "default")" + + echo "Tags: $(join ', ' "${versionAliases[@]}")" + echo "Architectures: $(join ', ' "${supportedArches[@]}")" + echo "GitCommit: ${commit}" + echo "Directory: ${version}" + echo + + # Get supported variants according to the target architecture. + # See details in function.sh + IFS=' ' read -ra variants <<<"$(get_variants "$(dirname "$version")")" + for variant in "${variants[@]}"; do + # Skip non-docker directories + [ -f "$version/$variant/Dockerfile" ] || continue + + commit="$(fileCommit "$version/$variant")" + + slash='/' + variantAliases=("${versionAliases[@]/%/-${variant//$slash/-}}") + variantAliases=("${variantAliases[@]//latest-/}") + # Get supported architectures for a specific version and variant. + # See details in function.sh + IFS=' ' read -ra supportedArches <<<"$(get_supported_arches "$version" "$variant")" + + echo "Tags: $(join ', ' "${variantAliases[@]}")" + echo "Architectures: $(join ', ' "${supportedArches[@]}")" + echo "GitCommit: ${commit}" + echo "Directory: ${version}/${variant}" + echo + done done diff --git a/generate-stackbrew-pr.sh b/generate-stackbrew-pr.sh index 14b43179b..a4972ae41 100755 --- a/generate-stackbrew-pr.sh +++ b/generate-stackbrew-pr.sh @@ -3,24 +3,24 @@ set -e . functions.sh if [ -z "$1" ]; then - COMMIT_ID="$TRAVIS_COMMIT" - COMMIT_MESSAGE="$TRAVIS_COMMIT_MESSAGE" - BRANCH_NAME="travis-$TRAVIS_BUILD_ID" - GITHUB_USERNAME="nodejs-github-bot" + COMMIT_ID="$TRAVIS_COMMIT" + COMMIT_MESSAGE="$TRAVIS_COMMIT_MESSAGE" + BRANCH_NAME="travis-$TRAVIS_BUILD_ID" + GITHUB_USERNAME="nodejs-github-bot" else - COMMIT_ID="$1" - COMMIT_MESSAGE="$(git show -s --format=%B "$1")" - BRANCH_NAME="travis-$(date +%s)" - if [[ "$(git remote get-url origin)" =~ github.com/([^/]*)/docker-node.git ]]; then - GITHUB_USERNAME="${BASH_REMATCH[1]}" - fi + COMMIT_ID="$1" + COMMIT_MESSAGE="$(git show -s --format=%B "$1")" + BRANCH_NAME="travis-$(date +%s)" + if [[ "$(git remote get-url origin)" =~ github.com/([^/]*)/docker-node.git ]]; then + GITHUB_USERNAME="${BASH_REMATCH[1]}" + fi fi if [[ "$COMMIT_MESSAGE" =~ Merge\ pull\ request\ \#([0-9]*) ]]; then - # This is a merge from a pull request - PR_NUMBER="${BASH_REMATCH[1]}" - COMMIT_MESSAGE="$(printf "%s" "$COMMIT_MESSAGE" | tail -n 1)" + # This is a merge from a pull request + PR_NUMBER="${BASH_REMATCH[1]}" + COMMIT_MESSAGE="$(printf "%s" "$COMMIT_MESSAGE" | tail -n 1)" fi IMAGES_FILE="library/node" @@ -31,134 +31,137 @@ DOCKER_SLUG="nodejs/docker-node" gitpath="../$REPO_NAME" function updated() { - local versions - local images_changed - - IFS=' ' read -ra versions <<< "$(IFS=','; get_versions)" - images_changed=$(git diff --name-only "$COMMIT_ID".."$COMMIT_ID"~1 "${versions[@]}") - - if [ -z "$images_changed" ]; then - return 1 - else - return 0 - fi + local versions + local images_changed + + IFS=' ' read -ra versions <<<"$( + IFS=',' + get_versions + )" + images_changed=$(git diff --name-only "$COMMIT_ID".."$COMMIT_ID"~1 "${versions[@]}") + + if [ -z "$images_changed" ]; then + return 1 + else + return 0 + fi } function auth_header() { - echo "Authorization: token $GITHUB_API_TOKEN" + echo "Authorization: token $GITHUB_API_TOKEN" } function permission_check() { - if [ -z "$GITHUB_API_TOKEN" ]; then - fatal "Environment variable \$GITHUB_API_TOKEN is missing or empty" - fi - - auth="$(curl -H "$(auth_header)" \ - -s \ - "https://api.github.com")" - - if [ "$(echo "$auth" | jq .message)" = "\"Bad credentials\"" ]; then - fatal "Authentication Failed! Invalid \$GITHUB_API_TOKEN" - fi - - auth="$(curl -H "$(auth_header)" \ - -s \ - "https://api.github.com/repos/$ORIGIN_SLUG/collaborators/$GITHUB_USERNAME/permission")" - if [ "$(echo "$auth" | jq .message)" != "null" ]; then - fatal "\$GITHUB_API_TOKEN can't push to https://github.com/$ORIGIN_SLUG.git" - fi + if [ -z "$GITHUB_API_TOKEN" ]; then + fatal "Environment variable \$GITHUB_API_TOKEN is missing or empty" + fi + + auth="$(curl -H "$(auth_header)" \ + -s \ + "https://api.github.com")" + + if [ "$(echo "$auth" | jq .message)" = "\"Bad credentials\"" ]; then + fatal "Authentication Failed! Invalid \$GITHUB_API_TOKEN" + fi + + auth="$(curl -H "$(auth_header)" \ + -s \ + "https://api.github.com/repos/$ORIGIN_SLUG/collaborators/$GITHUB_USERNAME/permission")" + if [ "$(echo "$auth" | jq .message)" != "null" ]; then + fatal "\$GITHUB_API_TOKEN can't push to https://github.com/$ORIGIN_SLUG.git" + fi } function setup_git_author() { - GIT_AUTHOR_NAME="$(git show -s --format="%aN" "$COMMIT_ID")" - GIT_AUTHOR_EMAIL="$(git show -s --format="%aE" "$COMMIT_ID")" - GIT_COMMITTER_NAME="$(git show -s --format="%cN" "$COMMIT_ID")" - GIT_COMMITTER_EMAIL="$(git show -s --format="%cN" "$COMMIT_ID")" + GIT_AUTHOR_NAME="$(git show -s --format="%aN" "$COMMIT_ID")" + GIT_AUTHOR_EMAIL="$(git show -s --format="%aE" "$COMMIT_ID")" + GIT_COMMITTER_NAME="$(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() { - echo "Node: $COMMIT_MESSAGE" + echo "Node: $COMMIT_MESSAGE" } function pr_payload() { - local escaped_message - escaped_message="$(echo "$COMMIT_MESSAGE" | sed -E -e "s/\"/\\\\\"/g")" - echo '{ - "title": "Node: '"$escaped_message"'", - "body": "Commit: nodejs/docker-node@'"$COMMIT_ID"'", - "head": "'"$GITHUB_USERNAME"':'"$BRANCH_NAME"'", - "base": "master" - }' + local escaped_message + escaped_message="$(echo "$COMMIT_MESSAGE" | sed -E -e "s/\"/\\\\\"/g")" + echo '{ + "title": "Node: '"$escaped_message"'", + "body": "Commit: nodejs/docker-node@'"$COMMIT_ID"'", + "head": "'"$GITHUB_USERNAME"':'"$BRANCH_NAME"'", + "base": "master" + }' } function comment_payload() { - local pr_url - pr_url="$1" - echo '{ - "body": "Created PR to the '"$REPO_NAME"' repo ('"$pr_url"')" - }' + local pr_url + pr_url="$1" + echo '{ + "body": "Created PR to the '"$REPO_NAME"' repo ('"$pr_url"')" + }' } if updated; then - permission_check - - # Set Git User Info - setup_git_author - - info "Cloning..." - git clone --depth 50 "https://github.com/$UPSTREAM_SLUG.git" $gitpath 2> /dev/null - - stackbrew="$(./generate-stackbrew-library.sh)" - - cd $gitpath - - echo "$stackbrew" > "$IMAGES_FILE" - git checkout -b "$BRANCH_NAME" - git add "$IMAGES_FILE" - git commit -m "$(message)" - - 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" - - cd - && rm -rf $gitpath - - info "Creating Pull request" - pr_response_payload="$(curl -H "$(auth_header)" \ - -s \ - -X POST \ - -d "$(pr_payload)" \ - "https://api.github.com/repos/$UPSTREAM_SLUG/pulls")" - - url="$(echo "$pr_response_payload" | jq -r .html_url)" - if [ "$url" != "null" ]; then - info "Pull request created at $url" - - if [ ! -z "$PR_NUMBER" ]; then - comment_endpoint="https://api.github.com/repos/$DOCKER_SLUG/issues/$PR_NUMBER/comments" - else - comment_endpoint="https://api.github.com/repos/$DOCKER_SLUG/commits/$COMMIT_ID/comments" - fi - - info "Creating Commit Comment" - commit_response_payload="$(curl -H "$(auth_header)" \ - -s \ - -X POST \ - -d "$(comment_payload "$url")" \ - "$comment_endpoint")" - - if [ "$(echo "$commit_response_payload" | jq .message)" != "null" ]; then - fatal "Error linking the pull request ($error_message)" - else - comment_url="$(echo "$commit_response_payload" | jq -r .html_url)" - info "Created comment at $comment_url" - fi - else - error_message=$(echo "$pr_response_payload" | jq .message) - fatal "Error creating pull request ($error_message)" - fi + permission_check + + # Set Git User Info + setup_git_author + + info "Cloning..." + git clone --depth 50 "https://github.com/$UPSTREAM_SLUG.git" $gitpath 2>/dev/null + + stackbrew="$(./generate-stackbrew-library.sh)" + + cd $gitpath + + echo "$stackbrew" >"$IMAGES_FILE" + git checkout -b "$BRANCH_NAME" + git add "$IMAGES_FILE" + git commit -m "$(message)" + + 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" + + cd - && rm -rf $gitpath + + info "Creating Pull request" + pr_response_payload="$(curl -H "$(auth_header)" \ + -s \ + -X POST \ + -d "$(pr_payload)" \ + "https://api.github.com/repos/$UPSTREAM_SLUG/pulls")" + + url="$(echo "$pr_response_payload" | jq -r .html_url)" + if [ "$url" != "null" ]; then + info "Pull request created at $url" + + if [ ! -z "$PR_NUMBER" ]; then + comment_endpoint="https://api.github.com/repos/$DOCKER_SLUG/issues/$PR_NUMBER/comments" + else + comment_endpoint="https://api.github.com/repos/$DOCKER_SLUG/commits/$COMMIT_ID/comments" + fi + + info "Creating Commit Comment" + commit_response_payload="$(curl -H "$(auth_header)" \ + -s \ + -X POST \ + -d "$(comment_payload "$url")" \ + "$comment_endpoint")" + + if [ "$(echo "$commit_response_payload" | jq .message)" != "null" ]; then + fatal "Error linking the pull request ($error_message)" + else + comment_url="$(echo "$commit_response_payload" | jq -r .html_url)" + info "Created comment at $comment_url" + fi + else + error_message=$(echo "$pr_response_payload" | jq .message) + fatal "Error creating pull request ($error_message)" + fi else - info "No change!" + info "No change!" fi diff --git a/test-build.sh b/test-build.sh index c03607f19..8af85b9a2 100755 --- a/test-build.sh +++ b/test-build.sh @@ -9,18 +9,21 @@ set -uo pipefail # Convert comma delimited cli arguments to arrays # E.g. ./test-build.sh 8,10 slim,onbuild # "8,10" becomes "8 10" and "slim,onbuild" becomes "slim onbuild" -IFS=',' read -ra versions_arg <<< "${1:-}" -IFS=',' read -ra variant_arg <<< "${2:-}" +IFS=',' read -ra versions_arg <<<"${1:-}" +IFS=',' read -ra variant_arg <<<"${2:-}" -function build () { +function build() { local version local tag local variant local full_tag local path - version="$1"; shift - variant="$1"; shift - tag="$1"; shift + version="$1" + shift + variant="$1" + shift + tag="$1" + shift if [ -z "$variant" ]; then 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" } -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 fatal "No valid versions found!" fi @@ -57,10 +60,10 @@ for version in "${versions[@]}"; do # Get supported variants according to the target architecture. # 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 - if [[ "${variants[*]}" =~ "default" ]] || [[ "${variants[*]}" =~ "onbuild" ]] ; then + if [[ "${variants[*]}" =~ "default" ]] || [[ "${variants[*]}" =~ "onbuild" ]]; then build "$version" "" "$tag" fi diff --git a/test-image.sh b/test-image.sh index 9c5d0a25d..bbcc8db3e 100755 --- a/test-image.sh +++ b/test-image.sh @@ -1,18 +1,18 @@ #!/bin/sh if [ "$(node -e "process.stdout.write(process.versions.node)")" != "$1" ]; then - echo "Test for node failed!" - exit 1 + echo "Test for node failed!" + exit 1 fi echo "Test for node succeeded." -if ! npm --version > /dev/null; then - echo "Test for npm failed!" - exit 2 +if ! npm --version >/dev/null; then + echo "Test for npm failed!" + exit 2 fi echo "Test for npm succeeded." -if ! yarn --version > /dev/null; then - echo "Test of yarn failed!" - exit 3 +if ! yarn --version >/dev/null; then + echo "Test of yarn failed!" + exit 3 fi echo "Test for yarn succeeded." diff --git a/update.sh b/update.sh index 8252cae9f..3170a6979 100755 --- a/update.sh +++ b/update.sh @@ -3,11 +3,11 @@ set -ue . 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 - fatal "No valid versions found!" + fatal "No valid versions found!" fi # Global variables @@ -18,100 +18,98 @@ arch=$(get_arch) yarnVersion="$(curl -sSL --compressed https://yarnpkg.com/latest-version)" -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 '> .travis.yml +' >>.travis.yml } 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 - # Skip "docs" and other non-docker directories - [ -f "$version/Dockerfile" ] || continue + # Skip "docs" and other non-docker directories + [ -f "$version/Dockerfile" ] || continue - info "Updating version $version..." + info "Updating version $version..." - parentpath=$(dirname "$version") - versionnum=$(basename "$version") - baseuri=$(get_config "$parentpath" "baseuri") + parentpath=$(dirname "$version") + versionnum=$(basename "$version") + baseuri=$(get_config "$parentpath" "baseuri") - add_stage "$baseuri" "$version" "default" - update_node_version "$baseuri" "$versionnum" "$parentpath/Dockerfile.template" "$version/Dockerfile" & + add_stage "$baseuri" "$version" "default" + update_node_version "$baseuri" "$versionnum" "$parentpath/Dockerfile.template" "$version/Dockerfile" & - # Get supported variants according the target architecture - # See details in function.sh - IFS=' ' read -ra variants <<< "$(get_variants "$parentpath")" + # 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" - update_node_version "$baseuri" "$versionnum" "$parentpath/Dockerfile-$variant.template" "$version/$variant/Dockerfile" "$variant" & - done + for variant in "${variants[@]}"; do + # Skip non-docker directories + [ -f "$version/$variant/Dockerfile" ] || continue + add_stage "$baseuri" "$version" "$variant" + update_node_version "$baseuri" "$versionnum" "$parentpath/Dockerfile-$variant.template" "$version/$variant/Dockerfile" "$variant" & + done done wait