项目开发环境搭建手记(3.GitLab搭建)

http://blog.csdn.net/fbysss/article/details/44085961

 

一、git安装

Centos下面,就是这么任性:

yum install git

中间有2步需要输入y确认一下。

其他操作系统方法:http://git-scm.com/download/linux

安装完成后,

git --version

git version 1.7.1

验证成功。

 

二、.gitlab安装

GitLab是很接近Github的,而且免费,所以选择它作为项目代码仓库。地址:https://about.gitlab.com/downloads/

 选择操作系统之后,会有一步步的提示。当然,有些已经有了的,就不用再安装了。可以用whereis检查一下

 curl-O https://downloads-packages.s3.amazonaws.com/centos-6.6/gitlab-7.6.2_omnibus.5.3.0.ci-1.el6.x86_64.rpm

 (curl的-O指的是把url这里的东西直接输出到文件中,文件名默认就是url中的文件名。)

  

sudo rpm -igitlab-7.6.2_omnibus.5.3.0.ci-1.el6.x86_64.rpm

sudo gitlab-ctl reconfigure

sudo lokkit -s http -s ssh

这句是用来做防火墙的。避免用户通过ssh方式和http来访问

按照步骤做了之后,访问http://localhost就可以进入登录界面了。

默认账号:

Username: root
Password: 5iveL!fe

 

进去之后就要设置新的密码,随便写一个sss54321

 

进来之后,可以create a new project

然后会发现,硬盘上已经生成了一个git文件: /var/opt/gitlab/git-data/repositories/root/sssApp.git

也看到了url http://localhost.localdomain:9999/root/sssApp.git

此时,在客户机中,也可以clone一下试试:

git clone http://192.168.0.12:9999/root/sssApp.git 

正常。warning: You appear to have cloned an empty repository.很显然,啥也没有,只是一个空仓库。

/opt/gitlab/embedded/etc/gitconfig

 

[sss@localhost ~]$sudo git config --list
fatal: error processing config file(s)

touch ~/.gitconfig

OK。

 

——自己装过一个git,gitlab也内嵌一个。但两者显然没有关联。安装好了居然没有配置文件?啥情况。

实际上也无所谓,只不过提示信息太吓唬人了。

rm ~/.gitconfig

git config--global color.diff auto

然后再看,文件已经自动生成了。

三、创建代码仓库:

su root

mkdir /repo/sssApp

cd /repo/sssApp

git init

git remote add originhttp://localhost.localdomain:9999/root/sssApp.git

git push -u origin master

 

error: The requested URL returned error: 401 Unauthorized while accessing http://localhost.localdomain:9999/root/sssApp.git/info/refs

 

git config--list发现 remote.origin.url=http://localhost.localdomain:9999/root/sssApp.git

git config -e可以进行修改

 

网上很多帖子说,升级版本之后就OK,或者换SSH方式,在我看来都是不知其所以然。

其实错误提示的已经很清楚了,就是需要认证。

这啦唯一需要大家注意的,就是这里的用户名,应该是gitlab的用户名,与linux主机无关。

正确的做法:

 remote.origin.url=http://localhost.localdomain:9999/root/sssApp.git

修改为 remote.origin.url=http://root:[email protected]:9999/root/sssApp.git

再试就OK了。

但是问题又来了,这样把密码明文写在这里,显然不是太好。

重新来一遍,新建一个目录和库

mkdir  /repo/justtest

cd  /repo/justtest

git init 

git remote add origin http://[email protected]:9999/root/justtest.git

git push -u origin master

 

(gnome-ssh-askpass:5430):Gtk-WARNING **: cannot opendisplay: 

看来是显示的问题。于是直接到Centos下,进入终端执行

这下弹出一个图形化窗口,要我输入密码。

但输入之后提示

error: src refspec master does not match any.
error: failed to push some refsto'http://[email protected]:9999/root/justtest.git'

 

查询之后,发现两点:

1.Gtk这个错误,用unset SSH_ASKPASS这个命令即可。

要一劳永逸解决:

echo 'unsetSSH_ASKPASS' >> ~/.bashrc && source~/.bashrc

这样的话,就会在命令提示符提问password,secureCRT也能搞定了。

2.第二个错误,是因为这个库什么都没有(刚才成功的步骤,是有创建文件步骤的,所以没注意)

解决:

touch test.txt  vi随便编辑点内容

git add test.txt

git commit test.txt

git push -u origin master

OK了。

 

收不到email,看看这个说明。

