Gitlab之一---企业级gitlab部署及使用

实战一:企业级Gitlab部署及配置

Gitlab服务的安装文档:https://about.gitlab.com/install/  

环境要求:https://docs.gitlab.com/ee/install/requirements.html

国外安装包下载地址:https://packages.gitlab.com/gitlab/gitlab-ce

rpm包国内下载地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/

已经下载了两种不同后缀的gitlab安装包,注意:不要下载最新版本的,最新版本可能存在bug问题,11.11.5,中的5就是修改5次的版本。

gitlab-ce-11.11.5-ce.0.el7.x86_64.rpm #此次安装此gitlab包
gitlab-ce_11.11.5-ce.0_amd64.ded

1、安装gitlab的虚拟机配置要求:内存4G,处理器双核。

Gitlab之一---企业级gitlab部署及使用

 2、此次试验安装rpm后缀的包,将ded结尾的安装包放到/usr/local/src/目录下:

[root@gitlab src]# pwd
/usr/local/src
[root@gitlab src]# ls
gitlab-ce-11.11.5-ce.0.el7.x86_64.rpm 

 3、开始安装gitlab

[root@gitlab src]# yum install gitlab-ce-11.11.5-ce.0.el7.x86_64.rpm  -y

4、以下是安装过程自动删除的目录:

/etc/gitlab   #配置文件目录
/run/gitlab   # 运行pid 目录
/opt/gitlab     #安装目录
/var/opt/gitlab  # 数据目录
/var/log/gitlab   # 日志 目录

5、配置gitlab文件:/etc/gitlab/gitlab.rb

external_url 'http://192.168.7.100' # 需要访问的IP地址,或者解析的域名也可以。

gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "974212253qq.com" # 设置开发的邮箱地址,防止忘记密码需要修改密码
gitlab_rails['smtp_password'] = "xxx" # 授权码
gitlab_rails['smtp_domain'] = "qq.com"
gitlab_rails['smtp_authentication'] = :login
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_from'] = "974212253@qq.com"
user["git_user_email"] = "974212253@qq.com"  # 配置公司邮箱

Gitlab之一---企业级gitlab部署及使用

6、启动gitlab服务,启动需要2分钟左右,启动后查看80端口是否已经被监听。

[root@gitlab local]# gitlab-ctl reconfigure 

Gitlab之一---企业级gitlab部署及使用

 7、访问gitlab网站,输入需要访问的IP地址:192.168.7.100,此时进入页面就会让你修改当前的密码,默认登陆用户是root,到此gitlab的安装完成。

Gitlab之一---企业级gitlab部署及使用

 实战二:Gitlab创建项目、克隆文件、上传代码

1、创建group组

1、使用管理员root创建组,一个组里面可以有多个项目分支,可以将开发添加到组里面进行设置权限不同的组就是公司不同的开发项目或者服务模块,

不同的组添加不同的开发即可实现对开发设置权限的管理。

Gitlab之一---企业级gitlab部署及使用

2、创建group组名linux_gitlab,并设置为私有信息

Gitlab之一---企业级gitlab部署及使用

 2、创建用户名

1、创建用户

Gitlab之一---企业级gitlab部署及使用

 2、创建一个user1用户名。

Gitlab之一---企业级gitlab部署及使用

3、添加用户名密码 

1、由于新建的user1用户没有密码,需要我们去创建,第一种创建是选择Edit修改,设置user1密码。Gitlab之一---企业级gitlab部署及使用Gitlab之一---企业级gitlab部署及使用

 2、第二种方法修改gitlab密码,输入创建的user1用户,点击忘记密码,然后跳转到需要输入的邮箱地址之后,就会发送邮件对其当前账号进行修改密码。

Gitlab之一---企业级gitlab部署及使用

 4、将创建的user1用户关联到创建的组内linux_gitlab

1、点击创建好的linux_gitlab组

Gitlab之一---企业级gitlab部署及使用

 2、点击add users to group,将创建的用户添加到组内

Gitlab之一---企业级gitlab部署及使用

5、选择linux_gitlab组之后,创建新的项目。 

