Gogs安装

安装docker
1
yum install -y docker
拉取gogs镜像
1
docker pull gogs/gogs:0.11.29
docker 运行容器
1
2
docker run -d --name=gogs -p 8030:3000 -p 8022:22 -v /docker/gogs:/data gogs:0.11.29
#8030和8022为外部访问端口,3000和22对应容器内部端口
查看是否运行成功
1
2
3
[root@public-service ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2317dd952a84 docker.io/gogs/gogs:0.11.29 "/app/gogs/docker/..." 4 hours ago Up 53 minutes 0.0.0.0:8022->22/tcp, 0.0.0.0:8030->3000/tcp gogs

安装完成后,访问http://192.168.1.1:8030

数据库设置,这里我们直接使用内置的 数据库即可,使用其他的需要自行搭建数据库;

应用基本设置,主要修改域名、SSH端口号和应用URL即可。

#### Gogs使用

外网访问路径为:http://192.168.1.1:8030/

登录

使用各自的名称全拼登录,例如(倪明坤,用户名为nimingkun,密码为nimingkun)

登录之后点击头像右侧的按钮,进入用户设置,修改密码

查看账户对应的仓库
创建用户
用户授权
创建组织

注意:需要把仓库建立再组织下

查看账户对应的组织
创建新的仓库

注意拥有者应设置为组织名,禁止设置为自己的用户

创建后可根据命令进行拉取或工具拉取

HTTP方式拉取

开发需要在本地配置

示例:

1
2
git config --global user.name "nimingkun"
git config --global user.email nimingkun@liangjian.com

配置后即可拉取git代码

1
2
3
4
5
6
7
8
9
10
11
[root@public ~]# git clone http://192.168.1.1:8030/hospWeb/bridge.git
正克隆到 'bridge'...
Username for 'http://192.168.1.1:8030': nimingkun
Password for 'http://nimingkun@192.168.1.1:8030':
remote: Counting objects: 1966, done.
remote: Compressing objects: 100% (777/777), done.
remote: Total 1966 (delta 1093), reused 1966 (delta 1093)
接收对象中: 100% (1966/1966), 773.85 KiB | 0 bytes/s, done.
处理 delta 中: 100% (1093/1093), done.
[root@public code]# ls
bridge
SSH方式拉取

开发需要在本地配置

示例:

1
2
git config --global user.name "nimingkun"
git config --global user.email nimingkun@liangjian.com

生成SSH Key(密钥)

1
ssh-keygen -t rsa -C "nimingkun@liangjian.com"

示例:

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
[root@public code]# ssh-keygen -t rsa -C "nimingkun@liangjian.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:G706PSkd8DxM7JS+wDzdrmZbFiZWuCYq/cpZXmr1g1k nimingkun@liangjian.com
The key's randomart image is:
+---[RSA 2048]----+
| |
| . |
| .... |
| ..=o |
| oS@*.o |
| . .=*@+E. |
| . o o*o@o |
| o =++@o+ |
| +o==o+ . |
+----[SHA256]-----+
[root@public code]# ll ~/.ssh/
总用量 12
-rw------- 1 root root 1679 10月 13 15:23 id_rsa
-rw-r--r-- 1 root root 405 10月 13 15:23 id_rsa.pub
-rw-r--r--. 1 root root 175 9月 21 16:32 known_hosts

Windows会生成在用户目录的.ssh下

查看公钥

1
2
[root@public code]# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDGyFIujsLkRzIVmfZJC2W7scE+JXljm54r98Od9LgTSsJosClHd9AmMNENtr1pX1s/yfp1UQVGk+BUJav+PrbboGJqE/PUQgQ3PaYhgl0YoVImLF3XvALJI3SgMTddRy4SOMkdqfzQnZRYY1R6YIpv+JNRY7W4HrbNRqP1x+HwFPP+fD/5tmF+rSKgHdJR6ygMxMf5y16V2FHjmf4s3iPAba10yvE/WW66/KV8s/FOnyjFNMYYS/aBsI73ilvE6sg9zLnZYxLIqMKIaQWak/p9iXTfRkJSI5d3sWx2c4tPyQsFJM895vsp0n+bKU02u/ekvaAZTEzHZ/5AjPK6IX91 nimingkun@liangjian.com

在页面中添加公钥

使用ssh链接进行克隆项目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@public code]# git clone ssh://git@192.168.1.1:8022/hospWeb/video.git
正克隆到 'video'...
The authenticity of host '[192.168.1.1]:8022 ([192.168.1.1]:8022)' can't be established.
ECDSA key fingerprint is SHA256:3ZXSjpz9luXRVT9coc+57HwVSmaSdi7pL4pvetgHEpk.
ECDSA key fingerprint is MD5:40:ca:ae:87:91:7b:f2:85:35:a3:7e:61:33:59:ce:95.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.1.1]:8022' (ECDSA) to the list of known hosts.
remote: Counting objects: 428, done.
remote: Compressing objects: 100% (371/371), done.
remote: Total 428 (delta 48), reused 428 (delta 48)
接收对象中: 100% (428/428), 951.64 KiB | 1.36 MiB/s, done.
处理 delta 中: 100% (48/48), done.
[root@public code]# ls
bridge video
查看项目提交历史和分支
搜索仓库,用户.组织
迁移外部仓库

填入需要克隆的地址和git用户名密码,即可

问题:

监听日志,提示没有权限
mkdir: can’t create directory ‘/data/git/‘: Permission denied
此时,可能是SELINUX阻止的原因

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
[root@localhost data]# docker logs --tail 0 -f 712c350adb28cc17b73f0a87548ab97e02f45d0b9bb56c6f6b495de258f01222

Saving key "/data/ssh/ssh_host_dsa_key" failed: No such file or directory
Saving key "/data/ssh/ssh_host_ecdsa_key" failed: No such file or directory
Saving key "/data/ssh/ssh_host_ed25519_key" failed: No such file or directory
chown: /data/ssh/*: No such file or directory
chmod: /data/ssh: No such file or directory
chmod: /data/ssh/*: No such file or directory
Could not load host key: /data/ssh/ssh_host_rsa_key
Could not load host key: /data/ssh/ssh_host_dsa_key
Could not load host key: /data/ssh/ssh_host_ecdsa_key
Could not load host key: /data/ssh/ssh_host_ed25519_key
sshd: no hostkeys available -- exiting.
mkdir: can't create directory '/data/git/': Permission denied
chmod: /data/git/.ssh: No such file or directory
./run: ./setup: line 11: can't create /data/git/.ssh/environment: nonexistent directory
chmod: /data/git/.ssh/environment: No such file or directory
chown: /data: Permission denied
chown: /data: Permission denied
chown: /data/git/: No such file or directory
chmod: /data: Permission denied
chmod: /data/gogs: No such file or directory
chmod: /data/git/: No such file or directory
2017/12/31 01:19:08 [ WARN] Custom config '/data/gogs/conf/app.ini' not found, ignore this if you're running first time
2017/12/31 01:19:08 [FATAL] [...g/setting/setting.go:501 NewContext()] Fail to create '/data/git/.ssh': mkdir /data/git: permission denied
Saving key "/data/ssh/ssh_host_rsa_key" failed: No such file or directory

关闭SELINUX即可解决

  • 临时关闭:setenforce 0
  • 永久关闭: vim /etc/selinux/config
1
2
3
4
5
6
7
8
9
10
11
12
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
#SELINUX=enforcing
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted