创建install.sh全自动安装部署脚本
This commit is contained in:
parent
464daa1195
commit
93266a0259
10
README.md
10
README.md
@ -61,10 +61,14 @@ php start.php start -d
|
||||
|
||||
> 需要开放程序所在服务器的8787端口,或者做一个反向代理到8787端口的http站点。
|
||||
|
||||
### 避免因自动打包进程意外终止而导致永远无法自动打包
|
||||
#### 定义WebHook
|
||||
|
||||
可以通过执行`auto_install.sh`脚本创建一个系统定时任务,用来定时清理由`auto_build.sh`脚本错误执行而产生的“僵尸锁”。
|
||||
要实现自动拉新部署,还需要在GIT服务器上对仓库创建推送事件的WebHook的钩子URL,就是GIT服务器可以访问到的deploy服务url地址,比如`http://192.168.0.8:8787`。
|
||||
|
||||
#### 全自动安装脚本
|
||||
|
||||
可以通过执行`install.sh`脚本实现编译文档项目、启动钩子服务、实现自动构建、定期清理僵尸锁等一系统动作的全自动执行。
|
||||
|
||||
```sh
|
||||
sh auto_install.sh
|
||||
sh install.sh
|
||||
```
|
||||
|
@ -1,37 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
lock_file="auto_install.lock"
|
||||
cleaner_script="auto_cleaner.sh"
|
||||
cron_command="sh $(pwd)/$cleaner_script $(pwd)/"
|
||||
|
||||
# 检查是否已安装
|
||||
if [ -f "$lock_file" ]; then
|
||||
echo "auto_cleaner.sh任务已安装,如需重新安装,请先删除$lock_file文件"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 获取当前目录的绝对路径
|
||||
current_dir=$(pwd)/
|
||||
|
||||
# 检查auto_cleaner.sh文件是否存在
|
||||
if [ ! -f "$current_dir$cleaner_script" ]; then
|
||||
echo "未找到$current_dir$cleaner_script文件,退出本次安装"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 检查是否已存在定时任务
|
||||
existing_cron=$(crontab -l | grep -F "$cron_command")
|
||||
if [ -n "$existing_cron" ]; then
|
||||
echo "auto_cleaner.sh任务已安装,退出本次安装"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 创建安装标记文件
|
||||
timestamp=$(date +"%Y-%m-%d %H:%M:%S")
|
||||
echo "$timestamp" > "$lock_file"
|
||||
|
||||
# 创建定时任务
|
||||
(crontab -l 2>/dev/null; echo "*/10 * * * * $cron_command") | crontab -
|
||||
echo "auto_cleaner.sh任务安装完毕"
|
||||
|
||||
exit 0
|
103
install.sh
Normal file
103
install.sh
Normal file
@ -0,0 +1,103 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 获取当前脚本所在目录路径
|
||||
work_dir=$(dirname $(readlink -f "$0"))
|
||||
|
||||
# 字义install.log文件路径
|
||||
log_file="$work_dir/install.log"
|
||||
|
||||
# 定义deploy目录路径
|
||||
deploy_dir="$work_dir/deploy"
|
||||
|
||||
# 定时清理脚本
|
||||
cleaner_script="auto_cleaner.sh"
|
||||
|
||||
# 编译脚本
|
||||
build_script="auto_build.sh"
|
||||
|
||||
# 定时任务命令
|
||||
cron_command="sh $work_dir/$cleaner_script $work_dir/"
|
||||
|
||||
# 检查install.lock文件是否存在
|
||||
if [ -f "$work_dir/install.lock" ]; then
|
||||
install_time=$(cat "$work_dir/install.lock")
|
||||
echo "项目已在$install_time完成安装,如需重新安装,可以手动删除install.lock文件后再尝试执行安装脚本"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 创建install.lock文件
|
||||
current_time=$(date +"%Y-%m-%d %H:%M:%S")
|
||||
echo "$current_time" >"$work_dir/install.lock"
|
||||
|
||||
# 检查.env文件是否存在
|
||||
env_file="$deploy_dir/.env"
|
||||
if [ -f "$env_file" ]; then
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] .env文件已存在,跳过创建" >>"$log_file"
|
||||
else
|
||||
# 创建.env文件
|
||||
branch=$(git branch --show-current)
|
||||
git_url=$(git remote -v | grep fetch | awk '{print $2}')
|
||||
echo "WORD_DIR = $work_dir" >"$env_file"
|
||||
echo "BRANCH = $branch" >>"$env_file"
|
||||
echo "GIT_URL = $git_url" >>"$env_file"
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] .env文件创建成功" >>"$log_file"
|
||||
fi
|
||||
|
||||
# 进入deploy目录
|
||||
cd "$deploy_dir"
|
||||
|
||||
start_script="start.php"
|
||||
# 先尝试停止一次应用
|
||||
php "$start_script" stop
|
||||
stop_result=$?
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] 先尝试执行php $start_script stop,结果:$stop_result" | tee -a "$log_file"
|
||||
|
||||
# 执行composer install
|
||||
yes | composer install
|
||||
install_result=$?
|
||||
if [ $install_result -ne 0 ]; then
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] PHP环境可能异常,自动拉新功能很可能不会生效" | tee -a "$log_file"
|
||||
fi
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] 执行composer install,结果:$install_result" | tee -a "$log_file"
|
||||
|
||||
# 启动应用
|
||||
if [ -f "$start_script" ]; then
|
||||
php "$start_script" start -d
|
||||
start_result=$?
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] 执行php $start_script start -d,结果:$start_result" | tee -a "$log_file"
|
||||
if [ $start_result -ne 0 ]; then
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] PHP环境可能异常,自动拉新功能不会生效" | tee -a "$log_file"
|
||||
else
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] deploy服务启动成功" | tee -a "$log_file"
|
||||
fi
|
||||
else
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] start.php文件不存在,代码不完整,自动拉新功能不会生效" | tee -a "$log_file"
|
||||
fi
|
||||
|
||||
# 退出deploy目录
|
||||
cd ..
|
||||
|
||||
# 检查auto_cleaner.sh文件和定时任务
|
||||
auto_cleaner_script="$work_dir/auto_cleaner.sh"
|
||||
if [ ! -f "$auto_cleaner_script" ]; then
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] 未找到$auto_cleaner_script文件,跳过" | tee -a "$log_file"
|
||||
else
|
||||
existing_cron=$(crontab -l | grep -F "$cron_command")
|
||||
if [ -n "$existing_cron" ]; then
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] auto_cleaner.sh任务已存在,跳过" | tee -a "$log_file"
|
||||
else
|
||||
(
|
||||
crontab -l 2>/dev/null
|
||||
echo "*/10 * * * * $cron_command"
|
||||
) | crontab -
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] auto_cleaner.sh任务安装成功" | tee -a "$log_file"
|
||||
fi
|
||||
fi
|
||||
|
||||
# 执行编译脚本
|
||||
sh $build_script
|
||||
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] 安装完毕,在$log_file中可以查看完整的安装日志" | tee -a "$log_file"
|
||||
|
||||
# 退出
|
||||
exit 0
|
Loading…
Reference in New Issue
Block a user