knowledge/auto_build.sh

76 lines
1.5 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
log_file="auto_build.log"
lock_file="auto_build.lock"
timeout=600 # 超时时间,单位为秒
# 记录日志函数
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [$$] $1" >> "$log_file"
}
# 记录开始执行日志
log "start"
# 检查是否有其他进程正在执行
while [ -f "$lock_file" ]; do
another_pid=$(cat "$lock_file")
log "另一个进程[$another_pid]正在执行,等待中……"
sleep 5
timeout=$((timeout - 1))
if [ "$timeout" -le 0 ]; then
log "等待超时,退出"
exit 1
fi
done
# 创建锁文件
echo $$ > "$lock_file"
# 执行git pull
execute() {
log "$1"
result=$(eval "$1" 2>&1)
log "$result"
}
# 执行git pull
execute "git pull"
# 检查git pull执行结果
if [ $? -eq 0 ]; then
# git pull执行成功执行yarn install
execute "yarn install"
# 检查yarn install执行结果
if [ $? -eq 0 ]; then
# yarn install执行成功执行yarn build
execute "yarn build"
# 检查yarn build执行结果
if [ $? -ne 0 ]; then
# yarn build执行失败异常退出
log "yarn build执行失败"
rm "$lock_file"
exit 1
fi
else
# yarn install执行失败异常退出
log "yarn install执行失败"
rm "$lock_file"
exit 1
fi
else
# git pull执行失败异常退出
log "git pull执行失败"
rm "$lock_file"
exit 1
fi
# 记录完成执行日志
log "done"
# 删除锁文件并正常退出
rm "$lock_file"
exit 0