test_kit_docker_push.sh 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/bash
  2. set -e
  3. [ -n "$GHCR_USER" ] && echo "$GHCR_PASSWORD" | docker login -u"$GHCR_USER" --password-stdin ghcr.io
  4. if ! (docker info | grep Username) > /dev/null 2>&1; then
  5. echo "Can't authorise to GHCR docker registry"
  6. exit 1
  7. fi
  8. # Helper to fix tag naming from branch -> docker tag allowance
  9. sanitize_tag() {
  10. local name=$1
  11. # Replace / with _
  12. name=$(echo "$name" | tr '/' '_')
  13. # Remove all characters that are not allowed in Docker tags
  14. # Docker tag rules: https://docs.docker.com/engine/reference/commandline/tag/
  15. # Tags can only contain lowercase and uppercase letters, digits, underscores, periods, and dashes
  16. name=$(echo "$name" | sed 's/[^a-zA-Z0-9_.-]//g')
  17. echo "$name"
  18. }
  19. # Current branch name
  20. if [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then
  21. current_branch="$GITHUB_HEAD_REF"
  22. else
  23. current_branch="$GITHUB_REF_NAME"
  24. fi
  25. should_push_main=0
  26. if [ "$TEST_RESULT" != "failure" ]; then
  27. should_push_main=1
  28. fi
  29. hub_repo="ghcr.io/${REPO_OWNER}/manticoresearch"
  30. img_url="${hub_repo}:test-kit-${BUILD_COMMIT}"
  31. images=("$img_url")
  32. [[ $current_branch == "master" ]] \
  33. && img_url_latest="${hub_repo}:test-kit-latest" \
  34. && images+=("$img_url_latest") \
  35. || img_url_latest=""
  36. # Get the latest tag from the git references
  37. # Check if any tag exists
  38. latest_tag=$(git describe --abbrev=0 --tags 2>/dev/null || echo "")
  39. # Assign the current branch or tag to the appropriate variable
  40. if [ -n "$latest_tag" ]; then
  41. img_url_tag="${hub_repo}:test-kit-$(sanitize_tag "$latest_tag")"
  42. images+=("$img_url_tag")
  43. fi
  44. if [ "$current_branch" != "master" ]; then
  45. img_url_branch="${hub_repo}:test-kit-$(sanitize_tag "$current_branch")"
  46. images+=("$img_url_branch")
  47. fi
  48. img_url_hash=
  49. if [ "${HASH_TAG_OK}" == "true" ] && [ -n "${SOURCE_HASH}" ]; then
  50. if [ -n "${BRANCH_TAG}" ]; then
  51. hash_branch_tag="${BRANCH_TAG}"
  52. else
  53. hash_branch_tag="$(sanitize_tag "$current_branch")"
  54. fi
  55. img_url_hash="${hub_repo}:test-kit-${hash_branch_tag}-${SOURCE_HASH}"
  56. images+=("$img_url_hash")
  57. fi
  58. # if we should skip pushing main branch and only commit, we reset latest tag
  59. if [ "$should_push_main" -eq 0 ]; then
  60. img_url_latest=
  61. fi
  62. echo "Going to push to '$img_url' and ('$img_url_latest', '$img_url_tag', '$img_url_branch', '$img_url_hash') (if not empty) if there's access to the registry"
  63. # exporting the image, it also squashes all the layers into one
  64. docker import ./manticore_test_kit.img "$img_url"
  65. [ -n "$img_url_latest" ] && docker tag "$img_url" "$img_url_latest"
  66. [ -n "$img_url_tag" ] && docker tag "$img_url" "$img_url_tag"
  67. [ -n "$img_url_branch" ] && docker tag "$img_url" "$img_url_branch"
  68. [ -n "$img_url_hash" ] && docker tag "$img_url" "$img_url_hash"
  69. # pusing to ghcr.io
  70. [ -n "$GHCR_USER" ] && for img in "${images[@]}"; do
  71. docker push "$img" \
  72. && echo "❗ Pushed the image to $img" \
  73. && echo "Pushed test-kit to $img" >> "$GITHUB_STEP_SUMMARY" \
  74. || echo "❗ Couldn't push the image to $img"
  75. done || echo "Skipped pushing to repo, because GHCR_USER is not set"