Skip to content

Instantly share code, notes, and snippets.

@picasso250
Last active May 14, 2018 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save picasso250/0b5ec345f3c9a4e96cb1b6c30d5d5f82 to your computer and use it in GitHub Desktop.
Save picasso250/0b5ec345f3c9a4e96cb1b6c30d5d5f82 to your computer and use it in GitHub Desktop.
deploy PHP project on linux
#!/bin/bash
# deploy project with ssh
# has to work with deploy_remote
set -eu
git_root_dir=~/Documents/work/git/xxx # the dir of git
git_src_dir=. # if you don't want copy whole dir, change it to sub dir
git_sub_dir="a b" # sub dir list to deploy
exclude="--exclude lms/vendor --exclude *.png --exclude assets" # to make the tar smaller
host=work@ip # where's your host
dest_dir=/var/www # where to untar the file
if [ ! -d $git_root_dir ] ; then
echo "$git_root_dir not dir"
exit 1
fi
usage() {
echo "Usage: $0 [release_branch]"
}
if [ $# -eq 0 ] ; then
usage
exit
fi
branch_name=master
if [ $# -eq 1 ] ; then
if [ $1 = '-h' -o $1 = '--help' ] ; then
usage
exit
fi
branch_name=$1
fi
cd $git_root_dir
# prepare files in git
echo "git fetch && merge"
git fetch
if [ ! $( git branch -a | grep $branch_name -c ) -ge 1 ] ; then
echo No branch $branch_name
exit 1
fi
git checkout -f $branch_name
git merge origin/$branch_name
echo "copy "
cd $git_src_dir
tmp_file=deploy.$branch_name.tar.gz
echo $branch_name > VERSION
sha1=$(git show | head -n1)
echo "$sha1" >> VERSION
echo "$sha1"
tar -czf $tmp_file $exclude $git_sub_dir VERSION
remote_tmp_file="/tmp/$tmp_file"
scp $tmp_file "$host:$remote_tmp_file"
echo "excute remote"
ssh $host "bash /data/bin/deploy_remote $remote_tmp_file $dest_dir"
/bin/rm $tmp_file
echo "OK"
#!/bin/bash
# 使用方式
# deploy [0126]
set -euo pipefail
#set -x # debug
git_root_dir=/data/git_root
dest_dir=/data/www
sub_dir_list="public or something" # * or file dir list
if [ ! -d $git_root_dir ] ; then
echo "$git_root_dir not dir"
exit 1
fi
if [ ! -d $dest_dir ] ; then
echo "$dest_dir not dir"
exit 1
fi
branch_name=master
if [ $# -eq 1 ] ; then
if [ $1 = '-h' -o $1 = '--help' ] ; then
echo "Usage: $0 release_branch"
exit
fi
branch_name=$1
fi
cd $git_root_dir
# prepare files in git
cmd="git fetch"
echo $cmd
$cmd
if [ ! $( git branch -a | grep $branch_name -c ) -ge 1 ] ; then
echo No branch $branch_name
exit 1
fi
echo "checkout $branch_name"
git checkout -f $branch_name
git merge origin/$branch_name
echo "copy $sub_dir_list => $dest_dir/"
if [ "$sub_dir_list" = "*" ] ; then
/bin/cp -urv * $dest_dir/
else
for sub_dir in $sub_dir_list ; do
/bin/cp -urv $sub_dir/* $dest_dir/$sub_dir/
done
fi
dep=$dest_dir/$sub_dir/.deploy.sh
if [ -f $dep ] ; then
/bin/bash $dep
/bin/rm $dep
fi
echo "OK"
#!/bin/bash
# put me in /data/bin/
# $1 is file
# $2 is the dest dir
# $3 is the name of dir
set -euo pipefail
cd /tmp
if [ ! -d deploy ] ; then
mkdir deploy
fi
cd deploy
tar -xzf $1
/bin/cp -urv * $2/
/bin/rm $1
/bin/rm -r /tmp/deploy
@picasso250
Copy link
Author

picasso250 commented Jan 29, 2018

用法:

必须将deploy_remote放到/data/bin/文件夹下

假如你想将git目录下的/a/b发布到远方的/a/b
则e

git_then_dir=a
dest_dir=/a

然后执行 ./deploy-ssh b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment