Shell script creates git repository and realizes automatic deployment

Time:2020-10-9

A few days ago, I learned how to build git server and automatic deployment in Linux. I had nothing to do at the weekend to study how to use shell script to realize it, so it is convenient to use it in the future

#!/bin/bash

Please enter the project name: "name

if [ "$name" = "" ];then
    Echo "project name is empty, stop execution"
    exit
fi

#Git directory
git_dir="/git/$name"
#Git warehouse
git_repository="/git/$name/$name.git"
#Web file directory
web_dir="/home/wwwroot/$name"
#Hook file
file_path="$git_repository/hooks/post-receive"



#Create and modify the web file directory owner
if [ ! -d "$web_dir" ];then
    mkdir $web_dir
    echo "$web_ Dir folder created successfully“
else
    echo "$web_ The dir folder already exists“
fi
chown -R git:git $web_dir

#Create git directory
if [ ! -d "$git_dir" ];then
    mkdir $git_dir
    echo "$git_ Dir folder created successfully“
else
    echo "$git_ The dir folder already exists“
fi
chown git:git $git_dir

#Creating git repository
if [ ! -d "$git_repository" ];then
    git init --bare $git_repository
    Echo "warehouse $Git_ Repository created successfully“
else
    Echo "warehouse $Git_ Repository already exists“
fi
chown -R git:git $git_repository

#Create git hook file
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 "hook file created successfully"
else
    Echo "hook file creation failed"
fi