1、登陆创建的user1用户账号,然后选择之前创建的linux_gitlab组,创建项目。Gitlab之一---企业级gitlab部署及使用

 2、选择linux_gitlab组之后创建新的项目。

Gitlab之一---企业级gitlab部署及使用

 3、创建一个web1项目

Gitlab之一---企业级gitlab部署及使用

4、创建一个测试内容

Gitlab之一---企业级gitlab部署及使用

5、创建文件内容,并注明文件信息。

Gitlab之一---企业级gitlab部署及使用

 6、此时就可以看到提交后文件的内容

Gitlab之一---企业级gitlab部署及使用

7、复制web1项目要克隆的URL地址。

Gitlab之一---企业级gitlab部署及使用

 6、克隆前面创建的项目地址。

1、安装git包,并在另一台主机上进行克隆测试。

# yum install git  -y

2、使用git命令开始克隆web1项目的数据

[root@jenkins ~]# git clone http://192.168.7.100/linux_gitlab/web1.git
Cloning into 'web1'...
Username for 'http://192.168.7.100': root  # 输入管理员账号和密码。
Password for 'http://root@192.168.7.100': 
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.

3、在web1目录下可以看到克隆后的文件内容

[root@jenkins ~]# cd web1/  # 切换到web1目录下
[root@jenkins web1]# ls
index.html
[root@jenkins web1]# cat index.html   # 查看文件信息
linux web1 V1[root@jenkins web1]# 

7、将linux系统上的文件传到gitlab网站上

1、在web1项目里的index.html文件添加内容,并将其信息传递到gitlab网站上。

[root@jenkins web1]# cd web1/ # 切换到克隆的项目目录下。
[root@jenkins web1]# cat index.html 
linux web1 V1
index V222  # 在index.html文件中添加内容。

[root@jenkins web1]# git add index.html      # 添加index.html文件到缓存中
[root@jenkins web1]# git commit -m "v2"        # 提交index.html文件。
[root@jenkins web1]# git push  # 将index.html文件传到gitlab网页上。
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Username for 'http://192.168.7.100': root  # 输入gitlab账号
Password for 'http://root@192.168.7.100': 
Counting objects: 5, done.
Writing objects: 100% (3/3), 254 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://192.168.7.100/linux_gitlab/web1.git
   5e5c4e8..93667aa  master -> master

2、在gitlab网站上查看上传上去的结果。

Gitlab之一---企业级gitlab部署及使用

 三、Gitlab基础使用

1、gitlab常用命令

1、以下是不常用命令。

gitlab-rails     # 用于启动控制台进行特殊操作,比如修改管理员密码、打开数据库控制台(gitlab-rails dbconsole)等。 # 不常用

gitlab-psql      # 数据库命令行

2、gitlab-ctl用法:

# gitlab-ctl stop   #停止gitlab
# gitlab-ctl restart  #重启gitlab
# gitlab-ctl status  # 查看gitlab状态
# gitlab-ctl tail nginx  # 查看nginx组件的日志
# gitlab-ctl  reconfigure  # 修改完配置文件,以及第一次启动gitlab,都会使用此命令。

2、s添加SSH-Keys公钥,在jenkins主机上实现非密码登陆克隆项目文件

1、修改jenkins主机的hostname,并生成公钥对。

[root@jenkins ~]# hostnamectl set-hostname jenkins  # 将第二个主机的hostname改为jenkins
[root@jenkins ~]# ssh-keygen  # 然后生成公钥对。

2、查看/root/.ssh目录下的id_rsa.pub的公钥对内容。

[root@jenkins ~]# cd /root/.ssh
[root@jenkins .ssh]# ls
id_rsa  id_rsa.pub  known_hosts
[root@jenkins .ssh]# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDKD7eISTB6VcDpF4h6oN8zusJ7GJMGd0GXZsjoQr3knjURN6PvUMmzP2UkwjRAryPS5e9LkQOHHy82CwNSFPV0Ow1npOAF8V3CyvOBcRhXhRZO4/PWP1lb6cqs5BeYg0gEQFIGpEt7kdSa9n5+6cEuzuzHfwFIXIHYFOrHOZeI6JPIzJR5Ww4u7N59MMCH3EKhFRF7QH92QcAEjvDYv0ncz1ymG5TkK72d2SniSwFTF4nOJWL6nGfTtnCQzlrMDR8GKfrbdkE6weoV6KHz21vGzHCUE/eM2aiIgFUvPbFbURWzsixzyq12mksejp1HnMD9T/VgHHT+nbMiUAVERv45 root@jenkins

