release_dockerhub.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/env bash
  2. # bash strict mode
  3. set -euo pipefail
  4. # Check if exactly one argument is supplied
  5. if [ "$#" -ne 2 ]; then
  6. echo "Usage: $0 <docker-username> <version>" >&2
  7. exit 1
  8. fi
  9. DOCKER_USERNAME=${1}
  10. VERSION_STR=${2}
  11. # Ensure jq is installed
  12. if ! command -v jq &> /dev/null; then
  13. echo "jq could not be found, please install jq to continue."
  14. exit 1
  15. fi
  16. # Validate version format
  17. if [[ $VERSION_STR =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  18. echo "Validated version number"
  19. else
  20. echo "Argument must be in the form x.x.x, where x is one or more numbers." >&2
  21. exit 2
  22. fi
  23. if [ -z "${DOCKER_USERNAME}" ]; then
  24. echo "Docker username is required" >&2
  25. exit 3
  26. fi
  27. # Determine architecture
  28. ARCH=$(uname -m)
  29. case "$ARCH" in
  30. aarch64) ARCH_SUFFIX="arm64" ;;
  31. arm64) ARCH_SUFFIX="arm64" ;;
  32. x86_64) ARCH_SUFFIX="amd64" ;;
  33. *) echo "Unsupported architecture" >&2; exit 7 ;;
  34. esac
  35. # Set the Miniconda URL based on architecture
  36. if [ "$ARCH_SUFFIX" == "arm64" ]; then
  37. MINICONDA_URL=https://repo.anaconda.com/miniconda/Miniconda3-py39_23.11.0-1-Linux-aarch64.sh
  38. else
  39. MINICONDA_URL=https://repo.anaconda.com/miniconda/Miniconda3-py39_23.11.0-1-Linux-x86_64.sh
  40. fi
  41. echo "------------------------------------------"
  42. echo "Building images for $ARCH_SUFFIX - ${VERSION_STR}"
  43. echo "------------------------------------------"
  44. # Build and push version-specific tag
  45. docker build -t ${DOCKER_USERNAME}/swe-agent:${VERSION_STR}-${ARCH_SUFFIX} --build-arg MINICONDA_URL=${MINICONDA_URL} -f docker/swe.Dockerfile .
  46. docker push ${DOCKER_USERNAME}/swe-agent:${VERSION_STR}-${ARCH_SUFFIX}
  47. # Build and push latest tag
  48. docker build -t ${DOCKER_USERNAME}/swe-agent:latest-${ARCH_SUFFIX} --build-arg MINICONDA_URL=${MINICONDA_URL} -f docker/swe.Dockerfile .
  49. docker push ${DOCKER_USERNAME}/swe-agent:latest-${ARCH_SUFFIX}
  50. # Repeat the build and push process for other Dockerfiles as necessary
  51. echo "------------------------------------------"
  52. echo "Building of all images for $ARCH_SUFFIX - ${VERSION_STR} complete"
  53. echo "------------------------------------------"
  54. read -p "Do you want to proceed with manifest creation for version and latest tags? (yes/no) " answer
  55. if [ "$answer" != "yes" ]; then
  56. echo "Skipping manifest creation. Process complete."
  57. exit 0
  58. fi
  59. # Function to check if a specific tag exists for an image on Docker Hub
  60. check_image_tag_exists() {
  61. local username=$1
  62. local image=$2
  63. local tag=$3
  64. local token=$(curl -s "https://auth.docker.io/token?service=registry.docker.io&scope=repository:${username}/${image}:pull" | jq -r .token)
  65. local tag_exists=$(curl -s -H "Authorization: Bearer ${token}" "https://registry-1.docker.io/v2/${username}/${image}/tags/list" | jq -r ".tags | .[] | select(. == \"${tag}\")")
  66. if [[ -z "$tag_exists" ]]; then
  67. echo "ERROR: The image ${username}/${image}:${tag} does not exist on Docker Hub." >&2
  68. echo "Please push the image before creating the manifest." >&2
  69. exit 9
  70. else
  71. echo "The image ${username}/${image}:${tag} exists on Docker Hub."
  72. fi
  73. }
  74. for ARCH in "amd64" "arm64"; do
  75. check_image_tag_exists ${DOCKER_USERNAME} "swe-agent" "${VERSION_STR}-${ARCH}"
  76. check_image_tag_exists ${DOCKER_USERNAME} "swe-agent" "latest-${ARCH}"
  77. done
  78. # Create and push the manifest for the version tag
  79. export DOCKER_CLI_EXPERIMENTAL=enabled
  80. docker manifest create -a ${DOCKER_USERNAME}/swe-agent:${VERSION_STR} \
  81. ${DOCKER_USERNAME}/swe-agent:${VERSION_STR}-amd64 \
  82. ${DOCKER_USERNAME}/swe-agent:${VERSION_STR}-arm64
  83. docker manifest annotate ${DOCKER_USERNAME}/swe-agent:${VERSION_STR} ${DOCKER_USERNAME}/swe-agent:${VERSION_STR}-arm64 --os linux --arch arm64
  84. docker manifest annotate ${DOCKER_USERNAME}/swe-agent:${VERSION_STR} ${DOCKER_USERNAME}/swe-agent:${VERSION_STR}-amd64 --os linux --arch amd64
  85. docker manifest push ${DOCKER_USERNAME}/swe-agent:${VERSION_STR}
  86. # Create and push the manifest for the latest tag
  87. docker manifest create -a ${DOCKER_USERNAME}/swe-agent:latest \
  88. ${DOCKER_USERNAME}/swe-agent:latest-amd64 \
  89. ${DOCKER_USERNAME}/swe-agent:latest-arm64
  90. docker manifest annotate ${DOCKER_USERNAME}/swe-agent:latest ${DOCKER_USERNAME}/swe-agent:latest-arm64 --os linux --arch arm64
  91. docker manifest annotate ${DOCKER_USERNAME}/swe-agent:latest ${DOCKER_USERNAME}/swe-agent:latest-amd64 --os linux --arch amd64
  92. docker manifest push ${DOCKER_USERNAME}/swe-agent:latest
  93. echo "Manifests for version ${VERSION_STR} and latest created and pushed."
  94. docker login || {
  95. echo "Failed to login to dockerhub" >&2
  96. exit 8
  97. }
  98. # Let's get serious
  99. # Function to execute upon exit
  100. on_error() {
  101. echo "====> ERROR!!! IMPORTANT: Make sure if you've already pushed something to dockerhub or pushed the tag to github!" >&2
  102. }
  103. trap on_error ERR
  104. read -p "Do you want to proceed with git tag (v${VERSION_STR}) creation and pushing to github? (yes/no) " answer
  105. if [ "$answer" != "yes" ]; then
  106. echo "Skipping git tag creation and pushing to github. Process complete."
  107. exit 0
  108. fi
  109. git tag v${VERSION_STR} || {
  110. echo "Failed to create a tag in git" >&2
  111. exit 5
  112. }
  113. echo "🔥 Tag v${VERSION_STR} created in git!"
  114. git push origin v${VERSION_STR} || {
  115. echo "Failed to push the tag to github" >&2
  116. exit 6
  117. }
  118. echo "🔥 Tag v${VERSION_STR} pushed to github"
  119. for image in "${IMAGES[@]}"; do
  120. docker push ${image}
  121. echo "🔥 Pushed ${image} to dockerhub"
  122. done