shell脚本创建git仓库并实现自动化部署

2次阅读

共计 870 个字符,预计需要花费 3 分钟才能阅读完成。

前几天学习了 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
正文完
 0