https://gitlab.com/gitlab-org/omnibus-gitlab/blob/629def0a7a26e7c2326566f0758d4a27857b52a3/doc/settings/smtp.md

 

 

 ! [rejected]       master ->master (non-fast-forward)
error: failed to push some refs to 'http://[email protected]/app-dev/sssApp.git'

原因是我之前改过project名称,加了一层父目录。已经提交到远程仓库中了。再次提交的话,就有冲突。

解决办法:加一个-f参数,强制推送。

文章http://blog.csdn.net/chain2012/article/details/7476493有较好的说明。

四、gitlab管理

创建一些用户,即开发人员

每一个用户,都会在 /var/opt/gitlab/git-data/repositories/下面建立一个同名目录

创建一个用户组,命名为app-dev,创建之后,发现在硬盘上有对应的目录生成

/var/opt/gitlab/git-data/repositories/app-dev

然后把刚才创建的用户加进来。作为developer

 

那么,问题来了。如何让用户组和这个project关联起来呢?找了半天没找到,因为group那里只有一个new project,不能选择。

不知道new一个存在的project是否ok

不过从project的左下方,有一个Transfer project,把namespace选成这个group,就ok了。

注意,修改namespace之后,发现git仓库的地址也改了:

http://localhost.localdomain:9999/app-dev/sssApp.git 而不是root了。

创建之后,在客户机里面,输入(假设服务器地址为192.168.0.12)

git clone http://192.168.0.12:9999/app-dev/sssApp.git

Cloning into'sssApp'...
Username for 'http://192.168.0.12:9999': newusername

Password for'http://[email protected]:9999':xxxxxx

OK。

但是问题来了。有的用户可以,但有的用户就不行

fatal:Authentication failed for'http://192.168.0.12:9999/app-dev/sssApp.git/'

原因有2.

1.密码没有设置,不能为空,需要在gitlab控制台里面设置一个。

2.密码至少8位以上。注意看提示哦

OK,这下已经可以了。

也可以用 git clone http://[email protected]:9999/app-dev/sssApp.git

的方式访问,直接输入密码

 

 

用普通用户在客户机上修改内容,然后git push,出现如下错误:

remote: GitLab: You don't have permission
To http://dev.fbysss.com/app-dev/sssApp.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to'http://dev.fbysss.com/app-dev/sssApp.git'

原因是这个流程还没理解。

如果用root用户肯定是可以的。

但是developoer用户,需要在本地建立分支。然后提交。然后由root来合并

 

后来的做法是:由root在gitlab上面给每个master建立一个branch-dev的分支,给大家提交开发。这个是有权限的。只是需要在push的时候要指定分支。

git push origin branch-dev

 

五、SSH方式配置:

OK,http方式是很简单,但是每次都很麻烦,要输入用户名和密码。

简单的方式,是使用ssh方式。

参考文章:http://blog.csdn.net/black_ox/article/details/17753943

1.客户端使用ssh-keygen -t rsa,会在用户目录下生成两个文件:id_rsa和id_rsa.pub

2.进入gitlab界面,profile setting,SSH Keys,内容里面填入id_rsa.pub的内容,标题随便填写。保存即可。

3.把原来的http方式,换成git@host的方式就可以了。

git [email protected]:app-dev/sssApp.git

非常轻松的搞定。

 

OK,原理是什么呢?

注意看git@xxxx,说明有一个git用户。

cat /etc/passwd|grep git

git:x:496:491::/var/opt/gitlab:/bin/sh

说明这个用户的home目录不是/home/git,而是/var/opt/gitlab

ll /var/opt/gitlab/.ssh

看到一个 authorized_keys文件,打开一看,自己的名字已经在里面了。

也就是说,这个服务器对外提供一个git用户,作为ssh信任接口。其他开发人员的公钥都在git用户的authorized_keys中记录。

 

在服务器上配置本地的ssh访问时,出现以下问题:

[root@localhosttemp]# git clone [email protected]:app-dev/sssApp.git

Initialized empty Git repository in /root/temp/sssApp/.git/
The authenticity ofhost '192.168.0.12 (127.0.0.1)' can't be established.

 

其实这只是一个警告

yes之后, root的.ssh/known_host文件中自动添加了主机信息,下次就直接进来了。

 

六、安装nginx

http://nginx.org/en/docs/install.html

http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm

yum install nginx

 

[sss@localhost ~]$whereis nginx
nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx

OK了。

[sss@localhost ~]$/usr/sbin/nginx -v
nginx version: nginx/1.6.2

 

