首先,开发人员使用git服务写代码并打tag,利用ssh服务将代码上传至git服务器,命令入下:
# 重复操作目录,将改动存入版本库(打快照)
[root@develop myweb]# echo 'tedu cloud' > 1.txt
[root@develop myweb]# ls1.txt readme
[root@develop myweb]# git add .
[root@develop myweb]# git commit -m "增加1.txt"
[root@develop myweb]# git tag
[root@develop mywebt]# git push
[root@develop myproject]# git push --tags
其他常用git命令:
git status
git log --oneline
git reset --hard hard-id
git branch
git checkout master
脚本一:在Jenkins服务器上,使用脚本从Git服务器拉取代码
pkg_dir=/var/ftp/deploy/packages
cp -r myproject-$web $pkg_dir
rm -rf $pkg_dir/myproject-$web/.git
cd $pkg_dir
tar -czpf myproject-$web.tar.gz myproject-$web
rm -rf myproject-$web
md5sum myproject-$web.tar.gz | awk '{print $1}' > myproject-$web.tar.gz.md5
cd ..
echo -n $web > ver.txt
脚本二:web应用服务器使用ftp服务,以运行脚本方式从Jenkins服务器下载代码,并部署到对应的应用服务器。代码入下:
[root@web1 ~]# cat web.sh
#!/bin/bash
ftp_url=ftp://192.168.22.30/deploy
deploy_dir=/var/www/deploy
dest=/var/www/html/tedu-cloud
down_file(){
versinotallow=$(curl -s $ftp_url/ver.txt)
wget -q $ftp_url/ver.txt -O $deploy_dir/ver.txt
wget -q $ftp_url/packages/myproject-$version.tar.gz -O $deploy_dir/myproject-$version.tar.gz
hash=$(md5sum $deploy_dir/myproject-$version.tar.gz | awk '{print $1}')
ftp_hash=$(curl -s $ftp_url/packages/myproject-$version.tar.gz.md5)
if [ "$hash" == "$ftp_hash" ]; then
tar -xf $deploy_dir/myproject-$version.tar.gz -C $deploy_dir
else
rm -f $deploy_dir/myproject-$version.tar.gz
fi
if [ -e "$dest" ]; then
rm -f $dest
fi
ln -s $deploy_dir/myproject-$version $dest
}
if [ ! -e "$deploy_dir" ]; then
mkdir $deploy_dir
fi
if [ ! -f $deploy_dir/ver.txt ]; then
down_file
fi
if [ -f $deploy_dir/ver.txt ]; then
ftp_ver=$(curl -s $ftp_url/ver.txt)
local_ver=$(cat $deploy_dir/ver.txt)
if [ "$ftp_ver" != "$local_ver" ]; then
down_file
fi
fi
最后,客户的测试访问:
访问http://服务器ip地址/tedu-cloud,可以看到部署的内容