最後活躍 1737707914

修訂 a85087bbbef052b61419c4aab013088e0b03aee5

build.sh 原始檔案
1#!/bin/bash
2
3# daily_build_v2.sh (refactored without tmux)
4# Script Purpose:
5# 1) For each branch in the list:
6# a) Switch to the branch, ensure local code is up-to-date (clean -fdx, reset --hard, pull)
7# b) Check if there are new commits in the past 24 hours
8# - If yes, a build is required
9# - If no but the last build was more than a week ago, a forced build is required
10# - Otherwise, skip
11# c) If a build is required, run build_all_langs.sh in the current session,
12# then copy build artifacts from ./src/dists/* to ~/built/<branch>/
13#
14# Additional requirements:
15# - Do NOT build branches in parallel. Must finish one before moving to the next.
16# - If no build is needed, do NOT remove any existing build artifacts.
17# - Provide more logs rather than comments.
18
19REPO_DIR="$HOME/anduinos"
20SRC_DIR="$REPO_DIR/src"
21BUILD_SCRIPT="$SRC_DIR/build_all_langs.sh"
22BASE_BUILT_DIR="$HOME/built"
23
24# Branch list (example: 1.1 & 1.2)
25BRANCHES=("1.1" "1.2")
26
27# Print an error message and exit
28error_exit() {
29 echo "[$(date)] ERROR: $1" >&2
30 exit 1
31}
32
33# Function to check and build a specific branch
34check_and_build_branch() {
35 local branch_name=$1
36
37 echo "[$(date)] INFO: Start checking branch: $branch_name"
38
39 local last_build_file="$HOME/.last_build_date_${branch_name}"
40
41 # Switch to the repository directory
42 echo "[$(date)] INFO: Changing directory to $REPO_DIR"
43 cd "$REPO_DIR" || error_exit "Cannot change directory to $REPO_DIR"
44
45 echo "[$(date)] INFO: Reset and fetch latest code from remote"
46 git reset --hard HEAD
47 git fetch
48
49 echo "[$(date)] INFO: Switching to branch $branch_name"
50 git switch "$branch_name" || error_exit "Failed to switch to branch $branch_name"
51
52 echo "[$(date)] INFO: Pulling latest code"
53 git pull || error_exit "Failed to pull latest code on branch $branch_name"
54
55 # Check if new commit exists in the past 24 hours
56 local BUILD_REQUIRED=false
57 if git log -1 --since="24 hours ago" "$branch_name" > /dev/null 2>&1; then
58 BUILD_REQUIRED=true
59 echo "[$(date)] INFO: Found new commits in the past 24 hours for branch $branch_name"
60 else
61 # Check last build time
62 if [ -f "$last_build_file" ]; then
63 local last_build_date
64 last_build_date=$(cat "$last_build_file")
65
66 local last_build_epoch
67 last_build_epoch=$(date -d "$last_build_date" +%s)
68 local current_epoch
69 current_epoch=$(date +%s)
70 local seven_days_ago=$(( current_epoch - 7*86400 ))
71
72 if [ "$last_build_epoch" -lt "$seven_days_ago" ]; then
73 BUILD_REQUIRED=true
74 echo "[$(date)] INFO: No recent commits, but last build on $last_build_date is older than 7 days; forcing a build."
75 else
76 echo "[$(date)] INFO: No new commits for branch $branch_name, and last build is within a week. Skipping build."
77 fi
78 else
79 BUILD_REQUIRED=true
80 echo "[$(date)] INFO: No last_build_date record found. Forcing a build."
81 fi
82 fi
83
84 # If build is required, proceed
85 if [ "$BUILD_REQUIRED" = true ]; then
86 echo "[$(date)] INFO: Build is required for branch $branch_name"
87
88 # Optional: Clean up the old artifacts only when we are about to build
89 echo "[$(date)] INFO: Removing old artifacts under $BASE_BUILT_DIR/$branch_name"
90 rm -rf "$BASE_BUILT_DIR/$branch_name"
91 mkdir -p "$BASE_BUILT_DIR/$branch_name"
92
93 echo "[$(date)] INFO: Running build script for branch $branch_name"
94 (cd "$SRC_DIR" && bash "$BUILD_SCRIPT")
95 if [ $? -eq 0 ]; then
96 echo "[$(date)] INFO: Build script completed successfully"
97
98 # Copy artifacts
99 echo "[$(date)] INFO: Copying build artifacts from $SRC_DIR/dists to $BASE_BUILT_DIR/$branch_name"
100 cp -rf "$SRC_DIR/dists/"* "$BASE_BUILT_DIR/$branch_name"/
101
102 # Update last build date
103 date +"%Y-%m-%d" > "$last_build_file"
104 echo "[$(date)] INFO: Updated last build date record for branch $branch_name"
105 else
106 error_exit "Build script failed for branch $branch_name"
107 fi
108 else
109 echo "[$(date)] INFO: No build needed for branch $branch_name. Keeping existing artifacts."
110 fi
111}
112
113# MAIN FLOW
114# Process each branch in sequence to avoid parallel builds.
115for branch in "${BRANCHES[@]}"; do
116 check_and_build_branch "$branch"
117done
118
119echo "[$(date)] INFO: All branches processed. Script completed."
120