
本文共 6133 字,大约阅读时间需要 20 分钟。
1.概述
在写这篇博客之前,为了更好的描述整个流程,本人亲自尝试了:从搭建到使用,下面就围绕这个流程来阐述整个过程,目录结构如下所示:
- Git的安装搭建
- Git的常规操作
- Gitlab的搭建
- Gitlab的使用
下面我们开始整个流程。
2.Git篇
其实,作为开发人员,Git的使用是必备的技能,也许我们都很熟悉如何使用Git,对Git的常用命令也了如指掌;但是对Git服务器的搭建也许我们并不太在意,因为这些事情自从我们进入公司,运维人员就已经帮我们做好来,我们只需要知道如何去使用(拉去代码,提交代码)。但是如果有一天,你去来一家打算使用Git来作为代码管理器的公司,要求你来搭建这个代码管理平台,也许你会碰很多壁,下面给出我搭建的流程,也许能为您节省很多不必要的麻烦,保证您一次搭建成功。
2.1Git服务器搭建
环境准备:
- 系统环境:CentOS 6.x
- 软件版本:git-2.3.1.tar.gz
- 下载地址:https://www.kernel.org/pub/software/scm/git/
编译Git版本:
首先安装依赖(以下都是在root目录下完成):
yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel
tar -zxvf git-2.3.1.tar.gz
cd git-2.3.1
make prefix=/usr/local all
make prefix=/usr/local install #root用户运行
注:编译时如果操作系统没有安装gcc,使用yum –y install gcc*
另外,如果直接使用yum –y install git安装的版本比较旧(1.7.x),安装完成后输入git --version,会显示对应的版本号,若现实的版本号不对(之前操作系统存在git),可以先卸载之前的版本,然后做一个软链接,命令如下:
ln –s /usr/local/bin/git /usr/bin/
2.2创建Git用户
下面创建Git用户,命令如下所示:
useradd git –d /home/git
2.3在Git用户下安装SSH
在Git用户下安装SSH,命令如下所示:
ssh-keygen –t rsa
然后一路回车即可。
2.4安装Gitolite
这是一款Git服务管理工具,通过公钥对用户进行认证,并能够利用配置文件进行repo的精细授权管理。 输入以下命令:
git clone git://github.com/sitaramc/gitolitemkdir -p ~/bingitolite/install -to ~/bingitolite setup -pk ~/.ssh/id_rsa.pub
出现如下图所示,即表示成功。
2.5添加开发者
由于手上机器有限,我将权限配置和Git 仓库配置在同一台机器上,在前面我们添加了Git用户下的公钥,下面在Git账户下直接输入如下命令:
git clone git@git.company.com: gitolite-admin.git
注:这里的git.company.com在实际使用中可以换成公司域名或IP。
如果在执行命令时,需要输入密码,说明前面某些配置出错了,需要重新查证解决后在继续。
克隆完成后,会在gitolite-admin目录下出现两个子目录:conf和keydir。conf是gitolite的权限配置文件夹,keydir用于放置所有用户的公钥。所以,现在我们将guest.pub文件(开发者电脑生成的公钥)放入keydir目录中,然后编辑conf/gitolite.conf,在文件末尾添加新的repo和权限。下面我们添加一个repo为hdfs-etl,拥有读写权限。内容如下:
repo hdfs-etl RW+ = guest
注:R(读)W(写)
修改完成后,我们提交更改,完成用户及其库的添加,命令如下:
git add confgit add keydirgit commit –m “add repo diexun-etl,give access dengjie”git push
2.6操作repo
下面我在guest的主机上克隆hdfs-etl的工程,命令如下:
git clone git@git.company.com: hdfs-etl.git
如果在网络正常的情况下克隆失败,说明公钥添加未成功;若克隆成功,则会出现hdfs-etl的目录。当然我们也可以查看一下操作的权限,命令如下:
ssh git@git.company.com info
如下图所示:
如图所示,我机上上拥有读和写的权限,与配置的权限一致。
2.7卸载
任何地方使用ssh或git登录到git服务器需要输入密码,都说明配置git服务器出现错误,需要重新安装gitolite,重新安装前先清除之前的文件,清除命令如下所示:
ls -a | grep gitolite | xargs rm –frrm -fr ~/repositories ~/bin ~/projects.list ~/.ssh/authorized_keys
到这里Git篇就赘述完成了,至于Git 的常用操作,我想作为开发人员,应该是了熟于心,这里我就不多赘述了。
3.Gitlab篇
GitLab 是一个用于仓库管理系统的开源项目。使用Git作为代码管理工具,并在此基础上搭建起来的web服务,它集成了Github的可视化功能,方便开发人员协同工作,下面我就围绕Gitlab的搭建使用来展开描述。
3.1Gitlab搭建
环境准备:
操作系统:CentOS 6.x
Gitlab版本:7.8.1
由于Gitlab官方给出的安装过程难以成功(尝试过很多次,各种出错,环境只是一种工具,不应该花费太多的时间在这上面)。下面给出一个集成安装工具包BitNami,关于BitNami的简介,这里不做赘述,可自行谷歌了解。
bitnami-gitlab下载地址:https://downloads.bitnami.com/files/stacks/gitlab/7.8.1-0/bitnami-gitlab-7.8.1-0-linux-x64-installer.run
下载完成后,我们开始安装流程,命令如下:
chmod u+x bitnami-gitlab-7.8.1-0-linux-x64-installer.run
./bitnami-gitlab-7.8.1-0-linux-x64-installer.run
注:机器内存至少1G,不然跑不起来。
下面列出安装流程:
The installer detects that exists a 'git' user in the system. This installer will change the config
uration for this user. Do you want to continue the installation? [y/N]: yThe installer detects that exists a 'gitlab_ci' user in the system. This installer will change the
configuration for this user. Do you want to continue the installation? [y/N]: y----------------------------------------------------------------------------Welcome to the Bitnami Gitlab Stack Setup Wizard.----------------------------------------------------------------------------Select the components you want to install; clear the components you do not wantto install. Click Next when you are ready to continue.GitLab : Y (Cannot be edited)GitLab CI [Y/n] :YIs the selection above correct? [Y/n]: Y----------------------------------------------------------------------------Installation folderPlease, choose a folder to install Bitnami Gitlab StackSelect a folder [/opt/gitlab-7.8.1-0]:----------------------------------------------------------------------------Create Admin accountBitnami Gitlab Stack admin user creationEmail Address [user@example.com]: smartdengjie@gmail.comLogin [user]: saPassword :********Please confirm your password :********----------------------------------------------------------------------------Hostname that will be used to create internal URLs. If this value is incorrect,you may be unable to access your Gitlab installation from other computers. It isadvisable to use a Domain instead of an IP address for compatibility withdifferent browsers.Domain [10.211.55.30]:Do you want to configure mail support? [y/N]: y----------------------------------------------------------------------------Configure SMTP SettingsThis is required so your application can send notifications via email.Default email provider:[1] GMail[2] CustomPlease choose an option [1] : 1----------------------------------------------------------------------------Configure SMTP SettingsThis data is stored in the application configuration files and may be visible toothers. For this reason, it is recommended that you do not use your personalaccount credentials.Username []: saPassword :Re-enter :----------------------------------------------------------------------------Setup is now ready to begin installing Bitnami Gitlab Stack on your computer.Do you want to continue? [Y/n]: y----------------------------------------------------------------------------Please wait while Setup installs Bitnami Gitlab Stack on your computer. Installing 0% ______________ 50% ______________ 100% #########################################
到这里,Gitlab就安装完成了。
3.2启动服务
命令如下:
/opt/gitlab-7.1.1-0/ctlscript.sh start #启动/opt/gitlab-7.1.1-0/ctlscript.sh restart #重启/opt/gitlab-7.1.1-0/ctlscript.sh stop #停止
4.Gitlab使用
下面我们登录到Gitlab的可视化界面,截图如下:
其实首次登录的时候,如果没有在配置文件中配置,是会有注册功能的,这里为了安全考虑,我以管理员的身份登录,然后在设置界面去掉了注册功能。
下面演示Gitlab使用流程:
- 创建项目:
其中需要注意: 如果属于Group级的项目,也就是属于多人配合的项目,建议Namespace使用Group name,另外Visibility默认为Internal。我们先创建一个组,如图所示:
这里我们添加组所属用户,如图所示:
- 源仓库的构建
我们接下来按照步骤来创建一个自己的项目:
按照以上提示的步骤操作,我们即可以创建或者导入一个项目成功。
到这里,Gitlab的搭建和项目创建就介绍完成了。
5.总结
后面,等手上的资料整理完成后,我会在写一篇Gitlab的Pull Request工作流的介绍,这篇文章就分享到这里;如果有什么问题,可以加群讨论或发送邮件给我,我会尽我所能为您解答,与君共勉!
发表评论
最新留言
关于作者
