publish.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/bin/bash -eu
  2. # Publish any versions of the docker image not yet pushed to jenkinsci/jenkins
  3. # Arguments:
  4. # -n dry run, do not build or publish images
  5. set -o pipefail
  6. sort-versions() {
  7. if [ "$(uname)" == 'Darwin' ]; then
  8. gsort --version-sort
  9. else
  10. sort --version-sort
  11. fi
  12. }
  13. # Try tagging with and without -f to support all versions of docker
  14. docker-tag() {
  15. local from="jenkinsci/jenkins:$1"
  16. local to="jenkinsci/jenkins:$2"
  17. local out
  18. if out=$(docker tag -f "$from" "$to" 2>&1); then
  19. echo "$out"
  20. else
  21. docker tag "$from" "$to"
  22. fi
  23. }
  24. get-variant() {
  25. local branch
  26. branch=$(git show-ref | grep $(git rev-list -n 1 HEAD) | tail -1 | rev | cut -d/ -f 1 | rev)
  27. if [ -z "$branch" ]; then
  28. >&2 echo "Could not get the current branch name for commit, not in a branch?: $(git rev-list -n 1 HEAD)"
  29. return 1
  30. fi
  31. case "$branch" in
  32. master) echo "" ;;
  33. *) echo "-${branch}" ;;
  34. esac
  35. }
  36. login-token() {
  37. # could use jq .token
  38. curl -q -sSL https://auth.docker.io/token\?service\=registry.docker.io\&scope\=repository:jenkinsci/jenkins:pull | grep -o '"token":"[^"]*"' | cut -d':' -f 2 | xargs echo
  39. }
  40. is-published() {
  41. get-manifest "$1" &> /dev/null
  42. }
  43. get-manifest() {
  44. local tag=$1
  45. curl -q -fsSL -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -H "Authorization: Bearer $TOKEN" "https://index.docker.io/v2/jenkinsci/jenkins/manifests/$tag"
  46. }
  47. get-digest() {
  48. #get-manifest "$1" | jq .config.digest
  49. get-manifest "$1" | grep -A 10 -o '"config".*' | grep digest | head -1 | cut -d':' -f 2,3 | xargs echo
  50. }
  51. get-latest-versions() {
  52. curl -q -fsSL https://api.github.com/repos/jenkinsci/jenkins/tags?per_page=20 | grep '"name": "jenkins-' | egrep -o '[0-9]+(\.[0-9]+)+' | sort-versions | uniq
  53. }
  54. publish() {
  55. local version=$1
  56. local variant=$2
  57. local tag="${version}${variant}"
  58. local sha
  59. local build_opts="--no-cache --pull"
  60. sha=$(curl -q -fsSL "http://repo.jenkins-ci.org/simple/releases/org/jenkins-ci/main/jenkins-war/${version}/jenkins-war-${version}.war.sha1")
  61. docker build --build-arg "JENKINS_VERSION=$version" \
  62. --build-arg "JENKINS_SHA=$sha" \
  63. --tag "jenkinsci/jenkins:${tag}" ${build_opts} .
  64. docker push "jenkinsci/jenkins:${tag}"
  65. }
  66. tag-and-push() {
  67. local source=$1
  68. local target=$2
  69. local digest_source; digest_source=$(get-digest ${tag1})
  70. local digest_target; digest_target=$(get-digest ${tag2})
  71. if [ "$digest_source" == "$digest_target" ]; then
  72. echo "Images ${source} [$digest_source] and ${target} [$digest_target] are already the same, not updating tags"
  73. else
  74. echo "Creating tag ${target} pointing to ${source}"
  75. if [ ! "$dry_run" = true ]; then
  76. docker-tag "jenkinsci/jenkins:${source}" "jenkinsci/jenkins:${target}"
  77. docker push "jenkinsci/jenkins:${source}"
  78. fi
  79. fi
  80. }
  81. publish-latest() {
  82. local version=$1
  83. local variant=$2
  84. # push latest (for master) or the name of the branch (for other branches)
  85. if [ -z "${variant}" ]; then
  86. tag-and-push "${version}${variant}" "latest"
  87. else
  88. tag-and-push "${version}${variant}" "${variant#-}"
  89. fi
  90. }
  91. publish-lts() {
  92. local version=$1
  93. local variant=$2
  94. tag-and-push "${version}" "lts${variant}"
  95. }
  96. dry_run=false
  97. if [ "-n" == "${1:-}" ]; then
  98. dry_run=true
  99. fi
  100. if [ "$dry_run" = true ]; then
  101. echo "Dry run, will not build or publish images"
  102. fi
  103. TOKEN=$(login-token)
  104. variant=$(get-variant)
  105. lts_version=""
  106. version=""
  107. for version in $(get-latest-versions); do
  108. if is-published "$version$variant"; then
  109. echo "Tag is already published: $version$variant"
  110. else
  111. echo "Publishing version: $version$variant"
  112. if [ ! "$dry_run" = true ]; then
  113. publish "$version" "$variant"
  114. fi
  115. fi
  116. # Update lts tag
  117. if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  118. lts_version="${version}"
  119. fi
  120. done
  121. publish-latest "${version}" "${variant}"
  122. if [ -n "${lts_version}" ]; then
  123. publish-lts "${lts_version}" "${variant}"
  124. fi