release.sh 5.6 KB

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