代码托管 ---- GitLab

代码托管 ---- GitLab

GitLab

https://docs.gitlab.com/omnibus/manual_install.html
https://packages.gitlab.com/gitlab/gitlab-ce
若下载速度慢,可以下面的地址
https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/

获取安装包

wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-11.9.12-ce.0.el7.x86_64.rpm --no-check-certificate -c

安装gitlab

注意:安装完默认数据在/OPT目录下,一般不建议修改

yum install audit-libs-python checkpolicy libcgroup libsemanage-python policycoreutils-python setools-libs audit audit-libs policycoreutils
yum install gitlab-ce-11.9.12-ce.0.el7.x86_64.rpm

配置gitlab

grep "^[a-Z]" /etc/gitlab/gitlab.rb
external_url 'http://192.168.10.11'	#可配置域名或IP
gitlab_rails['smtp_enable'] = true #邮件发送设置
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 25
gitlab_rails['smtp_user_name'] = "final233@qq.com"	#邮件配置,用于忘记密码修改的
gitlab_rails['smtp_password'] = "password"	
gitlab_rails['smtp_domain'] = "qq.com"
gitlab_rails['smtp_authentication'] = :login
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = false
gitlab_rails['gitlab_email_from'] = "final233@qq.com"
user["git_user_email"] = "final233@qq.com"

初始化gitlab

初始化有一定的时间

gitlab-ctl reconfigure	修改完配置文件执行此操作
Starting Chef Client, version 13.6.4
resolving cookbooks for run list: ["gitlab"]
Synchronizing Cookbooks:
  - gitlab (0.0.1)
  - registry (0.1.0)
  - gitaly (0.1.0)
  - redis (0.1.0)
  - nginx (0.1.0)
  - consul (0.1.0)
  - package (0.1.0)
  - postgresql (0.1.0)
  - runit (4.3.0)
  - letsencrypt (0.1.0)
  - mattermost (0.1.0)
  - crond (0.1.0)
  - acme (3.1.0)
  - compat_resource (12.19.1)
Installing Cookbook Gems:
Compiling Cookbooks...
Recipe: gitlab::default
  * directory[/etc/gitlab] action create
    - change mode from '0755' to '0775'
...省略
Recipe: gitlab::prometheus
  * execute[reload prometheus] action run
    - execute /opt/gitlab/bin/gitlab-ctl hup prometheus
Recipe: <Dynamically Defined Resource>
  * service[alertmanager] action restart
    - restart service service[alertmanager]
  * service[postgres-exporter] action restart
    - restart service service[postgres-exporter]

Running handlers:
Running handlers complete
Chef Client finished, 476/1268 resources updated in 02 minutes 00 seconds
gitlab Reconfigured!

查看gitlab状态

gitlab-ctl status
run: alertmanager: (pid 22379) 620s; run: log: (pid 22159) 638s
run: gitaly: (pid 22259) 623s; run: log: (pid 21291) 721s
run: gitlab-monitor: (pid 22307) 623s; run: log: (pid 21967) 651s
run: gitlab-workhorse: (pid 22279) 623s; run: log: (pid 21732) 676s
run: logrotate: (pid 21784) 667s; run: log: (pid 21813) 664s
run: nginx: (pid 21751) 673s; run: log: (pid 21767) 670s
run: node-exporter: (pid 22292) 623s; run: log: (pid 21916) 657s
run: postgres-exporter: (pid 22475) 620s; run: log: (pid 22204) 631s
run: postgresql: (pid 21376) 717s; run: log: (pid 21474) 714s
run: prometheus: (pid 22342) 622s; run: log: (pid 22139) 640s
run: redis: (pid 21158) 726s; run: log: (pid 21179) 725s
run: redis-exporter: (pid 22329) 622s; run: log: (pid 22007) 645s
run: sidekiq: (pid 21668) 685s; run: log: (pid 21686) 682s
run: unicorn: (pid 21612) 691s; run: log: (pid 21643) 688s

启停命令

gitlab-ctl start
gitlab-ctl stop
gitlab-ctl restart

登录gitlab页面

注意如打开以后是 502 的界面,可使用gitlab-ctl status查看下,检查 80 和 8080 端口是否被占用,内存是否满载
首次登录,设置root密码,密码策略最少8位

这个注册会关闭,稍后设置


管理用户名默认为root,密码则是刚设置的


关闭注册功能

注意:Sign-in restrictions此选项是关闭登录,修改相关复杂,需要进入到数据库中再打开此功能项


验证下,此时没有注册功能了


修改管理员账号邮箱



收到邮箱 后点击确认即可

创建账户


Email用于账户重置密码使用
Access level Regular为普通用户,Admin为管理员

设置账户密码

方法一:管理员设置




方法二:通过刚创建的账户设置的邮箱重设密码




创建组

不同业务划分不同的组


将用户添加到组中

权限Owner所有权限,Developer仅开发


创建项目



在此项目中创建一个版本web文件



查看项目连接地址



有两种方式,SSH和HTTP的方式

客户端使用HTTP方式

yum install git

git clone http://192.168.10.11/final233/web1.git    
Cloning into 'web1'...
Username for 'http://192.168.10.11': user1
Password for 'http://user1@192.168.10.11': 
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.

