build.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/env bash
  2. # smartbotic-automation production build orchestrator.
  3. # Modeled on /data/shadowman-cpp/packaging/build.sh.
  4. #
  5. # Usage:
  6. # packaging/build.sh # Build with version from VERSION
  7. # packaging/build.sh 1.2.0 # Override version
  8. # packaging/build.sh --rebuild-base # Force rebuild of base image
  9. # packaging/build.sh --sync --suite trixie # Build + publish to APT repo
  10. set -euo pipefail
  11. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  12. PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
  13. cd "$PROJECT_DIR"
  14. BASE_IMAGE_NAME="smartbotic-automation-build-base:debian13"
  15. OUTPUT_DIR="${PROJECT_DIR}/dist/debian13"
  16. BUILD_JOBS="${BUILD_JOBS:-$(nproc)}"
  17. # The repository checkout moved under /data/dev/<org>/ with the other projects;
  18. # the old top-level path no longer exists, and build.sh failed at the credential
  19. # read rather than at anything to do with building. Both are tried so a machine
  20. # that still has the old layout keeps working.
  21. DEB_REPO_DIR="${DEB_REPO_DIR:-}"
  22. if [ -z "$DEB_REPO_DIR" ]; then
  23. for candidate in /data/dev/smartbotics/smartbotics-deb-repo /data/smartbotics-deb-repo; do
  24. if [ -f "$candidate/.env" ]; then DEB_REPO_DIR="$candidate"; break; fi
  25. done
  26. DEB_REPO_DIR="${DEB_REPO_DIR:-/data/dev/smartbotics/smartbotics-deb-repo}"
  27. fi
  28. RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; BLUE='\033[0;34m'; NC='\033[0m'
  29. log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
  30. log_success() { echo -e "${GREEN}[OK]${NC} $1"; }
  31. log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
  32. log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
  33. REBUILD_BASE=false
  34. NO_CACHE=false
  35. BUILD_REPO=false
  36. SYNC_REPO=false
  37. REPO_SUITE=""
  38. VERSION=""
  39. DEB_REVISION="1"
  40. while [[ $# -gt 0 ]]; do
  41. case $1 in
  42. --rebuild-base) REBUILD_BASE=true; shift ;;
  43. --no-cache) NO_CACHE=true; shift ;;
  44. --deb-revision) DEB_REVISION="$2"; shift 2 ;;
  45. --repo) BUILD_REPO=true; shift ;;
  46. --suite) REPO_SUITE="$2"; shift 2 ;;
  47. --sync) SYNC_REPO=true; BUILD_REPO=true; shift ;;
  48. --help|-h)
  49. cat <<EOF
  50. smartbotic-automation Production Build Script
  51. Usage: $0 [OPTIONS] [VERSION]
  52. Options:
  53. --rebuild-base Force rebuild the base image
  54. --no-cache Build without using BuildKit caches
  55. --deb-revision N Debian revision suffix (default: 1)
  56. --repo Generate APT repo from built packages
  57. --suite NAME Distribution suite for repo (e.g., trixie)
  58. --sync Generate repo and sync to repository.smartbotics.ai
  59. Environment:
  60. BUILD_JOBS Parallel build jobs (default: nproc)
  61. DEB_REPO_DIR Path to smartbotics-deb-repo (default: the first of
  62. /data/dev/smartbotics/smartbotics-deb-repo or
  63. /data/smartbotics-deb-repo that has a .env)
  64. EOF
  65. exit 0 ;;
  66. *) VERSION="$1"; shift ;;
  67. esac
  68. done
  69. if [ -z "$VERSION" ]; then
  70. VERSION="$(tr -d '[:space:]' < VERSION 2>/dev/null || true)"
  71. if [ -z "$VERSION" ]; then
  72. log_error "Could not read version from VERSION file"; exit 1
  73. fi
  74. fi
  75. if ! command -v docker &>/dev/null; then
  76. log_error "Docker not found in PATH"; exit 1
  77. fi
  78. base_image_exists() { docker image inspect "$BASE_IMAGE_NAME" &>/dev/null; }
  79. build_base_image() {
  80. log_info "Building base image: $BASE_IMAGE_NAME"
  81. local repo_pass=""
  82. if [ -f "${DEB_REPO_DIR}/.env" ]; then
  83. repo_pass=$(grep -oP 'RepoPass:\s*\K.*' "${DEB_REPO_DIR}/.env" | tr -d '[:space:]')
  84. fi
  85. if [ -z "$repo_pass" ]; then
  86. log_error "Could not read RepoPass from ${DEB_REPO_DIR}/.env"; exit 1
  87. fi
  88. docker buildx build \
  89. -f packaging/Dockerfile.base \
  90. --build-arg REPO_PASS="$repo_pass" \
  91. -t "$BASE_IMAGE_NAME" \
  92. .
  93. log_success "Base image built"
  94. }
  95. if $REBUILD_BASE; then
  96. build_base_image
  97. elif base_image_exists; then
  98. log_success "Base image cached: $BASE_IMAGE_NAME"
  99. else
  100. build_base_image
  101. fi
  102. GIT_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo unknown)
  103. rm -rf "$OUTPUT_DIR"; mkdir -p "$OUTPUT_DIR"
  104. BUILD_ARGS=(
  105. -f packaging/Dockerfile.build
  106. --build-arg BASE_IMAGE="$BASE_IMAGE_NAME"
  107. --build-arg BUILD_VERSION="$VERSION"
  108. --build-arg BUILD_DEB_REVISION="$DEB_REVISION"
  109. --build-arg BUILD_GIT_COMMIT="$GIT_COMMIT"
  110. --build-arg BUILD_JOBS="$BUILD_JOBS"
  111. --target packages
  112. --output "type=local,dest=$OUTPUT_DIR"
  113. )
  114. $NO_CACHE && BUILD_ARGS+=(--no-cache)
  115. log_info "Building smartbotic-automation ${VERSION}-${DEB_REVISION} (commit ${GIT_COMMIT})"
  116. docker buildx build "${BUILD_ARGS[@]}" .
  117. log_success "Built packages:"
  118. ls -lh "$OUTPUT_DIR"/*.deb
  119. if $BUILD_REPO; then
  120. if [ -z "$REPO_SUITE" ]; then
  121. log_error "--suite is required with --repo"; exit 1
  122. fi
  123. if [ ! -d "$DEB_REPO_DIR" ]; then
  124. log_error "Repository dir not found: $DEB_REPO_DIR"; exit 1
  125. fi
  126. "$DEB_REPO_DIR/scripts/add-packages.sh" "$OUTPUT_DIR"
  127. "$DEB_REPO_DIR/scripts/create-repo.sh" --suite "$REPO_SUITE"
  128. fi
  129. if $SYNC_REPO; then
  130. "$DEB_REPO_DIR/scripts/sync-repo.sh"
  131. fi
  132. log_success "Done."