release.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/env bash
  2. #
  3. # Release a new version of cpp-httplib.
  4. #
  5. # Usage: ./release.sh [--run]
  6. #
  7. # By default, runs in dry-run mode (no changes made).
  8. # Pass --run to actually update files, commit, tag, and push.
  9. #
  10. # This script:
  11. # 1. Reads the current version from httplib.h
  12. # 2. Checks that the working directory is clean
  13. # 3. Verifies CI status of the latest commit (all must pass except abidiff)
  14. # 4. Determines the next version automatically:
  15. # - abidiff passed → patch bump (e.g., 0.38.0 → 0.38.1)
  16. # - abidiff failed → minor bump (e.g., 0.38.1 → 0.39.0)
  17. # 5. Updates httplib.h and docs-src/config.toml
  18. # 6. Commits, tags (vX.Y.Z), and pushes
  19. set -euo pipefail
  20. DRY_RUN=1
  21. if [ "${1:-}" = "--run" ]; then
  22. DRY_RUN=0
  23. shift
  24. fi
  25. if [ $# -ne 0 ]; then
  26. echo "Usage: $0 [--run]"
  27. exit 1
  28. fi
  29. # --- Step 1: Read current version from httplib.h ---
  30. CURRENT_VERSION=$(sed -n 's/^#define CPPHTTPLIB_VERSION "\([^"]*\)"/\1/p' httplib.h)
  31. IFS='.' read -r V_MAJOR V_MINOR V_PATCH <<< "$CURRENT_VERSION"
  32. echo "==> Current version: $CURRENT_VERSION"
  33. # --- Step 2: Check working directory is clean ---
  34. if [ -n "$(git status --porcelain)" ]; then
  35. echo "Error: working directory is not clean"
  36. exit 1
  37. fi
  38. # --- Step 3: Check CI status of the latest commit ---
  39. echo ""
  40. echo "==> Checking CI status of the latest commit..."
  41. HEAD_SHA=$(git rev-parse HEAD)
  42. HEAD_SHORT=$(git rev-parse --short HEAD)
  43. echo " Latest commit: $HEAD_SHORT"
  44. # Fetch all workflow runs for the HEAD commit
  45. RUNS=$(gh run list --json name,conclusion,headSha \
  46. --jq "[.[] | select(.headSha == \"$HEAD_SHA\")]")
  47. NUM_RUNS=$(echo "$RUNS" | jq 'length')
  48. if [ "$NUM_RUNS" -eq 0 ]; then
  49. echo "Error: No CI runs found for commit $HEAD_SHORT."
  50. echo " Wait for CI to complete before releasing."
  51. exit 1
  52. fi
  53. echo " Found $NUM_RUNS workflow run(s):"
  54. FAILED=0
  55. ABIDIFF_PASSED=0
  56. while IFS=$'\t' read -r name conclusion; do
  57. if [[ "$name" == *abidiff* ]] || [[ "$name" == *abi* && "$name" != *stability* ]]; then
  58. if [ "$conclusion" = "success" ]; then
  59. echo " [ OK ] $name"
  60. ABIDIFF_PASSED=1
  61. else
  62. echo " [FAIL] $name ($conclusion) → ABI break detected, minor bump"
  63. ABIDIFF_PASSED=0
  64. fi
  65. continue
  66. fi
  67. if [ "$conclusion" = "success" ]; then
  68. echo " [ OK ] $name"
  69. else
  70. echo " [FAIL] $name ($conclusion)"
  71. FAILED=1
  72. fi
  73. done < <(echo "$RUNS" | jq -r '.[] | [.name, .conclusion] | @tsv')
  74. if [ "$FAILED" -eq 1 ]; then
  75. echo ""
  76. echo "Error: Some CI checks failed. Fix them before releasing."
  77. exit 1
  78. fi
  79. echo " All non-abidiff CI checks passed."
  80. # --- Step 4: Determine new version ---
  81. if [ "$ABIDIFF_PASSED" -eq 1 ]; then
  82. NEW_PATCH=$((V_PATCH + 1))
  83. NEW_VERSION="$V_MAJOR.$V_MINOR.$NEW_PATCH"
  84. echo ""
  85. echo "==> abidiff passed → patch bump"
  86. else
  87. NEW_MINOR=$((V_MINOR + 1))
  88. NEW_VERSION="$V_MAJOR.$NEW_MINOR.0"
  89. echo ""
  90. echo "==> abidiff failed → minor bump"
  91. fi
  92. VERSION_HEX=$(printf "0x%02x%02x%02x" "${NEW_VERSION%%.*}" "$(echo "$NEW_VERSION" | cut -d. -f2)" "${NEW_VERSION##*.}")
  93. if [ "$DRY_RUN" -eq 1 ]; then
  94. echo "==> [DRY RUN] New version: $NEW_VERSION ($VERSION_HEX)"
  95. else
  96. echo "==> New version: $NEW_VERSION ($VERSION_HEX)"
  97. fi
  98. # --- Step 5: Update files ---
  99. echo ""
  100. if [ "$DRY_RUN" -eq 1 ]; then
  101. echo "==> [DRY RUN] Would update httplib.h:"
  102. echo " CPPHTTPLIB_VERSION = \"$NEW_VERSION\""
  103. echo " CPPHTTPLIB_VERSION_NUM = \"$VERSION_HEX\""
  104. echo ""
  105. echo "==> [DRY RUN] Would update docs-src/config.toml:"
  106. echo " version = \"$NEW_VERSION\""
  107. echo ""
  108. echo "==> [DRY RUN] Would commit, tag v$NEW_VERSION and latest, and push."
  109. echo ""
  110. echo "==> Dry run complete. No changes were made."
  111. else
  112. echo "==> Updating httplib.h..."
  113. sed -i '' "s/#define CPPHTTPLIB_VERSION \"[^\"]*\"/#define CPPHTTPLIB_VERSION \"$NEW_VERSION\"/" httplib.h
  114. sed -i '' "s/#define CPPHTTPLIB_VERSION_NUM \"0x[0-9a-fA-F]*\"/#define CPPHTTPLIB_VERSION_NUM \"$VERSION_HEX\"/" httplib.h
  115. echo " CPPHTTPLIB_VERSION = \"$NEW_VERSION\""
  116. echo " CPPHTTPLIB_VERSION_NUM = \"$VERSION_HEX\""
  117. echo ""
  118. echo "==> Updating docs-src/config.toml..."
  119. sed -i '' "s/^version = \"[^\"]*\"/version = \"$NEW_VERSION\"/" docs-src/config.toml
  120. echo " version = \"$NEW_VERSION\""
  121. # --- Step 6: Commit, tag, and push ---
  122. echo ""
  123. echo "==> Committing and tagging..."
  124. git add httplib.h docs-src/config.toml
  125. git commit -m "Release v$NEW_VERSION"
  126. git tag "v$NEW_VERSION"
  127. git tag -f "latest"
  128. echo ""
  129. echo "==> Pushing..."
  130. git push && git push --tags --force
  131. echo ""
  132. echo "==> Released v$NEW_VERSION"
  133. fi