cat web1/index.html 
This is Final233 Page ... V1

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 "V1" #提交文件到工作区
git status	#查看工作区状态
git push	#提交(上传)代码到服务器
git pull	#获取(下载)代码到本地
git log	#查看操作日志
vim .gitignore	#定义忽略文件
git reset --hard HEAD^^ #git版本回滚,HEAD为当前版本,加一个^为上一个,^^为上上一个版本。
git reflog	##获取每次提交的ID,可以使用-hard根据提交的ID进行版本回退
git reset --hard cb778a8 #回退到指定id的版本
#git branch	#查看当前所处的分支
#git checkout -b develop	#创建并切换到一个新分支
#git checkout develop #切换分支

示例

相当于每台机器自报家门,global表示全局,当前也可以对某个仓库进行设置

[root@git web1]# git config --global user.name "user1"
[root@git web1]# git config --global user.email "43880792@qq.com"
[root@git web1]# git config --global --list
user.name=user1
user.email=43880792@qq.com

git config -l

修改代码并提交代码上传至gitlab服务器

修改本地某个文件
[root@git web1]# cat index.html 
This is Final233 Page ... V1
This is Final233 Page ... V2
[root@git web1]# git add ./*	#将当前目录数据存入暂存区
[root@git web1]# git commit -m "V2"	#提交文件到工作区
[master fef0b1e] V2
 1 file changed, 2 insertions(+), 1 deletion(-)
[root@git web1]# git push	#上传到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.10.11': user1
Password for 'http://user1@192.168.10.11': 
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://192.168.10.11/final233/web1.git
   b9aef02..fef0b1e  master -> master	#当前仅有一个master分支,后面会创建其他分支

查看gitlab页面


查看操作日志

git log
commit fef0b1e019f8205e3ed63daa6013d7beb453804a
Author: user1 <43880792@qq.com>
Date:   Thu Nov 4 14:56:47 2021 +0800

    V2	#当前V2版本

commit b9aef026f59ae835efe27981d72e830a708f9354
Author: Administrator <final233@qq.com>
Date:   Thu Nov 4 13:47:57 2021 +0800

    By Final233

多提交几个版本,便于后期做回滚操作测试此处省略

查看版本

前面的ID,是回滚的参数
[root@git web1]# git reflog
b780d8a HEAD@{0}: commit: V5
820570a HEAD@{1}: commit: V4
6c20435 HEAD@{2}: commit: V3
fef0b1e HEAD@{3}: commit: V2
b9aef02 HEAD@{4}: clone: from http://192.168.10.11/final233/web1.git

回滚上一个版本

当前版本文件内容
[root@git web1]# cat index.html 
This is Final233 Page ... V1
This is Final233 Page ... V2
This is Final233 Page ... V3
This is Final233 Page ... V4
This is Final233 Page ... V5

[root@git web1]# git reset --hard HEAD^  
HEAD is now at 820570a V4
[root@git web1]# cat index.html 
This is Final233 Page ... V1
This is Final233 Page ... V2
This is Final233 Page ... V3
This is Final233 Page ... V4

回滚上上个版本

[root@git web1]# git reset --hard HEAD^^
HEAD is now at fef0b1e V2
[root@git web1]# cat index.html 
This is Final233 Page ... V1
This is Final233 Page ... V2

回滚到指定的版本

[root@git web1]# git reflog
fef0b1e HEAD@{0}: reset: moving to HEAD^^
820570a HEAD@{1}: reset: moving to HEAD^
b780d8a HEAD@{2}: commit: V5
820570a HEAD@{3}: commit: V4
6c20435 HEAD@{4}: commit: V3
fef0b1e HEAD@{5}: commit: V2
b9aef02 HEAD@{6}: clone: from http://192.168.10.11/final233/web1.git
[root@git web1]# git reset --hard b9aef02
HEAD is now at b9aef02 By Final233 
[root@git web1]# cat index.html 
This is Final233 Page ... V1

更新至最新版本

[root@git web1]# git pull
Username for 'http://192.168.10.11': user1
Password for 'http://user1@192.168.10.11': 
Updating b9aef02..b780d8a
Fast-forward
 index.html | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
[root@git web1]# cat index.html 
This is Final233 Page ... V1
This is Final233 Page ... V2
This is Final233 Page ... V3
This is Final233 Page ... V4

查看当前分支

[root@git web1]# git branch
* master

gitlab 门户也可以查看历史分支


创建分支



切换分支

[root@git web1]# git checkout -b develop
Switched to a new branch 'develop'

[root@git web1]# git branch
* develop
  master
  
拉取develop最新代码
[root@git web1]# git clone -b develop http://192.168.10.11/final233/web1.git
Cloning into 'web1'...
Username for 'http://192.168.10.11': user1
Password for 'http://user1@192.168.10.11': 
remote: Enumerating objects: 15, done.
remote: Counting objects: 100% (15/15), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 15 (delta 3), reused 0 (delta 0)
Unpacking objects: 100% (15/15), done.

修改代码并上传代码至develop分支
[root@git web1]# git add ./*
[root@git web1]# git commit -m "V6"
[develop 43e196c] V6
 2 files changed, 2 insertions(+)
 create mode 160000 web1
[root@git web1]# git push
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.10.11': user1
Password for 'http://user1@192.168.10.11': 
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 292 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: 
remote: To create a merge request for develop, visit:
remote:   http://192.168.10.11/final233/web1/merge_requests/new?merge_request%5Bsource_branch%5D=develop
remote: 
To http://192.168.10.11/final233/web1.git
   b780d8a..43e196c  develop -> develop

验证

develop分支


master分支


posted @ 2019-07-17 16:06  Final233  阅读(353)  评论(0)    收藏  举报