#!/bin/bash -ile
domain_name=xxx.com
app_dir=/data
if [ "$status" == "deploy" ]; then
md5_new=`cat composer.lock | md5sum | awk '{print $1}'`
md5_old=`cat composer_md5`
if [ "$md5_new" == "$md5_old" ];then
echo "same md5sum"
else
echo $md5_new > composer_md5
composer install
fi
for i in $(echo $hostlist | sed 's/,/ /g'); do
x=$(echo $i | awk -F\: '{print $2}')
echo $x
ssh work@$x "mkdir -p $app_dir/$domain_name/releases/$GIT_BRANCH"
rsync -az ../$JOB_NAME/ --exclude .git --exclude .gitignore work@$x:$app_dir/$domain_name/releases/$GIT_BRANCH
#ssh work@$x "rm -rf ${app_dir}/${domain_name}/current"
#ssh work@$x "ln -s ${app_dir}/${domain_name}/releases/${GIT_BRANCH} ${app_dir}/${domain_name}/current"
ssh work@$x "ln -snf $app_dir/$domain_name/releases/$GIT_BRANCH $app_dir/$domain_name/current_tmp && mv -fT $app_dir/$domain_name/current_tmp $app_dir/$domain_name/current"
sleep 10
#ssh work@$x "cd ${app_dir}/${domain_name}/releases && find . -maxdepth 1 -ctime +10 |sort -rn|xargs rm -rf"
done
elif [ "$status" == "rollback" ]; then
for i in $(echo $hostlist | sed 's/,/ /g'); do
x=$(echo $i | awk -F\: '{print $2}')
#x=`echo $i | awk -F\: '{print $2}'`
echo $x
num=$(ssh work@$x "ls $app_dir/$domain_name/releases| grep "$GIT_BRANCH"|wc -l")
if [ $num == "1" ]; then
ssh work@$x "rm -rf $app_dir/web/$domain_name/current ; ln -snf $app_dir/web/$domain_name/releases/$GIT_BRANCH $app_dir/web/$domain_name/current"
#ssh work@$x "rm -rf ${app_dir}/web/${domain_name}/current"
#ssh work@$x "ln -s ${app_dir}/web/${domain_name}/releases/${GIT_BRANCH} ${app_dir}/web/${domain_name}/current"
#sleep 1
else
echo "no $GIT_BRANCH on server!"
exit 1
fi
done
#重启php
elif [ "$status" == "reload_php" ]; then
for i in $(echo $hostlist | sed 's/,/ /g'); do
x=$(echo $i | awk -F\: '{print $2}')
echo $x
ssh work@$x "sudo systemctl reload php7-fpm"
done
#重启NGINX
elif [ "$status" == "reload_nginx" ]; then
for i in $(echo $hostlist | sed 's/,/ /g'); do
x=$(echo $i | awk -F\: '{print $2}')
echo $x
ssh work@$x "sudo systemctl reload nginx"
done
fi
在对应主机的release目录创建以分支或tag命名的文件夹。
rsync同步安卓composer组件后的代码,忽略掉 .git 和.gitignore 文件夹。
删除current软链接,创建current软链接(可能会中途失败,删除创建瞬间,http 404)。
创建current_tmp软链接,覆盖更名current_tmp为current(ln -snf防止要创建的软链接名称已存在,mv -fT软链接重命名)
ln -snf
current_tmp本身是一个软链接,要改变其指向的位置,用ln -s ln-sn ln -sf都无法改变,-snf -sTf可以更改。
ln -s必须使用绝对路径
mv -fT
mv软链接是移动,而不是重命名,需加T
-n, --no-dereference treat destination that is a symlink to a directory as if it were a normal file
-T, --no-target-directory treat LINK_NAME as a normal file
-n, –no-dereference 如果目的地是一个已存在的链接至某目录的符号链接,会将该符号链接当作普通文件处理
-T, –no-target-directory 将链接名当作普通文件
意思相近,谁知道有什么区别呢
Linux 连续执行多条命令的方法
https://blog.youkuaiyun.com/freedom2028/article/details/7104131
nginx php-fpm配置
配置fastcig_param SCRIPT_FILENAME为realpath_root,DOCUMENT_ROOT也改为realpath_root(这些参数可在$_SERVER中查看)
$realpath_root为nginx的内置变量
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;