docker-publish-daily-beta.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. #!/bin/bash
  2. # Daily beta build: build Docker image, push to registries, create/update GitHub prerelease
  3. #
  4. # Usage:
  5. # ./docker-publish-daily-beta.sh [--parallel] [--ghcr-only] [--dockerhub-only] [--skip-release]
  6. #
  7. # Examples:
  8. # ./docker-publish-daily-beta.sh # Full daily beta workflow
  9. # ./docker-publish-daily-beta.sh --parallel # Build both archs simultaneously
  10. # ./docker-publish-daily-beta.sh --ghcr-only # Only push to GHCR
  11. # ./docker-publish-daily-beta.sh --dockerhub-only # Only push to Docker Hub
  12. # ./docker-publish-daily-beta.sh --skip-release # Build+push without GitHub release
  13. #
  14. # Reads APP_VERSION from backend/app/core/config.py (must be a beta version like 0.2.2b1).
  15. # Builds and pushes a multi-arch Docker image tagged as 'daily'. Each push overwrites the
  16. # previous 'daily' image. A GitHub prerelease is created with a date-stamped tag for history.
  17. #
  18. # Users can stay up to date by pulling the 'daily' tag or using Watchtower:
  19. # docker pull ghcr.io/maziggy/bambuddy:daily
  20. #
  21. # Prerequisites:
  22. # 1. Log in to ghcr.io:
  23. # echo $GITHUB_TOKEN | docker login ghcr.io -u YOUR_USERNAME --password-stdin
  24. #
  25. # 2. Log in to Docker Hub:
  26. # docker login -u YOUR_USERNAME
  27. #
  28. # 3. GitHub CLI (gh) authenticated for creating releases
  29. #
  30. # Supported architectures:
  31. # - linux/amd64 (x86_64, most servers/desktops)
  32. # - linux/arm64 (Raspberry Pi 4/5, Apple Silicon via emulation)
  33. set -e
  34. # Configuration
  35. GHCR_REGISTRY="ghcr.io"
  36. DOCKERHUB_REGISTRY="docker.io"
  37. IMAGE_NAME="maziggy/bambuddy"
  38. GHCR_IMAGE="${GHCR_REGISTRY}/${IMAGE_NAME}"
  39. DOCKERHUB_IMAGE="${DOCKERHUB_REGISTRY}/${IMAGE_NAME}"
  40. PLATFORMS="linux/amd64,linux/arm64"
  41. BUILDER_NAME="bambuddy-builder"
  42. CONFIG_FILE="backend/app/core/config.py"
  43. CHANGELOG_FILE="CHANGELOG.md"
  44. # Colors for output
  45. RED='\033[0;31m'
  46. GREEN='\033[0;32m'
  47. YELLOW='\033[1;33m'
  48. BLUE='\033[0;34m'
  49. NC='\033[0m' # No Color
  50. # Parse arguments
  51. PARALLEL=false
  52. PUSH_GHCR=true
  53. PUSH_DOCKERHUB=true
  54. SKIP_RELEASE=false
  55. for arg in "$@"; do
  56. case $arg in
  57. --parallel)
  58. PARALLEL=true
  59. ;;
  60. --ghcr-only)
  61. PUSH_DOCKERHUB=false
  62. ;;
  63. --dockerhub-only)
  64. PUSH_GHCR=false
  65. ;;
  66. --skip-release)
  67. SKIP_RELEASE=true
  68. ;;
  69. --help|-h)
  70. echo "Usage: $0 [--parallel] [--ghcr-only] [--dockerhub-only] [--skip-release]"
  71. echo ""
  72. echo "Build and publish a daily beta Docker image using the APP_VERSION from config.py."
  73. echo ""
  74. echo "Options:"
  75. echo " --parallel Build both architectures simultaneously"
  76. echo " --ghcr-only Only push to GitHub Container Registry"
  77. echo " --dockerhub-only Only push to Docker Hub"
  78. echo " --skip-release Build+push without creating/updating GitHub release"
  79. echo " --help, -h Show this help"
  80. exit 0
  81. ;;
  82. *)
  83. echo -e "${RED}Unknown argument: $arg${NC}"
  84. echo "Run $0 --help for usage"
  85. exit 1
  86. ;;
  87. esac
  88. done
  89. # ============================================================
  90. # Step 1: Read and validate APP_VERSION
  91. # ============================================================
  92. echo -e "${BLUE}[1/4] Validating APP_VERSION...${NC}"
  93. VERSION=$(grep -oP 'APP_VERSION = "\K[^"]+' "$CONFIG_FILE")
  94. if [ -z "$VERSION" ]; then
  95. echo -e "${RED}Error: Could not read APP_VERSION from ${CONFIG_FILE}${NC}"
  96. exit 1
  97. fi
  98. if ! [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+b[0-9]+$ ]]; then
  99. echo -e "${RED}Error: APP_VERSION '${VERSION}' is not a beta version (expected X.Y.Zb<N>)${NC}"
  100. exit 1
  101. fi
  102. # Date-stamped tag for GitHub releases only (not used as Docker tag)
  103. DAILY_DATE=$(date +%Y%m%d)
  104. DAILY_TAG="${VERSION}-daily.${DAILY_DATE}"
  105. echo -e "${GREEN} APP_VERSION: ${VERSION}${NC}"
  106. echo -e "${GREEN} Docker tag: daily${NC}"
  107. echo -e "${GREEN} Release tag: v${DAILY_TAG}${NC}"
  108. # ============================================================
  109. # Step 2: Build & push Docker images
  110. # ============================================================
  111. echo ""
  112. # Get CPU count
  113. CPU_COUNT=$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4)
  114. echo -e "${GREEN}================================================${NC}"
  115. echo -e "${GREEN} Daily beta build${NC}"
  116. echo -e "${GREEN} Version: ${VERSION}${NC}"
  117. echo -e "${GREEN} Docker tag: daily${NC}"
  118. echo -e "${GREEN} Platforms: ${PLATFORMS}${NC}"
  119. echo -e "${GREEN} CPU cores: ${CPU_COUNT}${NC}"
  120. if [ "$PARALLEL" = true ]; then
  121. echo -e "${GREEN} Mode: PARALLEL (both archs simultaneously)${NC}"
  122. else
  123. echo -e "${GREEN} Mode: Sequential (amd64 → arm64)${NC}"
  124. fi
  125. echo -e "${GREEN} Registries:${NC}"
  126. if [ "$PUSH_GHCR" = true ]; then
  127. echo -e "${GREEN} - ${GHCR_IMAGE}${NC}"
  128. fi
  129. if [ "$PUSH_DOCKERHUB" = true ]; then
  130. echo -e "${GREEN} - ${DOCKERHUB_IMAGE}${NC}"
  131. fi
  132. echo -e "${GREEN}================================================${NC}"
  133. echo ""
  134. # Check registry logins
  135. if [ "$PUSH_GHCR" = true ]; then
  136. if ! grep -q "ghcr.io" ~/.docker/config.json 2>/dev/null; then
  137. echo -e "${YELLOW}Warning: You may not be logged in to ghcr.io${NC}"
  138. echo "Run: echo \$GITHUB_TOKEN | docker login ghcr.io -u YOUR_USERNAME --password-stdin"
  139. echo ""
  140. fi
  141. fi
  142. if [ "$PUSH_DOCKERHUB" = true ]; then
  143. if ! grep -q "index.docker.io\|docker.io" ~/.docker/config.json 2>/dev/null; then
  144. echo -e "${RED}Error: You are not logged in to Docker Hub${NC}"
  145. echo "Run: docker login -u YOUR_USERNAME"
  146. echo ""
  147. exit 1
  148. fi
  149. fi
  150. # Setup buildx builder if not exists
  151. echo -e "${BLUE}[2/4] Setting up Docker Buildx and building...${NC}"
  152. if ! docker buildx inspect "$BUILDER_NAME" >/dev/null 2>&1; then
  153. echo "Creating new buildx builder: $BUILDER_NAME (optimized for ${CPU_COUNT} cores)"
  154. docker buildx create \
  155. --name "$BUILDER_NAME" \
  156. --driver docker-container \
  157. --driver-opt network=host \
  158. --driver-opt "env.BUILDKIT_STEP_LOG_MAX_SIZE=10000000" \
  159. --buildkitd-flags "--allow-insecure-entitlement network.host --oci-worker-gc=false" \
  160. --config /dev/stdin <<EOF
  161. [worker.oci]
  162. max-parallelism = ${CPU_COUNT}
  163. EOF
  164. docker buildx inspect --bootstrap "$BUILDER_NAME"
  165. fi
  166. docker buildx use "$BUILDER_NAME"
  167. # Verify builder supports multi-platform
  168. if ! docker buildx inspect --bootstrap | grep -q "linux/arm64"; then
  169. echo -e "${YELLOW}Installing QEMU for cross-platform builds...${NC}"
  170. docker run --privileged --rm tonistiigi/binfmt --install all
  171. fi
  172. # Beta versions get 'daily' tag (never 'latest')
  173. echo -e "${YELLOW}Beta version — tagging as 'daily' (not 'latest')${NC}"
  174. # Build tags for all target registries
  175. TAGS=""
  176. if [ "$PUSH_GHCR" = true ]; then
  177. TAGS="$TAGS -t ${GHCR_IMAGE}:daily"
  178. fi
  179. if [ "$PUSH_DOCKERHUB" = true ]; then
  180. TAGS="$TAGS -t ${DOCKERHUB_IMAGE}:daily"
  181. fi
  182. # Common build args (no cache to ensure clean builds)
  183. BUILD_ARGS="--provenance=false --sbom=false --no-cache --pull"
  184. if [ "$PARALLEL" = true ]; then
  185. # Parallel build: Build each architecture separately then combine manifests
  186. echo -e "${YELLOW}Building amd64 and arm64 in parallel (${CPU_COUNT} cores each, no cache)...${NC}"
  187. # Build per-arch staging tags for each target registry
  188. ARCH_TAGS_AMD64=""
  189. ARCH_TAGS_ARM64=""
  190. if [ "$PUSH_GHCR" = true ]; then
  191. ARCH_TAGS_AMD64="$ARCH_TAGS_AMD64 -t ${GHCR_IMAGE}:daily-amd64"
  192. ARCH_TAGS_ARM64="$ARCH_TAGS_ARM64 -t ${GHCR_IMAGE}:daily-arm64"
  193. fi
  194. if [ "$PUSH_DOCKERHUB" = true ]; then
  195. ARCH_TAGS_AMD64="$ARCH_TAGS_AMD64 -t ${DOCKERHUB_IMAGE}:daily-amd64"
  196. ARCH_TAGS_ARM64="$ARCH_TAGS_ARM64 -t ${DOCKERHUB_IMAGE}:daily-arm64"
  197. fi
  198. # Build amd64 in background
  199. (
  200. echo -e "${BLUE}[amd64] Starting build...${NC}"
  201. docker buildx build \
  202. --platform linux/amd64 \
  203. ${ARCH_TAGS_AMD64} \
  204. ${BUILD_ARGS} \
  205. --push \
  206. . 2>&1 | sed 's/^/[amd64] /'
  207. echo -e "${GREEN}[amd64] Complete!${NC}"
  208. ) &
  209. PID_AMD64=$!
  210. # Build arm64 in background
  211. (
  212. echo -e "${BLUE}[arm64] Starting build...${NC}"
  213. docker buildx build \
  214. --platform linux/arm64 \
  215. ${ARCH_TAGS_ARM64} \
  216. ${BUILD_ARGS} \
  217. --push \
  218. . 2>&1 | sed 's/^/[arm64] /'
  219. echo -e "${GREEN}[arm64] Complete!${NC}"
  220. ) &
  221. PID_ARM64=$!
  222. # Wait for both builds
  223. echo "Waiting for parallel builds to complete..."
  224. wait $PID_AMD64
  225. wait $PID_ARM64
  226. # Create multi-arch manifests per registry (no cross-registry blob copies)
  227. echo -e "${BLUE}Creating multi-arch manifests...${NC}"
  228. if [ "$PUSH_GHCR" = true ]; then
  229. echo -e "${BLUE} Creating GHCR manifest...${NC}"
  230. docker buildx imagetools create \
  231. -t "${GHCR_IMAGE}:daily" \
  232. "${GHCR_IMAGE}:daily-amd64" \
  233. "${GHCR_IMAGE}:daily-arm64"
  234. fi
  235. if [ "$PUSH_DOCKERHUB" = true ]; then
  236. echo -e "${BLUE} Creating Docker Hub manifest...${NC}"
  237. docker buildx imagetools create \
  238. -t "${DOCKERHUB_IMAGE}:daily" \
  239. "${DOCKERHUB_IMAGE}:daily-amd64" \
  240. "${DOCKERHUB_IMAGE}:daily-arm64"
  241. fi
  242. else
  243. # Sequential build (default): Build both platforms in one command
  244. echo -e "${YELLOW}Building sequentially with ${CPU_COUNT} cores (no cache)...${NC}"
  245. DOCKER_BUILDKIT=1 docker buildx build \
  246. --platform "$PLATFORMS" \
  247. ${BUILD_ARGS} \
  248. $TAGS \
  249. --push \
  250. .
  251. fi
  252. # ============================================================
  253. # Step 3: Create/update GitHub release
  254. # ============================================================
  255. if [ "$SKIP_RELEASE" = true ]; then
  256. echo -e "${YELLOW}[3/4] Skipping GitHub release (--skip-release)${NC}"
  257. else
  258. echo -e "${BLUE}[3/4] Creating/updating GitHub release...${NC}"
  259. # Extract release notes from CHANGELOG: content between ## [<version>] and the next ## [ heading
  260. CHANGELOG_NOTES=$(sed -n "/^## \[${VERSION}\]/,/^## \[/{/^## \[/!p}" "$CHANGELOG_FILE" | sed '/^$/d; 1{/^$/d}')
  261. # Strip @mentions so GitHub doesn't auto-generate a "Contributors" section
  262. CHANGELOG_NOTES=$(echo "$CHANGELOG_NOTES" | sed 's/@\([a-zA-Z0-9_-]*\)/\1/g')
  263. if [ -z "$CHANGELOG_NOTES" ]; then
  264. echo -e "${YELLOW} Warning: No changelog notes found for ${VERSION}${NC}"
  265. CHANGELOG_NOTES="No changelog notes available for this release."
  266. fi
  267. # Build pull commands for the release body
  268. PULL_COMMANDS=""
  269. if [ "$PUSH_GHCR" = true ]; then
  270. PULL_COMMANDS="docker pull ghcr.io/maziggy/bambuddy:daily"
  271. fi
  272. if [ "$PUSH_DOCKERHUB" = true ]; then
  273. if [ -n "$PULL_COMMANDS" ]; then
  274. PULL_COMMANDS="${PULL_COMMANDS}
  275. # or
  276. docker pull maziggy/bambuddy:daily"
  277. else
  278. PULL_COMMANDS="docker pull maziggy/bambuddy:daily"
  279. fi
  280. fi
  281. # Create the release body
  282. TODAY=$(date +%Y-%m-%d)
  283. RELEASE_BODY=$(cat <<EOF
  284. > [!NOTE]
  285. > This is a **daily beta build** (${TODAY}). It contains the latest fixes and improvements but may have undiscovered issues.
  286. >
  287. > **Docker users:** Update by pulling the new image:
  288. > \`\`\`
  289. > ${PULL_COMMANDS}
  290. > \`\`\`
  291. >
  292. > **Tip:** Use [Watchtower](https://containrrr.dev/watchtower/) to automatically update when new daily builds are pushed.
  293. ---
  294. ${CHANGELOG_NOTES}
  295. EOF
  296. )
  297. # Delete ALL old daily releases — only the latest daily build should exist
  298. echo " Cleaning up old daily releases..."
  299. OLD_DAILY_RELEASES=$(gh release list --limit 100 --json tagName --jq '.[] | select(.tagName | test("-daily\\.")) | .tagName' 2>/dev/null || true)
  300. if [ -n "$OLD_DAILY_RELEASES" ]; then
  301. while IFS= read -r old_tag; do
  302. echo " Deleting old daily release: ${old_tag}..."
  303. gh release delete "$old_tag" --yes --cleanup-tag 2>/dev/null || true
  304. done <<< "$OLD_DAILY_RELEASES"
  305. fi
  306. # Create/move tag to current HEAD and push
  307. echo " Tagging current HEAD as v${DAILY_TAG}..."
  308. git tag -f "v${DAILY_TAG}"
  309. git push origin "v${DAILY_TAG}" --force
  310. echo " Creating release v${DAILY_TAG}..."
  311. gh release create "v${DAILY_TAG}" \
  312. --title "Daily Beta Build v${DAILY_TAG}" \
  313. --prerelease \
  314. --generate-notes=false \
  315. --notes "$RELEASE_BODY"
  316. echo -e "${GREEN} Created GitHub release: v${DAILY_TAG}${NC}"
  317. fi
  318. # ============================================================
  319. # Step 4: Verify
  320. # ============================================================
  321. echo -e "${BLUE}[4/4] Verifying...${NC}"
  322. if [ "$PUSH_GHCR" = true ]; then
  323. echo -e "${BLUE}GHCR manifest:${NC}"
  324. docker buildx imagetools inspect "${GHCR_IMAGE}:daily"
  325. fi
  326. if [ "$PUSH_DOCKERHUB" = true ]; then
  327. echo -e "${BLUE}Docker Hub manifest:${NC}"
  328. docker buildx imagetools inspect "${DOCKERHUB_IMAGE}:daily"
  329. fi
  330. if [ "$SKIP_RELEASE" != true ]; then
  331. echo ""
  332. echo -e "${BLUE}GitHub release:${NC}"
  333. gh release view "v${DAILY_TAG}"
  334. fi
  335. # ============================================================
  336. # Summary
  337. # ============================================================
  338. echo ""
  339. echo -e "${GREEN}================================================${NC}"
  340. echo -e "${GREEN} Daily beta build complete!${NC}"
  341. echo -e "${GREEN} Version: ${VERSION}${NC}"
  342. echo -e "${GREEN}================================================${NC}"
  343. if [ "$PUSH_GHCR" = true ]; then
  344. echo " GHCR: ${GHCR_IMAGE}:daily"
  345. fi
  346. if [ "$PUSH_DOCKERHUB" = true ]; then
  347. echo " Docker Hub: ${DOCKERHUB_IMAGE}:daily"
  348. fi
  349. if [ "$SKIP_RELEASE" != true ]; then
  350. echo " Release: https://github.com/${IMAGE_NAME}/releases/tag/v${DAILY_TAG}"
  351. fi
  352. echo ""
  353. echo -e "${BLUE}Supported platforms:${NC}"
  354. echo " - linux/amd64 (Intel/AMD servers, desktops)"
  355. echo " - linux/arm64 (Raspberry Pi 4/5, Apple Silicon)"
  356. echo ""
  357. echo -e "${GREEN}Users can now run:${NC}"
  358. if [ "$PUSH_GHCR" = true ]; then
  359. echo " docker pull ${GHCR_IMAGE}:daily"
  360. fi
  361. if [ "$PUSH_DOCKERHUB" = true ]; then
  362. echo " docker pull ${DOCKERHUB_IMAGE}:daily"
  363. echo " docker pull ${IMAGE_NAME}:daily # shorthand"
  364. fi