blog-site

git clone git://git.lin.moe/blog-site.git

  1---
  2title: "极简自建个人 git"
  3date: 2023-05-21T12:54:37+08:00
  4draft: true
  5---
  6
  7## 简述
  8手工创建 git bare repo ,系统 ssh + git-daemon + cgit ,git-hooks 走 systemd-run 做 CI  
  9
 10## 远程访问
 11### SSH 
 12简单的使用系统本身的 ssh daemo 
 13
 14如果系统已经带了 git 用户,使用原有的就行了,如果系统不带:  
 15
 16```
 17# 新建 git 用户
 18# 使用 git-shell 作为默认 shell
 19# 家目录放在 /srv/git 
 20useradd -d /srv/git -m -s /usr/bin/git-shell git
 21```  
 22
 23把你的公钥放在 /srv/git/.ssh/authorized_keys 中,如果有必要,也可以给 git 用户初始化密码  
 24
 25> 这种方式有一个缺点,不能用 `ssh://git.example.tld/repo.git` 的 remote url 访问。  
 26>
 27> 必须用 `ssh://git.example.tld/~/repo.git` 指明家目录下的 repo 。  
 28> 
 29> 或者 `git@git.example.tld:repo.git`  
 30
 31### git 协议  
 32安装 `git-daemon` ,如果是 debian ,包名是 `git-daemon-run`
 33```
 34apt install git-daemon-run
 35```
 36
 37创建一个 systemd-service 文件 
 38
 39```
 40# /etc/systemd/system/git-daemon.service
 41
 42[Unit]
 43Description=Start Git Daemon
 44
 45[Service]
 46ExecStart=/usr/bin/git daemon --reuseaddr --base-path=/srv/git/ /srv/git/
 47
 48Restart=always
 49RestartSec=500ms
 50
 51StandardOutput=syslog
 52StandardError=syslog
 53SyslogIdentifier=git-daemon
 54
 55User=git
 56Group=git
 57
 58[Install]
 59WantedBy=multi-user.target
 60```
 61
 62```
 63# 启动
 64systemctl enable --now git-daemon
 65```
 66### Web
 67我选用的是 cgit ,需要安装 cgit 和 fastcgi 
 68```
 69# debian 
 70apt-get install -y cgit fcgiwrap
 71```
 72
 73```
 74# /etc/cgitrc 配置文件
 75
 76# cgit config
 77# see cgitrc(5) for details
 78
 79css=/cgit.css
 80logo=/avatar.jpg
 81virtual-root=/
 82
 83clone-url=git://git.lin.moe/$CGIT_REPO_URL https://git.lin.moe/$CGIT_REPO_URL
 84
 85readme=:README
 86readme=:README.txt
 87readme=:README.html
 88readme=:README.htm
 89
 90about-filter=/usr/lib/cgit/filters/about-formatting.sh
 91
 92repository-sort=age
 93
 94enable-index-owner=1
 95enable-http-clone=1
 96enable-blame=1
 97enable-commit-graph=1
 98enable-log-filecount=1
 99enable-log-linecount=1
100enable-index-owner=0
101enable-index-links=0
102branch-sort=age
103max-stats=quarter
104root-title=Lin's cgit
105root-desc=
106footer=
107# root-readme=
108snapshots=tar.gz zip
109
110enable-git-config=1
111
112mimetype.gif=image/gif
113mimetype.html=text/html
114mimetype.jpg=image/jpeg
115mimetype.jpeg=image/jpeg
116mimetype.pdf=application/pdf
117mimetype.png=image/png
118mimetype.svg=image/svg+xml
119		
120scan-path=/srv/git
121```
122
123使用 nginx 作为 web 服务器
124```
125# /etc/nginx/site-enabled/cgit
126
127server {
128  listen 443 http2 ssl;
129  listen [::]:443 http2 ssl;
130  server_name git.example.tld;
131
132  ssl_certificate /path/to/certificate;
133  ssl_certificate_key /path/to/private_key;
134
135  root /usr/share/cgit;
136
137  try_files $uri @cgit;
138
139  location ~ /.+/(info/refs|git-upload-pack) {
140    include             fastcgi_params;
141    fastcgi_param       SCRIPT_FILENAME     /usr/lib/git-core/git-http-backend;
142    fastcgi_param       PATH_INFO           $uri;
143    fastcgi_param       GIT_HTTP_EXPORT_ALL 1;
144    fastcgi_param       GIT_PROJECT_ROOT    /srv/git;
145    fastcgi_param       HOME                /srv/git;
146    fastcgi_pass        unix:/run/fcgiwrap.socket;
147  }
148
149  location @cgit {
150    include             fastcgi_params;
151    fastcgi_param       SCRIPT_FILENAME /usr/lib/cgit/cgit.cgi;
152    fastcgi_param       PATH_INFO       $uri;
153    fastcgi_param       QUERY_STRING    $args;
154    fastcgi_param       HTTP_HOST       $server_name;
155    
156    # 使 go get [module] 可用
157    sub_filter '</head>'
158      '<meta name="go-import" content="$host$uri git https://$host$uri"></head>';
159    sub_filter_once on;
160
161    fastcgi_pass        unix:/run/fcgiwrap.socket;
162  }
163}
164```  
165
166## git shell command
167在 git 用户
168
169## CI 
170CI 要极简风,有点麻烦。我用 git server-side hooks + systemd-run + go-task 来管理。