03·私有远程仓库Gitlab

Git远程仓库衍生品

1.Github(国外最大的代码仓库)
2.Gitee(码云 阿里)
3.Coding(扣钉 腾讯)
4.Codehub(华为)
5.GitLab(开源的私有远程仓库)

gitee码云远程仓库使用

file

## 全局设置
git config --global user.name "rowey"
git config --global user.email "1627556442@qq.com"

## 推送本地仓库代码到远程仓库
# 将远程仓库的地址保存到本地git仓库的配置文件中
[root@gitlab web]# git remote add origin https://gitee.com/rowey/linux2.git
remote:远端,远程
add:添加
origin:远程仓库的名字(可自定义)
远程仓库的地址
[root@gitlab web]# git remote add lw git@gitee.com:rowey/lw.git

# 将本地仓库代码推送到远程仓库,将代码推送到名字为'origin'的远程仓库的master分支
[root@gitlab web]# git push -u origin "ceo_branch"
-u:指定远程仓库的名字 "master"

# 查看一个本地仓库添加了几个远程仓库
[root@gitlab web]# git remote -v
lw  git@gitee.com:rowey/lw.git (fetch)
lw  git@gitee.com:rowey/lw.git (push)
origin  https://gitee.com/rowey/linux2.git (fetch)
origin  https://gitee.com/rowey/linux2.git (push)

创建密钥对

file

# 1.创建秘钥对
[root@gitlab web]# ssh-keygen 

# 2.查看公钥
[root@gitlab web]# cat ~/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC0ISYnOciJDR9c7PXYnxleCArE/YjubjU4DS+zcBlEcIEuchlbufQ0uQKDyxFY5hADkw7lToJIIo1sS62z1mvgl4ObB+PB6J3XNEt/DnqUo+Xm61UlTMBdOgBRi3eFvm6sJWxdHQS9ci3Cvx8L2+Mj/At+90AbhBV3NqYh5SIdqPoAvIbvnuWvmLLyWoHjM+2l4Ok3+plYIfEAHBio8rB3R7wFeCX2LOl/yGUj3fdQce7ewY2AcRC60PWSTfDhicTRNnkB47QGocKOsNTKnp7D4Hqrhy3dHOlUd6akwwaZalwPdq03UQ3eVmtp41EUtui7ufv8V0miwqItOg+SS0LZ root@gitlab

file

# 推送到指定分支
[root@gitlab web]# git push -u lw ceo_branch
# 推送所有分支
[root@gitlab web]# git push -u lw --all
# 推送标签
[root@gitlab web]# git push -u lw --tag

## 每次修改代码之后,要做的操作
# 1.将新写的代码加入暂存区
[root@gitlab web]# git add .
# 2.提交代码
[root@gitlab web]# git commit -m 'v111'
# 3.打标签(可打 可不打)
# 4.推送代码到远程仓库
[root@gitlab web]# git push      //默认推送代码到上一次推送的仓库
[root@gitlab web]# git push -u origin master   // 指定远程仓库推送

Gitlab介绍

1.私有代码仓库 ,除了gitlab以外 还有gogs
2.精细化的权限配置
3.控制用户/用户组权限,避免任何用户都可以将代码提交到master

Gitlab的架构

file

Gitlab-ce的部署

## 使用清华源,直接安装
[root@gitlab ~]# yum install -y https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-12.3.5-ce.0.el7.x86_64.rpm

[root@gitlab ~]# yum localinstall -y gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm 

2578327957@qq.com
## 安装过程
Please configure a URL for your GitLab instance by setting `external_url`
configuration in /etc/gitlab/gitlab.rb file.
Then, you can start your GitLab instance by running the following command:
  sudo gitlab-ctl reconfigure

For a comprehensive list of configuration options please see the Omnibus GitLab readme
https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md

# 1.修改配置文件
[root@gitlab ~]# vim /etc/gitlab/gitlab.rb
external_url 'http://gitlab.lw.com'

## 添加邮箱相关配置
gitlab_rails['gitlab_email_enabled'] = true
gitlab_rails['gitlab_email_from'] = '1627556442@qq.com'
gitlab_rails['gitlab_email_display_name'] = 'lw gitlab notice'

gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "1627556442@qq.com"
gitlab_rails['smtp_password'] = "授权码"
gitlab_rails['smtp_domain'] = "qq.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true

prometheus['enable'] = false
prometheus['monitor_kubernetes'] = false
prometheus_monitoring['enable'] = false
node_exporter['enable'] = false
redis_exporter['enable'] = false
postgres_exporter['enable'] = false
gitlab_monitor['enable'] = false
###### 新版本关闭 ######
grafana['enable'] = false
alertmanager['enable'] = false

# 2.重新加载配置文件
[root@gitlab ~]# gitlab-ctl reconfigure

Chef Client finished, 299/430 resources updated in 02 minutes 28 seconds
gitlab Reconfigured!

Gitlab操作

