当前位置 博文首页 > lyndon:谁不想拥有自己的代码仓库?

    lyndon:谁不想拥有自己的代码仓库?

    作者:[db:作者] 时间:2021-08-23 16:18

    自己搭建代码仓库的好处

    1. github 在国外,从国内正常访问的话,速度很慢。
    2. gitee、codechina 在国内,访问速度倒是很快,但是单个仓库大小、仓库总数量都受平台限制。
    3. 有时公司需要搭建自己的代码仓库

    这时候,自己搭建代码仓库的好处就体现出来了。下面我们就讲讲如何使用 Gogs 搭建自己的代码仓库。

    下载二进制程序

    https://gogs.io/docs/installation/install_from_binary.html

    根据自己的平台下载压缩包,我的环境是 Linux amd64
    请添加图片描述

    解压

    [root@Box tmp]# tar xvf gogs_0.12.3_linux_amd64.tar.gz -C /opt/
    

    运行

    [root@Box gogs]# ./gogs web
    2021/08/20 15:30:36 [ WARN] Custom config "/opt/gogs/custom/conf/app.ini" not found. Ignore this warning if you're running for the first time
    2021/08/20 15:30:36 [TRACE] Log mode: Console (Trace)
    2021/08/20 15:30:36 [ INFO] Gogs 0.12.3
    2021/08/20 15:30:36 [TRACE] Work directory: /opt/gogs
    2021/08/20 15:30:36 [TRACE] Custom path: /opt/gogs/custom
    2021/08/20 15:30:36 [TRACE] Custom config: /opt/gogs/custom/conf/app.ini
    2021/08/20 15:30:36 [TRACE] Log path: /opt/gogs/log
    2021/08/20 15:30:36 [TRACE] Build time: 2020-10-07 03:03:48 UTC
    2021/08/20 15:30:36 [TRACE] Build commit: f0e3cd90f8d7695960eeef2e4e54b2e717302f6c
    2021/08/20 15:30:36 [ INFO] Run mode: Development
    2021/08/20 15:30:36 [ INFO] Listen on http://0.0.0.0:3000
    

    从浏览器进行后续部署

    http://<server_ip>:3000/install

    可能出现的问题:浏览器无响应。

    解决办法:关闭服务器防火墙。

    ubuntu:iptables -Xiptables -Ziptables -F

    centos:systemctl stop firewalld.service,永久关闭 systemctl disable firewalld.service
    请添加图片描述
    为了部署简单,数据库选用 sqlite3,这样就不需要安装任何数据库了,甚至 sqlite3 命令都不需要安装。

    登录

    请添加图片描述

    创建仓库并上传项目

    请添加图片描述

    开机自启

    这里着重讲一下开机自启,我尝试了好几种方式都不行,如下

    1. 在 rc.local 中添加 /opt/gogs/gogs web > /dev/null 2>&1 &,结果不生效,这行命令应该是执行了(在其上面添加了一句测试命令,执行到了),只是没有真正把 gogs 启动起来。
    2. 在 /etc/init.d/ 中添加自启脚本,链接到 rc[2-5].d 目录下,依旧不生效。

    最后发现,其实,

    gogs 已经提供了相应开机启动服务脚本,不过需要根据自己的配置修改

    /opt/gogs/scripts/systemd/gogs.service

    [root@Box systemd]# cat gogs.service
    [Unit]
    Description=Gogs
    After=syslog.target
    After=network.target
    After=mariadb.service mysqld.service postgresql.service memcached.service redis.service
    
    [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
    Type=simple
    User=root
    Group=root
    WorkingDirectory=/opt/gogs
    ExecStart=/opt/gogs/gogs web
    Restart=always
    Environment=USER=root HOME=/opt
    
    # Some distributions may not support these hardening directives. If you cannot start the service due
    # to an unknown option, comment out the ones not supported by your version of systemd.
    ProtectSystem=full
    PrivateDevices=yes
    PrivateTmp=yes
    NoNewPrivileges=true
    
    [Install]
    WantedBy=multi-user.target
    

    拷贝到 /usr/lib/systemd/system,并使能服务,重启

    [root@Box systemd]# cp gogs.service /usr/lib/systemd/system
    [root@Box systemd]# systemctl enable gogs.service
    Created symlink /etc/systemd/system/multi-user.target.wants/gogs.service → /usr/lib/systemd/system/gogs.service.
    [root@Box systemd]# reboot
    

    查看 gogs 是否启动

    [root@Box ~]# ps -ef
    ...
    root         916       1  1 17:45 ?        00:00:00 /opt/gogs/gogs web
    ...
    

    看到 gogs 已经自启了,浏览器也可以正常访问。

    展示

    这是我三年前在云服务器上搭建的,一直稳定运行到现在
    在这里插入图片描述
    在这里插入图片描述
    希望你们也拥有自己的代码仓库。

    cs