CentOS 7 builds Git to realize automatic code synchronization/automatic deployment

The basic steps

  1. install git
  2. Create groups and users for system GIT
  3. Create a GIT repository to sync with the settings
  4. clone server repository to server directory
  5. Clone the server's GIT repository to the local
  6. Sync native code from server

Environment and Description

  1. CentOS 7 64x

1. Install GIT

1.1 Install Git dependencies

yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel

1.2 Install Git

yum install -y git

After the installation is complete, you can git --versionverify whether the git installation was successful by

git --version
git version 1.8.3.1

2. Create groups and users of system GIT

Create separate system user groups and users for Git to improve system security.

2.1 Create a system Git user group

# user-gourp 替换为您想创建的用户组,一般 git 即可
groupadd user-group

2.2 Create a Git link user under the system Git group

# username 替换为您的 Git 用户名
# user-gourp 替换为您创建 Git 用户组
adduser username -g user-gourp 

2.3 Set a password for the Git user

# username 替换为您的 Git 用户名
passwd username 
Changing password for user username.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

If it is an official server, in order to avoid being attacked, please set the password strength to be stronger

3. Create a GIT repository and set synchronization

3.1 Create a GIT repository

The warehouse can be created in any location, if the system disk is tight, it can be created in the mount disk or other directory

This article takes the creation in the home directory as an example

# 切换至 home 目录
cd /home
# 创建 git_repository 目录
mkdir git_repository
# 切换至 git_repository 目录
cd git_repository
# 创建一个裸的 git 仓库 --bare 是代表创建裸仓库,这个参数一定记得带上
git init --bare demo.git
# 把 demo.git 目录赋权给您创建的 Git 用户
# username 替换为您创建 Git 用户名
# user-gourp 替换为您创建 Git 用户组
chown -R username:user-group demo.git
# 文件只有文件所有者或同在一个用户组下的用户可读,可写,可执行,其他用户只有读权限
chmod 774 -R demo.git

3.2 Set up GIT directory synchronization

The principle is to create a script that is executed when a commit request is received, so that the post-receive script is triggered when you execute git push in the local repository.

3.2.1 Create post-receive script file

# your-git-repository-path 替换为您 git_repository 路径
cd /your-git-repository-path/demo.git

# 切换目录至 hooks
cd hooks

# 创建 post-receive
vi post-receive

3.2.2 Modify the post-receive script file

#!/bin/sh
unset GIT_DIR
# 您项目的工作目录
ProjectPath="/var/www/demo"

# 定义日志目录
LogPath="/your-git-repository-path/project.git/hooks"  

# 输出日期到日志
echo -e "\n=================  `date +"%Y-%m-%d %H:%M:%S"`  ===============\n" >> $LogPath/auto-sync.log 2>&1

# 自动同步方法

# 1. 直接拉取并合并
git pull origin master  >> $LogPath/auto-sync.log 2>&1 

# 2. 拉取最新版本至本地,不自动合并
# git fetch --all
# git reset --hard origin/master

echo "====================================================== "  >> $LogPath/auto-sync.log 2>&1

There are 2 unresolved issues here

  1. Define the log directory, if you define hooks outer layer, it cannot be written. The empowerment has been completed and it has not come out.

  2. According to the GitHooks artifact of the server 's automatic deployment project, it is implemented with git fetch instead of git pull. The difference is that:

    1. git fetch: It is equivalent to fetching the latest version from the remote to the local, and will not automatically merge.
    2. git pull: It is equivalent to fetching the latest version from the remote and merging to the local.
    3. The pull implementation is equivalent to fetch and then use merge to merge local and remote code.

    There is a problem here. If the developer makes a mistake in the submission process, after using git reset to reset, the remote code version is lower than the code version on the web side, and the synchronization with the developer's local code cannot be achieved when using pull again. . So after using fetch here, it is forced to use reset to achieve the same code version pointer on the web side as on the git server side. (If you can use stash on this machine, then pull, and then drop the content of the stash just now)

    What he meant by this sentence was understood, but he did not understand the specific operation.

3.2.3 post-receive script file authorization

# username 替换为您创建 Git 用户名
# user-gourp 替换为您创建 Git 用户组
chown username:user-group post-receive
chmod -R 774 post-receive

Fourth, clone the server warehouse to the server directory

The language for automatic code synchronization in this article is php, other languages ​​are similar, please switch the server directory by yourself

# 切换至 apache 项目目录
cd /var/www

# your-git-repository-path 替换为您 git_repository 路径
# project-name 切换为clone 后您需要的项目名称
git clone /your-git-repository-path/demo.git project-name

# 赋权
# username 替换为您创建 Git 用户名
# user-gourp 替换为您创建 Git 用户组
# project-name 替换为clone 后的项目名称
# 赋权775 其他用户可读可执行,否则项目可能启动不了
chown -R username:user-group project-name
chmod -R 775 project-name 

5. Clone the server's GIT repository to the local

The tools of each operating system are inconsistent, you can use the tools and command line to synchronize to the local .

link address:

# username 替换为您创建 Git 用户名
# your-git-repository-path 替换为您 git_repository 路径
# server 可以是 IP,也可以是域名
# port 当您修改了端口时需要,未设置为空即可
ssh://username@server:port/your-git-repository-paht/demo.git

Example:

ssh://[email protected]/home/git_respository/demo.git

6. Synchronize the local code from the server

Available tools, you can also order to submit code from the server, I won't say more here

Error log can view auto-sync.log

# your-git-repository-path 替换为您 git_repository 路径
cat /your-git-repository-paht/demo.git/hooks/auto-sync.log

Advertising column: welcome to follow my personal blog

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325443291&siteId=291194637