test-pr.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_DEBUG
  71. -e BASHBREW_SECOND_STAGE=1
  72. )
  73. cmd=( ./test-pr.sh "$pull" "$@" )
  74. exec docker run -it --rm "${args[@]}" "$dockerImage" "${cmd[@]}"
  75. fi
  76. if [ -d .git ]; then
  77. echo >&2 'error: something has gone horribly wrong; .git already exists'
  78. echo >&2 ' why do you have BASHBREW_SECOND_STAGE set?'
  79. exit 1
  80. fi
  81. if [ "$pull" = '0' ]; then
  82. commit='FAKE'
  83. else
  84. dir="$(mktemp -d)"
  85. trap "rm -rf '$dir'" EXIT
  86. cd "$dir"
  87. # TODO we only have "git version 2.4.1" which doesn't support "clone -q" :(
  88. git init -q .
  89. git remote add origin https://github.com/docker-library/official-images.git
  90. git fetch -q origin
  91. git reset -q --hard origin/master
  92. git config user.name 'nobody'
  93. git config user.email '[email protected]'
  94. git fetch -q origin "pull/$pull/head:pr-$pull"
  95. git merge -q --no-edit "pr-$pull" > /dev/null
  96. commit="$(git log -1 --format=format:%h "pr-$pull")"
  97. fi
  98. export BASHBREW_LIBRARY="$PWD/library"
  99. if [ "$#" -eq 0 ]; then
  100. IFS=$'\n'
  101. files=( $(git diff --name-only origin/master...HEAD -- library | xargs -n1 basename) )
  102. unset IFS
  103. # TODO narrow this down into groups of the exact tags for each image that changed >:)
  104. else
  105. files=( "$@" )
  106. fi
  107. if [ ${#files[@]} -eq 0 ]; then
  108. echo >&2 'no files in library/ changed in PR #'"$pull"
  109. exit 0
  110. fi
  111. join() {
  112. sep="$1"
  113. arg1="$2"
  114. shift 2
  115. echo -n "$arg1"
  116. [ $# -gt 0 ] && printf "${sep}%s" "$@"
  117. }
  118. IFS=$'\n'
  119. files=( $(bashbrew list --repos --uniq --build-order "${files[@]}") )
  120. unset IFS
  121. echo 'Build test of' '#'"$pull"';' "$commit" '(`'"$(join '`, `' "${files[@]}")"'`):'
  122. failedBuild=()
  123. failedTests=()
  124. for img in "${files[@]}"; do
  125. IFS=$'\n'
  126. uniqImgs=( $(bashbrew list --uniq "$img") )
  127. unset IFS
  128. echo
  129. echo '```console'
  130. for uniqImg in "${uniqImgs[@]}"; do
  131. echo
  132. echo '$ bashbrew build' "$uniqImg"
  133. if bashbrew build --pull=missing "$uniqImg"; then
  134. echo
  135. echo '$ test/run.sh' "$uniqImg"
  136. if ! ./test/run.sh "$uniqImg"; then
  137. failedTests+=( "$uniqImg" )
  138. fi
  139. else
  140. failedBuild+=( "$uniqImg" )
  141. fi
  142. echo
  143. done
  144. echo '```'
  145. done
  146. if [ "${#failedBuild[@]}" -gt 0 ]; then
  147. echo
  148. echo 'The following images failed to build:' "${failedBuild[@]}"
  149. fi
  150. if [ "${#failedTests[@]}" -gt 0 ]; then
  151. echo
  152. echo 'The following images failed at least one test:' "${failedTests[@]}"
  153. fi