安装依赖包:

1

yum install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

 

等等。

 

参考文章:http://www.nowamagic.net/academy/detail/1330235

 

 ok,启动不起来,为啥呢?原来被gitlab端口占用了。

 

 

另外,

vi/var/opt/gitlab/gitlab-shell/config.yml

/var/opt/gitlab/gitlab-rails/etc/unicorn.rb里面使用了8080,要注意。但是,修改这里也是没用的。文件里面已经写得很清楚。要修改/etc/gitlab/gitlab.rb

 

 

自己安装了一个nginx,发现gitlab安装的时候内嵌了一个。

/var/opt/gitlab/nginx/conf/gitlab-http.conf看到有端口号配置 ,这个文件是被nginx.conf文件所include的。

这里决定了gitlab对外提供服务的端口号。

在gitlab.rb里面的配置要注意:

1.一定要有端口号。如下:external_url'http://192.168.0.12:9999'

2.使用原始地址,问题在于,对外提供的git url是地址,比较难看。

3.外面再来一个nginx,配置一个域名,转到192.168.0.12:9999

4.如果外面做了转向,把这个url代理到dev.fbysss.com,就不能使用http://dev.fbysss.com,而是要原始地址。

结果:在gitlab控制面板里面看到的,还是ip+端口,但是已经可以在外面使用域名替代了。

 

 修改80为9999之后,我想不用重启就生效,怎么办呢?

 

 ps aux |grep nginx就知道了

[root@localhostconf]# ps aux |grep nginx
root      1138  0.0 0.0  3940   308 ?       Ss  20:19   0:00 runsv nginx
root      1150  0.0  0.0  4084  332 ?       S   20:19   0:00 svlogd -tt /var/log/gitlab/nginx
root      1152  0.0 0.0 26936   724?       Ss   20:19  0:00 nginx: master process/opt/gitlab/embedded/sbin/nginx -p/var/opt/gitlab/nginx
495       1180  0.0 0.2 31216  3004 ?       S   20:19   0:00 nginx:workerprocess                                  
495       1181  0.0 0.0 30948   884?       S   20:19   0:01 nginx:workerprocess                                  
495       1182  0.0 0.0 30948   832?       S   20:19   0:01 nginx:workerprocess                                  
495       1183  0.0 0.0 30948   788?       S   20:19   0:01 nginx: worker process    

 

/opt/gitlab/embedded/sbin/nginx-t 检查一下:

nginx: [alert] could not open error log file: open()"/opt/gitlab/embedded/logs/error.log"failed (2: No such file ordirectory)

 

mkdir /opt/gitlab/embedded/logs/

就好了。但是

/opt/gitlab/embedded/sbin/nginx-s reload  (注意打全路径是为了与刚安装的nginx区分)

出现错误:

nginx: [error]invalid PID number ""in"/opt/gitlab/embedded/logs/nginx.pid"

自己想出来的解决办法:直接编辑这个文件,输入1152,也就是正在运行的nginx主进程id。

ok了!

现在http://localhost:9999,已经可以了。

 

/var/opt/gitlab/gitlab-rails/etc/gitlab.yml
/var/opt/gitlab/gitlab-rails/etc/resque.yml
/var/opt/gitlab/gitlab-rails/etc/database.yml
/var/opt/gitlab/gitlab-shell/config.yml

 

/opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml

[sss@localhost ~]$ll/opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml
lrwxrwxrwx. 1 root root 43 Jan  5 23:14/opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml-> /var/opt/gitlab/gitlab-rails/etc/gitla

原来这个gitlab.yml和上一个是同一个,不过是一个链接罢了。

 

可以 看到,这种安装方法,与其他的都不一样,很多东西都已经打包安装了。放在/opt/gitlab/embedded下面。

sudo vi/var/opt/gitlab/gitlab-rails/etc/gitlab.yml

可以看到第一行提示:真贴心啊

# This file is managed by gitlab-ctl. Manual changes will be
# erased! To change the contents below, edit /etc/gitlab/gitlab.rb
# and run `sudo gitlab-ctl reconfigure`.

 

这意思就是说啊,手工修改这个配置文件是无效的,要修改就修改gitlab.rb,然后运行sudo gitlab-ctl reconfigure

可以看到,运行之后,连/var/opt/gitlab/nginx/conf/gitlab-http.conf都重新覆盖了。原来的9999又变成了80

 

果然,实际上只需要修改一句话:

external_url 'http://localhost.localdomain'修改成external_url'http://localhost.localdomain:9999

 

 

七、问题解决记录:搞定fatal: The remote end hung up unexpectedly


问题:在远程pc访问服务器时没有问题,在服务器本机反倒有问题了:

[sss@localhost tmp]$ git ls-remote git@localhost:app-dev/sssApp.git 
fatal: 'app-dev/sssApp.git' does not appear to be a git repository
fatal: The remote end hung up unexpectedly

 

http方式也没问题。

key也都加上了。

 

后来,没有办法,如此解决:

参考:http://serverfault.com/questions/523002/local-ssh-connection-to-gitlab-not-working

http://blog.chinaunix.net/uid-26701523-id-3383244.html

 

git ls-remote git@localhost:app-dev/sssApp.git  改成

git ls-remote git@localhost:git-data/repositories/app-dev/sssApp.git  ——这是相对git用户的home目录/var/opt/gitlab,写全了也可以。

 

而这就是一种绝对路径的方式。

注意这些key的关系:

实际上,只要主机之间建立了ssh信任关系(只要客户机的pubkey已经添加到服务器对应的用户目录文件中,至于登录linux的用户名是什么,与gitlab用户名是否有关联,是否有权限,都并无关系)

但是,奇怪的是,root用户又没有这个问题。

注意,如果我们把gitlab中的root用户名修改为root2,再来测试,则会报错,只不过报错提示不一样。

[sss@localhost tmp]$ sudo git ls-remotegit@localhost:root/justtest.git   
Access denied.
fatal: The remote end hung up unexpectedly

其他参考:

http://stackoverflow.com/questions/16912542/git-does-not-appear-to-be-a-git-repository-ip-and-port-instead-of-domain

  

终于,功夫不负有心人!问题得以解决!

原因出在cat /var/opt/gitlab/.ssh/authorized_keys 里面。也正是通过查找一些资料,激发灵感来看一看这个文件,从而找到了线索。

以下贴出authorized_keys 部分内容:

我们发现,有两行来处理[email protected]

其中一个有command,一个则没有。

分析:没有command的这一行,显然有些奇怪。似乎就是我开始自己手工用cat /home/sss/.ssh/ida_rsa.pub>>追加进去的。删掉是不是就可以了呢?

备份authorized_keys 文件

然后删除没有command的哪一行。

 

ssh-rsaAAAAB3NzaC1yc2EAAAABIwAAAQEAo7JeBNBQaSGBmq5cqxKRgYiG6/aUS7waFUbcBQ8Fwpz2+LfV2shK/zH1evAQPHq+AyuuaH5ZSnbrhT1YnX5/7u2GO1rHf24G3dljxitOY9Dvsy3hXDS067LZ356jNlPCMlzAPTLe9owKLY72HKDwYsA4E7SRDGjZGaKNQmCmRTglpQXlFqwMMzvJ6SeEAKZg7xkr+KyUKypdVMXsSsegNOHuNkUOfo+j8hFYCnySS7XXH6YUNNYrTnOAT7t9NtLpuF7XdjzqdNz0h8I8V4yMV33OFRZAWWAAtpSwuLeOOMtUasZOg6fk2TL96JmtGR4oOb0qZsD6A7F/[email protected]
command="/opt/gitlab/embedded/service/gitlab-shell/bin/gitlab-shellkey-16",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-ptyssh-rsaAAAAB3NzaC1yc2EAAAABIwAAAQEA3Rrnrou8vC31RPj/1M9uGT5Cp7FDcY9TIF+7JZFhsbOxwB7CV2r2QrLcQpHw/ZyhKWB3FM9eUY3JhJEL8rVcZwoFAsQBVI2oPUisGkUMxWO822RBy2zukR3kjqFWbIrVHqr2tY00roI6Qj187fEjs6FfoMqYxn5/y+oI05n91r8pmPP3TI3OmxqtoJYuNxkZbOrR5xR9IQ7C9Fw1uJjh9ngKTvc9mCiqo67TIjIRFK71eikwIrmkail+wDfs0B478EW6aSHDdElUMYLW6yLKloRAGTEHrnSlEsEZpVIJh1bwe0gi/gkxsbBNU+zc8eIxTiCOind1EYH+/[email protected]
command="/opt/gitlab/embedded/service/gitlab-shell/bin/gitlab-shellkey-18",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-ptyssh-rsaAAAAB3NzaC1yc2EAAAABIwAAAQEAo7JeBNBQaSGBmq5cqxKRgYiG6/aUS7waFUbcBQ8Fwpz2+LfV2shK/zH1evAQPHq+AyuuaH5ZSnbrhT1YnX5/7u2GO1rHf24G3dljxitOY9Dvsy3hXDS067LZ356jNlPCMlzAPTLe9owKLY72HKDwYsA4E7SRDGjZGaKNQmCmRTglpQXlFqwMMzvJ6SeEAKZg7xkr+KyUKypdVMXsSsegNOHuNkUOfo+j8hFYCnySS7XXH6YUNNYrTnOAT7t9NtLpuF7XdjzqdNz0h8I8V4yMV33OFRZAWWAAtpSwuLeOOMtUasZOg6fk2TL96JmtGR4oOb0qZsD6A7F/[email protected]

 

