test-pr.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/bash
  2. set -eo pipefail
  3. dir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
  4. usage() {
  5. cat <<-EOUSAGE
  6. usage: $0 [PR number] [repo[:tag]]
  7. ie: $0 1024
  8. $0 9001 debian php django
  9. $0 0 hylang # special case that runs against local directory
  10. This script builds and tests the specified pull request to official-images and
  11. provides ouput in markdown for commenting on the pull request.
  12. EOUSAGE
  13. }
  14. pull="$1"
  15. shift || { usage >&2 && exit 1; }
  16. if [ -z "$BASHBREW_SECOND_STAGE" ]; then
  17. docker build --pull -t bashbrew "$dir" > /dev/null
  18. if [ "$pull" = '0' ]; then
  19. name="bashbrew-test-local-$RANDOM"
  20. else
  21. name="bashbrew-test-pr-$pull"
  22. fi
  23. exec docker run \
  24. -it --rm \
  25. --name "$name" \
  26. -e BASHBREW_SECOND_STAGE=1 \
  27. -v /var/run/docker.sock:/var/run/docker.sock \
  28. -w /usr/src/pr \
  29. bashbrew /usr/src/official-images/test-pr.sh "$pull" "$@"
  30. fi
  31. if [ -d .git ]; then
  32. echo >&2 'error: something has gone horribly wrong; .git already exists'
  33. echo >&2 ' why do you have BASHBREW_SECOND_STAGE set?'
  34. exit 1
  35. fi
  36. if [ "$pull" = '0' ]; then
  37. cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"
  38. commit='FAKE'
  39. else
  40. # TODO we only have "git version 2.4.1" which doesn't support "clone -q" :(
  41. git init -q .
  42. git remote add origin https://github.com/docker-library/official-images.git
  43. git fetch -q origin
  44. git reset -q --hard origin/master
  45. git config user.name 'nobody'
  46. git config user.email '[email protected]'
  47. git fetch -q origin "pull/$pull/head:pr-$pull"
  48. git merge -q --no-edit "pr-$pull"
  49. commit="$(git log -1 --format=format:%h "pr-$pull")"
  50. fi
  51. if [ "$#" -eq 0 ]; then
  52. IFS=$'\n'
  53. files=( $(git diff --name-only origin/master...HEAD -- library | xargs -n1 basename) )
  54. unset IFS
  55. # TODO narrow this down into groups of the exact tags for each image that changed >:)
  56. else
  57. files=( "$@" )
  58. fi
  59. if [ ${#files[@]} -eq 0 ]; then
  60. echo >&2 'no files in library/ changed in PR #'"$pull"
  61. exit 0
  62. fi
  63. join() {
  64. sep="$1"
  65. arg1="$2"
  66. shift 2
  67. echo -n "$arg1"
  68. [ $# -gt 0 ] && printf "${sep}%s" "$@"
  69. }
  70. echo 'Build test of' '#'"$pull"';' "$commit" '(`'"$(join '`, `' "${files[@]}")"'`):'
  71. failed=
  72. for img in "${files[@]}"; do
  73. echo
  74. echo '```console'
  75. echo '$ bashbrew build "'"$img"'"'
  76. if ./bashbrew/bashbrew.sh build "$img"; then
  77. echo '$ bashbrew list --uniq "$url" | xargs test/run.sh'
  78. if ! ./bashbrew/bashbrew.sh list --uniq "$img" | xargs ./test/run.sh; then
  79. failed=1
  80. fi
  81. else
  82. failed=1
  83. fi
  84. echo '```'
  85. done
  86. if [ "$failed" ]; then
  87. echo
  88. echo 'There is at least one failure in the above build log.'
  89. fi