前几天学习了linux搭建git服务器及自动化部署,周末没事研究下怎么使用shell脚本实现,这样以后使用的时候也方便
#!/bin/bash
read -p "请输入项目名称:" name
if [ "$name" = "" ];then
echo "项目名称为空,停止执行"
exit
fi
# git目录
git_dir="/git/$name"
# git仓库
git_repository="/git/$name/$name.git"
# web文件目录
web_dir="/home/wwwroot/$name"
# 钩子文件
file_path="$git_repository/hooks/post-receive"
# 创建并修改web文件目录所有者
if [ ! -d "$web_dir" ];then
mkdir $web_dir
echo "$web_dir文件夹创建成功"
else
echo "$web_dir文件夹已存在"
fi
chown -R git:git $web_dir
# 创建git目录
if [ ! -d "$git_dir" ];then
mkdir $git_dir
echo "$git_dir文件夹创建成功"
else
echo "$git_dir文件夹已存在"
fi
chown git:git $git_dir
# 创建git仓库
if [ ! -d "$git_repository" ];then
git init --bare $git_repository
echo "仓库$git_repository创建成功"
else
echo "仓库$git_repository已存在"
fi
chown -R git:git $git_repository
# 创建git钩子文件
touch $file_path
echo "git --work-tree=$web_dir --git-dir=$git_repository checkout -f" > $file_path
chown -R git:git $file_path
chmod +x $file_path
if [ -f "$file_path" ];then
echo "钩子文件创建成功"
else
echo "钩子文件创建失败"
fi