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