结果,再次运行git ls-remote git@localhost:app-dev/sssApp.git

发现提示有了变化!

Access denied.
fatal: The remote end hung up unexpectedly

这不就是类似root开始的提示——root如何好的?可能就是在gitlab中增加了key。

再看路径,这个路径本来就没有给sss用户赋予权限。

我们用一个别的仓库,授权给sss,然后再来,OK!

 

ok,我们顺藤摸瓜,搞清楚gitlab的机制。/opt/gitlab/embedded/service/gitlab-shell/bin/gitlab-shellkey-18是什么呢?

原来,gitlab中,公钥的规则是这样的:

1.私钥只能有一份,但是可以挂在任何用户之下。

2.私钥就对应command中的key-n,运行这句命令,可以看到命令行提示

Welcome to GitLab, <username>!

sss这个linux用户的公钥挂在哪个gitlab用户下,用git访问 git@localhost:app-dev/sssApp.git 就会对应check gitlab的相应用户的权限,并进行路由。

 

——此问题google了N遍,有人说使用gitlab-rake

 

也在这里找到了官方的说明:

 

https://github.com/gitlabhq/gitlabhq/blob/master/doc/raketasks/maintenance.md

 

Rebuildauthorized_keys file

In some case itis necessary to rebuild the authorized_keys file.

ForOmnibus-packages:

sudogitlab-rake gitlab:shell:setup

Forinstallations from source:

cd/home/git/gitlab

sudo -ugit -H bundle exec rake gitlab:shell:setup RAILS_ENV=production

This willrebuild an authorized_keys file.

You willlose any data stored in authorized_keys file.

Do youwant to continue (yes/no)? yes

 

............................

那么,我们也来试试吧:

备份正确的auth文件,然后把原来有问题的文件拷贝过来覆盖。

再运行git命令,有问题。

[root@localhostgitlab]# gitlab-rake gitlab:shell:setup
This will rebuild an authorized_keys file.
You will lose any data stored in authorized_keys file.
Do you want to continue (yes/no)? yes

 

再看authorized_keys ,已经正确了。显然是根据gitlab数据库的信息去更新的。

再运行 git ls-remotegit@localhost:app-dev/sssApp.git,一切OK!

 

[sss@localhosttmp]$ git ls-remote git@localhost:sss/justtest.git
a502e8cc45e8f7de05905d94faf5c59ce57c2b10       HEAD
a502e8cc45e8f7de05905d94faf5c59ce57c2b10       refs/heads/master

 

 

 

======================

问题:发现我的工程总不能顺利的从git更新,每次都需要输入git用户的密码。

 

排查:在eclipse设置里面搜索ssh,看到SSH2 Home设置有问题。设置的ssh目录没有前面的一点。原因未知。

 

解决:将目录更改正确即可。

 

——中间还尝试过,使用key management,可以生成DSA和RSA,但和自己生成的的内容不同。loadfile的时候,出现提示 failed to load given file.因为有上面的解决办法,所以这个问题暂时不予深究。

 

 

 

 

遗留问题:

ok,对于团队开发,用一个ip地址也确实太low了。至少用一个内部域名吧。

http://localhost.localdomain修改为http://dev.fbysss.com

然后 sudo gitlab-ctl reconfigure

这一次作为服务一直启动,停在那里了。其实已经好了。不管他——后来报错了,是内存不足。

客户端配置hosts文件,测试,OK。

试想一下,gitlab应该是做了什么手脚,让webserver能够解析这个域名。

查找nginx.conf,没有发现改动。不过从

/var/log/gitlab/nginx/gitlab_access.log

可以看到,访问过来的url已经是带域名的了。

——待弄清,到底在哪里设置了。

你可能感兴趣的:(项目开发环境搭建手记(3.GitLab搭建))