# 查看当前服务状态
[root@gitlab ~]# gitlab-ctl status
run: gitaly: (pid 25900) 656s; run: log: (pid 25744) 696s
run: gitlab-workhorse: (pid 25889) 656s; run: log: (pid 25700) 714s
run: logrotate: (pid 25729) 702s; run: log: (pid 25728) 702s
run: nginx: (pid 25713) 708s; run: log: (pid 25712) 708s
run: postgresql: (pid 25475) 778s; run: log: (pid 25474) 778s
run: redis: (pid 25415) 789s; run: log: (pid 25414) 789s
run: sidekiq: (pid 25684) 720s; run: log: (pid 25683) 720s
run: unicorn: (pid 25646) 726s; run: log: (pid 25645) 726s

# 停止所有服务
[root@gitlab ~]# gitlab-ctl stop

# 启动所有服务
[root@gitlab ~]# gitlab-ctl start

# 重启所有服务
[root@gitlab ~]# gitlab-ctl restart

# 重启指定服务
[root@gitlab ~]# gitlab-ctl restart nginx

# 停止指定服务
[root@gitlab ~]# gitlab-ctl stop nginx

# 启动指定服务
[root@gitlab ~]# gitlab-ctl start nginx

# 查看gitlab所有组件日志
[root@gitlab ~]# gitlab-ctl tail

# 查看gitlab指定组件日志
[root@gitlab ~]# gitlab-ctl tail nginx

# 连接gitlab终端
[root@gitlab ~]# gitlab-rails console
## 测试发邮件
irb(main):001:0> Notify.test_email('1627556442@qq.com','gitlab test','测试').deliver_now

# 忘记root密码
[root@gitlab ~]# gitlab-rails console
## 登录root用户
irb(main):011:0> user = User.where(id: 1).first
irb(main):012:0> user.password='123456789'
irb(main):012:0> user.password_confirmation='123456789'
irb(main):012:0> user.save

file

file

Gitlab汉化

汉化网站:TP

file

file

# 1.下载汉化包
[root@gitlab ~]# wget https://gitlab.com/xhang/gitlab/-/archive/10-2-stable-zh/gitlab-10-2-stable-zh.tar.gz

# 2.解压汉化包
[root@gitlab ~]# tar xf gitlab-10-2-stable-zh.tar.gz

# 3.停止所有服务
[root@gitlab ~]# gitlab-ctl stop

# 4.覆盖
[root@gitlab ~]# \cp -a gitlab-10-2-stable-zh/* /opt/gitlab/embedded/service/gitlab-rails/

# 5.重新加载配置文件
[root@gitlab ~]# gitlab-ctl reconfigure

# 6.启动所有服务
[root@gitlab ~]# gitlab-ctl start

file

创建项目

file

file

# 命令行指令

# Git 全局设置
git config --global user.name "Administrator"
git config --global user.email "admin@example.com"

# 创建新版本库
git clone http://gitlab.lw.com/root/linux2.git
cd linux2
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

# 已存在的文件夹
cd existing_folder
git init
git remote add origin http://gitlab.lw.com/root/linux2.git
git add .
git commit -m "Initial commit"
git push -u origin master

# 已存在的 Git 版本库
cd existing_repo
git remote rename origin old-origin
git remote add origin http://gitlab.lw.com/root/linux2.git
git push -u origin --all
git push -u origin --tags

file

命令行指令

Git 全局设置
git config --global user.name "Administrator"
git config --global user.email "admin@example.com"

创建新版本库
git clone git@gitlab.lw.com:root/linux2.git
cd linux2
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

已存在的文件夹
cd existing_folder
git init
git remote add origin git@gitlab.lw.com:root/linux2.git
git add .
git commit -m "Initial commit"
git push -u origin master

已存在的 Git 版本库
cd existing_repo
git remote rename origin old-origin
git remote add origin git@gitlab.lw.com:root/linux2.git
git push -u origin --all
git push -u origin --tags

添加git远程仓库

# 1.添加
[root@gitlab web]# git remote add lw git@gitlab.lw.com:root/linux2.git

# 2.添加hosts解析
[root@gitlab web]# vim /etc/hosts
10.0.0.91 gitlab.lw.com

# 3.查看公钥
[root@gitlab web]# cat ~/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC0ISYnOciJDR9c7PXYnxleCArE/YjubjU4DS+zcBlEcIEuchlbufQ0uQKDyxFY5hADkw7lToJIIo1sS62z1mvgl4ObB+PB6J3XNEt/DnqUo+Xm61UlTMBdOgBRi3eFvm6sJWxdHQS9ci3Cvx8L2+Mj/At+90AbhBV3NqYh5SIdqPoAvIbvnuWvmLLyWoHjM+2l4Ok3+plYIfEAHBio8rB3R7wFeCX2LOl/yGUj3fdQce7ewY2AcRC60PWSTfDhicTRNnkB47QGocKOsNTKnp7D4Hqrhy3dHOlUd6akwwaZalwPdq03UQ3eVmtp41EUtui7ufv8V0miwqItOg+SS0LZ root@gitlab

网页添加ssh秘钥

file

file

file

file

Gitlab创建用户和用户组

file

file

file

file

file

下载git仓库中的代码

# 克隆代码
[root@gitlab opt]# git clone git@gitlab.lw.com:ops/new_linux2.git

# 同步仓库代码跟gitlab中保持一致
[root@gitlab new_linux2]# git pull

# 拉所有分支代码
 [root@gitlab new_linux2]# git fetch -vp

山林不向四季起誓 荣枯随缘