test-pr.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/bin/bash
  2. set -eo pipefail
  3. # TODO something clever with this pattern to get the exact list of _tags_ which have changed, not just repos:
  4. #format='{{ range .Entries }}{{ join " " (join ":" $.RepoName (.Tags | first)) .GitRepo .GitFetch .GitCommit .Directory }}{{ "\n" }}{{ end }}'
  5. #comm -13 \
  6. # <(bashbrew cat -f "$format" https://github.com/docker-library/official-images/raw/master/library/docker | sort) \
  7. # <(bashbrew cat -f "$format" https://raw.githubusercontent.com/infosiftr/stackbrew/d92ffa4b5f8a558c22c5d0a7e0f33bff8fae990b/library/docker | sort) \
  8. # | cut -d' ' -f1
  9. # make sure we can GTFO
  10. trap 'echo >&2 Ctrl+C captured, exiting; exit 1' SIGINT
  11. # start with an error if Docker isn't working...
  12. docker version > /dev/null
  13. dir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
  14. usage() {
  15. cat <<-EOUSAGE
  16. usage: $0 [PR number] [repo[:tag]]
  17. ie: $0 1024
  18. $0 9001 debian php django
  19. $0 0 hylang # special case that runs against local directory
  20. This script builds and tests the specified pull request to official-images and
  21. provides ouput in markdown for commenting on the pull request.
  22. EOUSAGE
  23. }
  24. pull="$1"
  25. shift || { usage >&2 && exit 1; }
  26. if [ -z "$BASHBREW_SECOND_STAGE" ]; then
  27. dockerImage='bashbrew'
  28. docker build --pull -t "$dockerImage" "$dir" > /dev/null
  29. args=()
  30. if [ "$pull" = '0' ]; then
  31. args+=( --name "bashbrew-test-local-$RANDOM" )
  32. else
  33. args+=( --name "bashbrew-test-pr-$pull" )
  34. fi
  35. args+=(
  36. -v /var/run/docker.sock:/var/run/docker.sock
  37. --group-add 0
  38. -v /etc/passwd:/etc/passwd:ro
  39. -v /etc/group:/etc/group:ro
  40. )
  41. if getent group docker &> /dev/null; then
  42. args+=( --group-add "$(getent group docker | cut -d: -f3)" )
  43. fi
  44. # if we don't have DOCKER_HOST set, let's bind-mount cache for speed!
  45. if [ -z "$DOCKER_HOST" ]; then
  46. export BASHBREW_CACHE="${BASHBREW_CACHE:-${XDG_CACHE_HOME:-$HOME/.cache}/bashbrew}" # resolve path to current "host-side" cache directory
  47. mkdir -p "$BASHBREW_CACHE" # ensure it's created by our user, not root
  48. export BASHBREW_CACHE="$(cd "$BASHBREW_CACHE" && pwd -P)" # readlink -f
  49. args+=(
  50. -v "$BASHBREW_CACHE":/bashbrew-cache
  51. -e BASHBREW_CACHE=/bashbrew-cache
  52. # make sure our user in the container can read it
  53. --group-add "$(stat -c '%g' "$BASHBREW_CACHE")"
  54. )
  55. else
  56. dockerGid="$(
  57. docker run -i --rm "${args[@]}" "$dockerImage" sh -e <<-'EOSH'
  58. exec 2>/dev/null
  59. stat -c '%g' /var/run/docker.sock \
  60. || getent group docker | cut -d: -f3
  61. EOSH
  62. )" || true
  63. if [ "$dockerGid" ]; then
  64. args+=( --group-add "$dockerGid" )
  65. fi
  66. fi
  67. args+=(
  68. --user "$(id -u)":"$(id -g)"
  69. $(id -G | xargs -n1 echo --group-add)
  70. -e BASHBREW_SECOND_STAGE=1
  71. )
  72. for e in "${!BASHBREW_@}"; do
  73. case "$e" in
  74. BASHBREW_SECOND_STAGE|BASHBREW_CACHE|BASHBREW_LIBRARY) ;;
  75. *)
  76. args+=( -e "$e" )
  77. ;;
  78. esac
  79. done
  80. cmd=( ./test-pr.sh "$pull" "$@" )
  81. if [ -t 1 ]; then
  82. # only add "-t" if we have a TTY
  83. args+=( -t )
  84. fi
  85. exec docker run -i --rm "${args[@]}" "$dockerImage" "${cmd[@]}"
  86. fi
  87. if [ -d .git ]; then
  88. echo >&2 'error: something has gone horribly wrong; .git already exists'
  89. echo >&2 ' why do you have BASHBREW_SECOND_STAGE set?'
  90. exit 1
  91. fi
  92. if [ "$pull" = '0' ]; then
  93. commit='FAKE'
  94. else
  95. dir="$(mktemp -d)"
  96. trap "rm -rf '$dir'" EXIT
  97. cd "$dir"
  98. # TODO we only have "git version 2.4.1" which doesn't support "clone -q" :(
  99. git init -q .
  100. git remote add origin https://github.com/docker-library/official-images.git
  101. git fetch -q origin
  102. git reset -q --hard origin/master
  103. git config user.name 'nobody'
  104. git config user.email '[email protected]'
  105. git fetch -q origin "pull/$pull/head:pr-$pull"
  106. git merge -q --no-edit "pr-$pull" > /dev/null
  107. commit="$(git log -1 --format=format:%h "pr-$pull")"
  108. fi
  109. export BASHBREW_LIBRARY="$PWD/library"
  110. if [ "$#" -eq 0 ]; then
  111. IFS=$'\n'
  112. files=( $(git diff --name-only origin/master...HEAD -- library | xargs -n1 basename) )
  113. unset IFS
  114. # TODO narrow this down into groups of the exact tags for each image that changed >:)
  115. else
  116. files=( "$@" )
  117. fi
  118. if [ ${#files[@]} -eq 0 ]; then
  119. echo >&2 'no files in library/ changed in PR #'"$pull"
  120. exit 0
  121. fi
  122. join() {
  123. sep="$1"
  124. arg1="$2"
  125. shift 2
  126. echo -n "$arg1"
  127. [ $# -gt 0 ] && printf "${sep}%s" "$@"
  128. }
  129. IFS=$'\n'
  130. files=( $(bashbrew list --repos --uniq --build-order "${files[@]}") )
  131. unset IFS
  132. echo 'Build test of' '#'"$pull"';' "$commit"';' '`'"${BASHBREW_ARCH:-amd64}"'`' '(`'"$(join '`, `' "${files[@]}")"'`):'
  133. declare -A failedBuild=() failedTests=()
  134. for img in "${files[@]}"; do
  135. IFS=$'\n'
  136. uniqImgs=( $(bashbrew list --uniq --build-order "$img") )
  137. unset IFS
  138. echo
  139. echo '```console'
  140. for uniqImg in "${uniqImgs[@]}"; do
  141. imgRepo="${uniqImg%%:*}"
  142. echo
  143. echo '$ bashbrew build' "$uniqImg"
  144. if bashbrew build --pull=missing "$uniqImg"; then
  145. echo
  146. echo '$ test/run.sh' "$uniqImg"
  147. if ! ./test/run.sh "$uniqImg"; then
  148. failedTests[$imgRepo]+=" $uniqImg"
  149. fi
  150. else
  151. failedBuild[$imgRepo]+=" $uniqImg"
  152. fi
  153. echo
  154. done
  155. echo '```'
  156. done
  157. echo
  158. if [ "${#failedBuild[@]}" -gt 0 ]; then
  159. echo 'The following images failed to build:'
  160. echo
  161. for repo in "${!failedBuild[@]}"; do
  162. echo '- `'"$repo"'`:'
  163. for img in ${failedBuild[$repo]}; do
  164. echo ' - `'"$img"'`'
  165. done
  166. done
  167. echo
  168. fi
  169. if [ "${#failedTests[@]}" -gt 0 ]; then
  170. echo
  171. echo 'The following images failed at least one test:'
  172. echo
  173. for repo in "${!failedTests[@]}"; do
  174. echo '- `'"$repo"'`:'
  175. for img in ${failedTests[$repo]}; do
  176. echo ' - `'"$img"'`'
  177. done
  178. done
  179. echo
  180. fi