Skip to content

Instantly share code, notes, and snippets.

@itbdw
Last active August 29, 2015 14:05
Show Gist options
  • Save itbdw/6ef88925b0071d33eec9 to your computer and use it in GitHub Desktop.
Save itbdw/6ef88925b0071d33eec9 to your computer and use it in GitHub Desktop.
nginx_deploy program
#!/bin/bash
# auto_deploy nginx config
#
# zhao.binyan
# 2014-05-09
su xxx -c "sh /home/xxx/bin/deploy_nginx_rewrite.sh"
nginx_status=`nginx -t 2>&1`
if [[ $nginx_status =~ "successful" ]] ; then
echo $nginx_status
echo "reload nginx ......"
nginx -s reload
echo "ok"
else
echo $nginx_status
fi
#!/bin/bash
# to deploy nginx server config auto...ly
# zhao.binyan
# 2014-08-07
#config path
servers_path="/tmp/nginx_test";
servers_path="/home/server/openresty/nginx/conf/servers/";
#config files
config_files=""
config_files="$config_files beta.www.xxx.com"
#config_files="$config_files beta.m.xxx.com"
#config_files="$config_files beta.moirai.xxx.com"
#config_files="$config_files beta.api.xxx.com"
#config ports ,NOTE the aegis is not in charge here
config_ports=""
#config_ports="$config_ports #demo_comment1 #demo_comment2 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019"
config_ports="$config_ports #zhao.binyan 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039"
#config_ports="$config_ports #zhang.li 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049"
#config_ports="$config_ports #zhang.danfeng 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059"
#config_ports="$config_ports #wan.li 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069"
#config_ports="$config_ports #li.shuang 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079"
#config_ports="$config_ports #meng.qinglong 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089"
#tmp file
config_server=""
for x in $config_files
do
for y in $config_ports
do
if [[ $y =~ "#" ]]
then
config_server="$config_server\n$y";
else
config_server="$config_server\nserver {\n\tlisten $y;\n\tinclude custom_conf/$x;\n}";
fi
done;
echo -e "deploy $x";
echo -e $config_server > $servers_path$x".auto";
config_server="";
done;
#!/bin/bash
# dev envirament auto deploy
# php usage
# get the absolute path and exec it
# $filepath = __DIR__ . '/../../../../shell/deploy/git_deploy';
# $filepath = realpath($filepath);
# exec($filepath . " $branch $repo $after 2>&1");
#
# zhao.binyan
#
# 2014-04-28
start_time=`date '+%H:%M:%S'`
git_branch='8010_tieba'
repo='git@git.xxx.com:web/website.git'
after=''
username=''
if [ $1 ] ; then
git_branch=$1
fi
if [ $2 ] ; then
repo=$2
fi
if [ $3 ] ; then
after=$3
fi
if [ $4 ] ; then
username=$4
fi
port=`echo $git_branch | cut -f1 -d"_"`
deploy_name_tmp=`echo $repo | cut -f2 -d":"`
deploy_name=`echo $deploy_name_tmp | cut -f1 -d"/"`
deploy_name="$deploy_name-`echo $deploy_name_tmp | cut -f2 -d"/" | cut -f1 -d"."`"
echo $username
echo $git_branch
#echo $repo
#echo $after
#echo $port
echo $deploy_name
if [[ $git_branch =~ ^[0-9]+ ]] ; then
path="/home/deploy/ci-xxx-$deploy_name/$port/"
path_without_slash=`echo $path|cut -c1-10`
#echo $path
#dir exists?
if [[ ! -d $path ]]; then
status_mkdir=`mkdir -p $path 2>&1`
echo $status_mkdir
if [[ $status_mkdir =~ "$path_without_slash" ]]; then
echo "the runner don't have the rights to mkdir $path , deploy stoped."
exit
fi
echo 'mkdir done'
fi
cd $path
#git repo exists?
git_exists=`git status 2>&1`
if [[ $git_exists =~ ^fatal ]] ;then
echo `git clone "$repo" .`
echo 'clone repo done'
else
#get current branch
current_branch=`git branch | grep '*' | cut -f2 -d" "`
#echo "current_branch ${current_branch}, discarding changes..."
#discard any local changes
nouse=`git reset --hard origin/$current_branch 2>&1`
fi
#fetch all from remote , may reduce size
nouse=`git fetch -p 2>&1`
nouse=`git checkout $git_branch 2>&1`
nouse=`git merge origin/$git_branch 2>&1`
chmod -R 777 $path
pull_end_time=`date '+%H:%M:%S'`
#modify static address
timestap=`date +%s`
files=$(find . \( -name "*.html" \) -print)
perl -i -p -e "s/(src|link href)=\"\//\1=\"http:\/\/beta.www.xxx.com:$port\//g" $files
#perl -i -p -e "s/\.(css|png|gif|jpg)\?(\d)+/.\1?$timestap/g" $files
static_update_time=`date '+%H:%M:%S'`
#du -sh $path 2>/dev/null
#du_end_time=`date '+%H:%M:%S'`
echo "$start_time => $pull_end_time => $static_update_time"
#echo "=>du $du_end_time"
fi
$postdata = file_get_contents("php://input");
if ($postdata) {
$jsondata = json_decode($postdata, true);
if (!$jsondata || !isset($jsondata['ref'])) {
die('bad request');
}
$branch = str_replace('refs/heads/','', $jsondata['ref']);
//8000-65535
if (preg_match('/^(\d{4,5})_*/', $branch, $match)) {
//为了安全。。
if (!preg_match('/^[\w\.]{4,}$/', $branch)) {
error_log('auto_deploy_log invalid branch name ' . $branch . ' ' . '注:走自动部署的端口号分支名称只能是字母、数字、下划线、点,且以端口号开头用下划线连接其它部分。');
exit;
}
// $port = $match[1];
// if (!in_array($port, array('8010', '8990', '8765', '8866', '9010'))) {
// die('not parse this port');
// }
$repo = $jsondata['repository']['url'];
$after = $jsondata['after'];
$username = $jsondata['user_name'];
if ($after == '0000000000000000000000000000000000000000') {
//error_log('auto_deploy_log ' . $branch . ' deleted');
exit;
}
$filepath = __DIR__ . '/../../../../shell/deploy/git_deploy';
$filepath = realpath($filepath);
$cmd = $filepath . " $branch $repo $after $username 2>&1";
exec($cmd, $output);
error_log('auto_deploy_log ' . implode(' ', $output));
echo "ok!\n";
} else {
echo "not a valid branch!\n";
}
} else {
echo "bad request!\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment