push.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/bin/bash
  2. set -e
  3. # so we can have fancy stuff like !(pattern)
  4. shopt -s extglob
  5. dir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
  6. library="$dir/../library"
  7. logs="$dir/logs"
  8. namespaces='library stackbrew'
  9. library="$(readlink -f "$library")"
  10. logs="$(readlink -f "$logs")"
  11. # TODO actually log stuff
  12. # arg handling: all args are [repo|repo:tag]
  13. usage() {
  14. cat <<EOUSAGE
  15. usage: $0 [options] [repo[:tag] ...]
  16. ie: $0 --all
  17. $0 debian ubuntu:12.04
  18. This script pushes the specified docker images from library that are built
  19. and tagged in the specified namespaces.
  20. options:
  21. --help, -h, -? Print this help message
  22. --all Pushes all docker images built for the given namespaces
  23. --no-push Don't actually push the images to the Docker Hub
  24. --library="$library"
  25. Where to find repository manifest files
  26. --namespaces="$namespaces"
  27. Space separated list of namespaces to tag images in after
  28. building
  29. EOUSAGE
  30. }
  31. opts="$(getopt -o 'h?' --long 'help,all,no-push,library:,logs:,namespaces:' -- "$@" || { usage >&2 && false; })"
  32. eval set -- "$opts"
  33. doPush=1
  34. buildAll=
  35. while true; do
  36. flag=$1
  37. shift
  38. case "$flag" in
  39. --help|-h|'-?')
  40. usage
  41. exit 0
  42. ;;
  43. --all) buildAll=1 ;;
  44. --no-push) doPush= ;;
  45. --library) library="$1" && shift ;;
  46. --logs) logs="$1" && shift ;;
  47. --namespaces) namespaces="$1" && shift ;;
  48. --)
  49. break
  50. ;;
  51. *)
  52. {
  53. echo "error: unknown flag: $flag"
  54. usage
  55. } >&2
  56. exit 1
  57. ;;
  58. esac
  59. done
  60. repos=()
  61. if [ "$buildAll" ]; then
  62. repos=( "$library"/!(MAINTAINERS) )
  63. fi
  64. repos+=( "$@" )
  65. repos=( "${repos[@]%/}" )
  66. if [ "${#repos[@]}" -eq 0 ]; then
  67. echo >&2 'error: no repos specified'
  68. usage >&2
  69. exit 1
  70. fi
  71. for repoTag in "${repos[@]}"; do
  72. repo="${repoTag%%:*}"
  73. tag="${repoTag#*:}"
  74. [ "$repo" != "$tag" ] || tag=
  75. if [ -f "$repo" ]; then
  76. repoFile="$repo"
  77. repo="$(basename "$repoFile")"
  78. repoTag="${repo}${tag:+:$tag}"
  79. else
  80. repoFile="$library/$repo"
  81. fi
  82. repoFile="$(readlink -f "$repoFile")"
  83. # parse the repo library file
  84. IFS=$'\n'
  85. tagList=( $(awk -F ': +' '!/^#|^\s*$/ { print $1 }' "$repoFile") )
  86. unset IFS
  87. tags=()
  88. for tag in "${tagList[@]}"; do
  89. tags+=( "$repo:$tag" )
  90. done
  91. pushes=()
  92. if [ "$repo" = "$repoTag" ]; then
  93. pushes=( "${tags[@]}" )
  94. else
  95. pushes+=( "$repoTag" )
  96. fi
  97. for pushTag in "${pushes[@]}"; do
  98. for namespace in $namespaces; do
  99. if [ "$doPush" ]; then
  100. docker push "$namespace/$pushTag"
  101. else
  102. echo "docker push" "$namespace/$pushTag"
  103. fi
  104. done
  105. done
  106. done