Dernière activité 1753519534

Révision 2dad0ca6856e469664360f15cfc7ca137e03f252

build.sh Brut
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="/mnt/second/anduinos"
20SRC_DIR="${REPO_DIR}/src"
21BUILD_SCRIPT="${REPO_DIR}/build_all.sh"
22BASE_BUILT_DIR="/mnt/second/built"
23BUILD_MIRROR="https://ftp.uni-stuttgart.de/ubuntu/"
24
25# Branch list (example: 1.1 & 1.3)
26BRANCHES=("1.1" "1.3")
27
28# Print an error message and exit
29error_exit() {
30 echo "[$(date)] ERROR: $1" >&2
31 # Infinite sleep to keep the container running for debugging
32 sleep 99999999
33}
34
35# Function to check and build a specific branch
36check_and_build_branch() {
37 local branch_name="$1"
38 echo "[$(date)] INFO: Start checking branch: $branch_name"
39
40 local last_build_file="$HOME/.last_build_date_${branch_name}"
41
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 with timeout and retries"
53 local RETRY_COUNT=0
54 local MAX_RETRIES=10
55 local PULL_SUCCESS=false
56
57 while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
58 echo "[$(date)] INFO: Pull attempt $((RETRY_COUNT + 1)) of $MAX_RETRIES"
59 if timeout 300s git pull; then
60 PULL_SUCCESS=true
61 echo "[$(date)] INFO: git pull succeeded"
62 break
63 else
64 echo "[$(date)] WARN: git pull attempt $((RETRY_COUNT + 1)) failed, retrying in 5s..."
65 RETRY_COUNT=$((RETRY_COUNT + 1))
66 sleep 5
67 fi
68 done
69
70 if [ "$PULL_SUCCESS" != true ]; then
71 error_exit "Failed to pull latest code on branch $branch_name after $MAX_RETRIES attempts"
72 fi
73
74 # Check if new commit exists in the past 24 hours
75 local BUILD_REQUIRED=false
76 if git log -1 --since="24 hours ago" "$branch_name" > /dev/null 2>&1; then
77 BUILD_REQUIRED=true
78 echo "[$(date)] INFO: Found new commits in the past 24 hours for branch $branch_name"
79 else
80 if [ -f "$last_build_file" ]; then
81 local last_build_date
82 last_build_date=$(cat "$last_build_file")
83 local last_build_epoch
84 last_build_epoch=$(date -d "$last_build_date" +%s)
85 local current_epoch
86 current_epoch=$(date +%s)
87 local seven_days_ago=$(( current_epoch - 7*86400 ))
88 if [ "$last_build_epoch" -lt "$seven_days_ago" ]; then
89 BUILD_REQUIRED=true
90 echo "[$(date)] INFO: No recent commits, but last build on $last_build_date is older than 7 days; forcing a build."
91 else
92 echo "[$(date)] INFO: No new commits for branch $branch_name, and last build is within a week. Skipping build."
93 fi
94 else
95 BUILD_REQUIRED=true
96 echo "[$(date)] INFO: No last_build_date record found. Forcing a build."
97 fi
98 fi
99
100 if [ "$BUILD_REQUIRED" = true ]; then
101 echo "[$(date)] INFO: Build is required for branch $branch_name"
102 sed -i "s|export BUILD_UBUNTU_MIRROR=.*|export BUILD_UBUNTU_MIRROR=\"$BUILD_MIRROR\"|" "$SRC_DIR/args.sh"
103 echo "[$(date)] INFO: Updated BUILD_UBUNTU_MIRROR in $SRC_DIR/args.sh"
104
105 (cd "$REPO_DIR" && bash "$BUILD_SCRIPT" -c ./config/all.json)
106 if [ $? -eq 0 ]; then
107 echo "[$(date)] INFO: Build script completed successfully"
108 echo "[$(date)] INFO: Removing old artifacts under $BASE_BUILT_DIR/$branch_name"
109 rm -rf "$BASE_BUILT_DIR/$branch_name"
110 mkdir -p "$BASE_BUILT_DIR/$branch_name"
111 echo "[$(date)] INFO: Moving build artifacts from $SRC_DIR/dist to $BASE_BUILT_DIR/$branch_name"
112 mv "$SRC_DIR/dist/"* "$BASE_BUILT_DIR/$branch_name"/
113 date +"%Y-%m-%d" > "$last_build_file"
114 echo "[$(date)] INFO: Updated last build date record for branch $branch_name"
115 else
116 error_exit "Build script failed for branch $branch_name"
117 fi
118 else
119 echo "[$(date)] INFO: No build needed for branch $branch_name. Keeping existing artifacts."
120 fi
121}
122
123# MAIN FLOW
124for branch in "${BRANCHES[@]}"; do
125 check_and_build_branch "$branch"
126done
127
128echo "[$(date)] INFO: All branches processed. Script completed."
129