3、在gitlab网站上,将jenkins主机的公钥对复制到SSH-Keys对应的位置。Gitlab之一---企业级gitlab部署及使用

4、在web1项目上复制SSH的地址链接。

Gitlab之一---企业级gitlab部署及使用

 5、在jenkins主机上通过SSH地址进行克隆web1项目的文件,此时就是免秘钥登录,克隆web1项目的文件,如果有web1目录,就无法克隆。

[root@jenkins ~]# git clone git@192.168.7.100:linux_gitlab/web1.git 
[root@jenkins ~]# ls
anaconda-ks.cfg  web1 # 切换到项目目录下
[root@jenkins ~]# cd web1/
[root@jenkins web1]# ls
index.html
[root@jenkins web1]# cat index.html   #查看项目下默认的文件名内容
linux web1 V1
index V222

3、git命令使用

1、git命令用法

git config --global user.name name  # 设置全局用户名
git config --global user.email xxx@xx.com # 设置全局邮箱
git config --global --list # 列出用户全局设置
git add index.html      # 添加指定文件、目录或当前目录下所有数据到暂存区
git commit m "11" # 提交文件到工作区
git stat us   #查看工作区的状态
git push   # 提交代码到服务器
git pull   # 获取代码到本地
git log   # 查看操作日志
vim .gitignore   # 定义忽略文件
git reset --hard HEAD^^ # git版本回滚,HEAD为当前版本,加一个为上一个,为上上一个版本
git reflog # 获取每次提交的ID,可以使用 hard 根据提交的ID进行版本回退
git reset --hard 5ae4b06 # 回退到指定id的版本
git branch # 查看当前所处的分支
git checkout -b develop # 创建develop,并切换到一个新分支
git checkout develop  # 切换到develop分支

示例一:回滚命令用法:

[root@jenkins web1]# git reset --hard HEAD^  # 将部署的代码回滚到上一版本,用法最多
HEAD is now at 5e5c4e8 Add index.html V1 version
[root@jenkins web1]# cat index.html 
linux web1 V1[root@jenkins web1]#

[root@jenkins web1]# git reflog
93667aa HEAD@{0}: reset: moving to 93667aa
5e5c4e8 HEAD@{1}: reset: moving to HEAD^
93667aa HEAD@{2}: clone: from git@192.168.7.100:linux_gitlab/web1.git
[root@jenkins web1]# cat index.html # 查看文件内容
linux web1 V1
index V222
index V23
[root@jenkins web1]# git reflog  # 查看ID号
93667aa HEAD@{0}: reset: moving to 93667aa
5e5c4e8 HEAD@{1}: reset: moving to HEAD^
93667aa HEAD@{2}: clone: from git@192.168.7.100:linux_gitlab/web1.git
[root@jenkins web1]# git reset --hard 93667aa   # 指定ID号,回滚到上一个版本
HEAD is now at 93667aa v2
[root@jenkins web1]# cat index.html 
linux web1 V1
index V222

示例二:创建branch分支(领导层才会使用)

1、创建分支

Gitlab之一---企业级gitlab部署及使用

2、分支命名

Gitlab之一---企业级gitlab部署及使用

 3、在linux主机上进行克隆,并切换到分支内,查看当前所处的分支

[root@jenkins ~]# git clone git@192.168.7.100:linux_gitlab/web1.git  # 克隆web1项目的文件
Cloning into 'web1'...
remote: Enumerating objects: 13, done.
remote: Counting objects: 100% (13/13), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 13 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (13/13), done.
[root@jenkins ~]# cd web1/  # 切换到web1目录下
[root@jenkins web1]# git checkout develop  # 切换到develop分支内
Branch develop set up to track remote branch develop from origin.
Switched to a new branch 'develop'
[root@jenkins web1]# git branch  # 查看此时所处的分支名称
* develop
  master