Git on Server

Git是一个SSH服务器

Git使用ssh协议来连接,其在服务端上也处于身份“SSH服务器”(兼容)。

配置服务端

创建git用户组,因为一般不会使用root用户来管理仓库。

 1# 创建组
 2groupadd git
 3# 创建用户并加入组
 4useradd git -g git
 5
 6# 设置密码
 7passwd git
 8# 创建根目录
 9mkdir -p /repo
10chown git:git /repo

创建仓库

1# 新建仓库,--bare无工作区仓库
2git --bare init hello
3
4# 客户端主机,类似scp命令
5git clone git@server_ip:/repo/hello

接受更新动作

当服务端收到push动作时,更新仓库。而我们要从裸仓库中自动签出工作区。

1# 进入储存库.git中的hooks
2cd /repo/hello/hooks
3# 创建文件,收到push动作触发
4touch post-receive
5# 赋权
6chmod +x post-receive

内容参考:把仓库工作区内容签出到指定目录

1#!/bin/bash
2# 定义库名称
3NAME="hello"
4TARGET="/opt/$NAME"
5GIT_DIR="/repo/$NAME"
6# 创建目标目录
7mkdir -p $TARGET
8# 签出
9git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f

后续还可以添加一些自动构建命令

Licensed under CC BY-NC-SA 4.0
Chrsitine's blog ❤️
Built with Hugo
Theme Stack designed by Jimmy