言成言成啊 | Kit Chen's Blog

Gitblit迁移Gitea

Gitblit、Gitlab、Gitea,之前一直使用的Gitblit,相对来说,是目前所有GitServer中,最具性价比的。

但是后来服务器也更换了,所以就想找一个性价比次高的平台,那就是Gitea了。

一、安装

官网链接从二进制安装 - Docs

首先Gitea需要git版本2以上的,Centos7默认自带的是git1.8,所以需要更新yum存储库。

1
2
3
yum -y install \
https://repo.ius.io/ius-release-el7.rpm \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

搜索Git并安装合适版本

1
2
3
yum search git|grep 'Fast Version Control System'
yum -y install git236
git --version

下载并安装gitea

1
2
3
4
5
6
7
8
9
10
11
curl -L -o /usr/local/bin/gitea https://dl.gitea.io/gitea/1.17.1/gitea-1.17.1-linux-amd64
chmod +x gitea
useradd git
# userdel -rf git
# 创建结构
mkdir -p /var/lib/gitea/{custom,data,log}
chown -R git:git /var/lib/gitea/
chmod -R 750 /var/lib/gitea/
mkdir /etc/gitea
chown root:git /etc/gitea
chmod 770 /etc/gitea

安装完成之后,需要将gitea配置成服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
cat > /usr/lib/systemd/system/gitea.service <<EOF
[Unit]
Description=Gitea (Git with a cup of tea)
After=syslog.target
After=network.target
###
# Don't forget to add the database service dependencies
###
#
#Wants=mysql.service
#After=mysql.service
#
#Wants=mariadb.service
#After=mariadb.service
#
#Wants=postgresql.service
#After=postgresql.service
#
#Wants=memcached.service
#After=memcached.service
#
#Wants=redis.service
#After=redis.service
#
###
# If using socket activation for main http/s
###
#
#After=gitea.main.socket
#Requires=gitea.main.socket
#
###
# (You can also provide gitea an http fallback and/or ssh socket too)
#
# An example of /etc/systemd/system/gitea.main.socket
###
##
## [Unit]
## Description=Gitea Web Socket
## PartOf=gitea.service
##
## [Socket]
## Service=gitea.service
## ListenStream=<some_port>
## NoDelay=true
##
## [Install]
## WantedBy=sockets.target
##
###

[Service]
# Modify these two values and uncomment them if you have
# repos with lots of files and get an HTTP error 500 because
# of that
###
#LimitMEMLOCK=infinity
#LimitNOFILE=65535
RestartSec=2s
Type=simple
User=git
Group=git
WorkingDirectory=/var/lib/gitea/
# If using Unix socket: tells systemd to create the /run/gitea folder, which will contain the gitea.sock file
# (manually creating /run/gitea doesn't work, because it would not persist across reboots)
#RuntimeDirectory=gitea
ExecStart=/usr/local/bin/gitea web --config /etc/gitea/app.ini
Restart=always
Environment=USER=git HOME=/home/git GITEA_WORK_DIR=/var/lib/gitea
# If you install Git to directory prefix other than default PATH (which happens
# for example if you install other versions of Git side-to-side with
# distribution version), uncomment below line and add that prefix to PATH
# Don't forget to place git-lfs binary on the PATH below if you want to enable
# Git LFS support
#Environment=PATH=/path/to/git/bin:/bin:/sbin:/usr/bin:/usr/sbin
# If you want to bind Gitea to a port below 1024, uncomment
# the two values below, or use socket activation to pass Gitea its ports as above
###
#CapabilityBoundingSet=CAP_NET_BIND_SERVICE
#AmbientCapabilities=CAP_NET_BIND_SERVICE
###
# In some cases, when using CapabilityBoundingSet and AmbientCapabilities option, you may want to
# set the following value to false to allow capabilities to be applied on gitea process. The following
# value if set to true sandboxes gitea service and prevent any processes from running with privileges
# in the host user namespace.
###
#PrivateUsers=false
###

[Install]
WantedBy=multi-user.target
EOF

之后就正常作为服务操作

1
systemctl start|stop|restart|status|enable|disable gitea

二、Hook钩子

使用Git的Hook钩子,实现push操作后,服务器自动将上传内容部署到博客的路由下。

由于使用Gitblit习惯了,找了半天Gitea上面,都没找到hooks,原来是需要直接在仓库下面配置。而不是其他平台在网页配置。

以我仓库为例

1
cd /var/lib/gitea/data/gitea-repositories/meethigher/blog.git/hooks/post-receive.d

创建一个push后要触发的hook钩子脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cat > /var/lib/gitea/data/gitea-repositories/meethigher/blog.git/hooks/post-receive.d/post-receive <<EOF
#!/usr/bin/env bash
repo_dir=/var/lib/gitea/data/gitea-repositories/meethigher/blog.git
blog_dir=/var/lib/gitea/data/gitea-repositories/meethigher/blog
web_dir=/site/web/youarebitch/
rm -rf \${blog_dir}
echo "delete bak"
git clone \${repo_dir} \${blog_dir}
echo "clone into bak"
rm -rf \${web_dir}blog/*
echo "delete old blog"
cp -rf \${blog_dir} \${web_dir}
echo "copy bak into new blog"
echo "success"
EOF

一定要添加执行权限

1
chmod +x /var/lib/gitea/data/gitea-repositories/meethigher/blog.git/hooks/post-receive.d/post-receive

然后重启gitea

1
systemctl restart gitea

push上传测试一下,查看git服务端返回内容

三、参考

IUS - YUM存储库

从二进制安装 - Docs

使用 Gitea + Git Hook 实现 Hexo 博客源码托管与持续集成 | 朝花夕拾

Hexo使用Git部署到Windows

发布:2022-09-06 23:25:09
修改:2022-09-07 21:29:57
链接:https://meethigher.top/blog/2022/gitea/
标签:devops git hexo 
付款码 打赏 分享