Ubuntu20.04 搭建repo + gitlab的代码管理系统
1、为什么要用gitlab?
GitLab 是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的Web服务。安装方法是参考GitLab在GitHub上的Wiki页面。Gitlab是目前被广泛使用的基于git的开源代码管理平台, 基于Ruby on Rails构建, 主要针对软件开发过程中产生的代码和文档进行管理, Gitlab主要针对group和project两个维度进行代码和文档管理, 其中group是群组, project是工程项目, 一个group可以管理多个project, 可以理解为一个群组中有多项软件开发任务, 而一个project中可能包含多个branch, 意为每个项目中有多个分支, 分支间相互独立, 不同分支可以进行归并。
2、为什么要用repo?
repo命令行工具,是一个python脚本,对git部分命令进行封装,简化了对多个git版本库的管理,通过配置manifests配置文件,可以将很多个git仓库有效的进行组织管理。最开始是用在Android源码的管理上,目前被广泛使用。
3、ubuntu 20.04 安装gitlab应用
1、安装相关依赖
sudo apt install ca-certificates curl openssh-server postfix
postfix是一种电子邮件服务器,安装时弹出选择框,选择默认即可。
2、下载gitlab安装包,并安装。
1、执行完命令,当前目录得到 gitlab-ce14.2.0-ce.0_amd64.deb 安装包。
wget -c https://mirrors.tuna.tsinghua.edu.cn/gitlad-ce/ubuntu/pool/focal/main/g/gitlab-ce/gitlab-ce14.2.0-ce.0_amd64.deb
2、输入命令安装
sudo dpkg -I gitlab-ce_14.2.0-ce.0_amd64.deb
如下图所示即安装成功
3、配置gitlab
1、修改gitlab.文件
sudo vi /etc/gitlab/gitlab.rb
找到external_url关键字,修改web登陆的ip以及端口。
ubuntu 本机地址为192.168.47.97,所以我这里改为
external_url ‘http://192.168.47.97:3001’ ,保存退出。
2、输入命令重新配置gitlab,重启gitlab
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
等待一段时间后,打开浏览器,输入http://http://192.168.47.97:3001/ 即可显示以下画面。
如不能显示,输入下面命令。
sudo ufw allow 3001 #允许外部IP访问本地定义的端口。
3、出现登陆页面后,我们要修改下root用户的密码。
输入以下命令:
1、sudo gitlab-rails console
2、u=User.where(id:1).first
3、u.password=12345678 # 密码设置为12345678
4、u.password_confirmation=12345678 # 确认密码
5、u.save! # 保存
4、此时应该可以用 root 跟 12345678 进行登陆了。
4、gitlab后台服务器相关操作
1、新建分组
点击左上menu->your group -> New Group
我们新建一个Max的分组,
然后点击 New subGroup 新建一个 dev的分组
建好分组 如下图所示
2、新建工程
1、点击进入dev分组页面,然后点击New Project按钮。选择create blank project。
输入project demo勾选 Initialize repository with a README ,点击create project 即可创建工程。
如图所示。
3、本地下载代码 并提交代码
1、从远端下载代码到本地。
ubuntu下输入命令:
git clone ssh://[email protected]/max/dev/demo.git
max/dev即我们上面建的分组,demo.git即我们上面新建的工程,但此时需要密码验证。
2、对于ssh连接, 我们可以生成ssh key,并提交到gitlab的服务器,这样我们就不需要每次输入密码。
ubuntu下执行以下命令:
git config --global user.name "Max" # Max 为你的名字
git config --global user.email "[email protected]" # [email protected] 为你的邮箱
ssh-keygen -t rsa -C "[email protected]" # 生成ssh key
生成的key存放在 ~/.ssh文件下
storage@storage-PowerEdge-R740xd:/demo$ ls -l ~/.ssh/demo$ cat ~/.ssh/id_rsa.pub
total 8
-rw------- 1 storage storage 2602 7月 26 10:11 id_rsa
-rw-r–r-- 1 storage storage 570 7月 26 10:11 id_rsa.pub
复制id_rsa.pub中的内容
storage@storage-PowerEdge-R740xd:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC8qo6pJyhyQEnzlpBlOwdNUD+y/Xh8CpNkF433OKBsNNdiS6/igZWsyogzUInB1Tig3dcPSjq2XDayDPveUzXGWmsbnB0F+Jqwtzi9DIR4OVA26hDWx1JC2U0CwZdpQuuhhB8RfAchGA+vqDfY+A8upLJ5k2BNAZOQFUzYjP/mQPjpPMta5uUGYdHjzSlKgHGgkV1R19rwuW6QpVRwyvY794Nt9g51CLGZbGj5SfcCx4NM7NIeQst1ByLEXO3xxKnLSmojvtIujz7jJJq1diCfoWdvQHDND5nbcV1jqyHzL3y74ekYG+DKCsxJLPKdt9Mo1iWkq8g7RxnJ1wNBKH7t4rNxFXIav2A+tjY8UdOP8Acc1Akzuy1QVE4cQgLrJ10Y4flUL2zO7IpWisk6p1G6xA/lhw6whQlTKflDYzeI/6KkZozrlSuYimE2EY7i2FRv+hnNajo2ANiSVGw4RFJpejhkl4QXu+JbvKGbypXY5yV3/7AMQWniC/R1AfG6ylc= 738270732@qq.com
gitlab 登陆后,点击右上角的图像->Preferences->SSH Keys 将复制内容添加到文本框,点击add key即可。
如图即添加成功
3、此时我们重新clone代码试下
storage@storage-PowerEdge-R740xd:/demo$ git clone ssh://git@192.168.47.97/max/dev/demo.git/demo$
Cloning into ‘demo’…
The authenticity of host ‘192.168.47.97 (192.168.47.97)’ can’t be established.
ED25519 key fingerprint is SHA256:T1nA+6BrjyMOfAPJpTCuLF7xNpY/vcqWSpLcqdauUpw.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added ‘192.168.47.97’ (ED25519) to the list of known hosts.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
storage@storage-PowerEdge-R740xd:
storage@storage-PowerEdge-R740xd:/demo$ ls/demo$
demo
storage@storage-PowerEdge-R740xd:
是不是没输入密码也能下载了?
4、提交代码
storage@storage-PowerEdge-R740xd:/demo/demo$ touch hello.txt/demo/demo$
storage@storage-PowerEdge-R740xd:
storage@storage-PowerEdge-R740xd:/demo/demo$/demo/demo$ git add hello.txt
storage@storage-PowerEdge-R740xd:
storage@storage-PowerEdge-R740xd:/demo/demo$ git commit -m “add hello text”/demo/demo$ git push origin HEAD:refs/heads/main
[main 329e2d0] add hello text
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 hello.txt
storage@storage-PowerEdge-R740xd:
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 40 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 270 bytes | 270.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To ssh://192.168.47.97/max/dev/demo.git
f191303…329e2d0 HEAD -> main
storage@storage-PowerEdge-R740xd:~/demo/demo$
看看后台是不是已经提交上了
5、repo管理代码仓
1、客户端 安装repo 命令
git clone https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/
storage@storage-PowerEdge-R740xd:/demo$ git clone https://mirrors.tuna.tsinghua.edu.cn/git/git-repo//demo$ ls
Cloning into ‘git-repo’…
remote: Enumerating objects: 7438, done.
remote: Counting objects: 100% (7438/7438), done.
remote: Compressing objects: 100% (3978/3978), done.
remote: Total 7438 (delta 4804), reused 5617 (delta 3386)
Receiving objects: 100% (7438/7438), 3.30 MiB | 1.14 MiB/s, done.
Resolving deltas: 100% (4804/4804), done.
storage@storage-PowerEdge-R740xd:
demo git-repo
storage@storage-PowerEdge-R740xd:~/demo$
红圈里的这个文件就是我们需要的。
在用户根目录下,新建一个bin文件夹,将这个repo文件拷贝进去。
storage@storage-PowerEdge-R740xd:~$ cd
storage@storage-PowerEdge-R740xd:~$ mkdir bin
storage@storage-PowerEdge-R740xd:~$ cp demo/git-repo/repo ./bin/
storage@storage-PowerEdge-R740xd:~$ ls -l bin/
total 48
-rwxrwxr-x 1 storage storage 45277 7月 26 11:41 repo
2、gitlab服务器端配置
1、登陆http://http://192.168.47.97:3001/后,选择dev用户组,新建一个manifest工程。
再新建一个文件,名字命名为default.xml,如图所示。
default.xml内容如下
<?xml version="1.0" encoding="UTF-8"?>
<manifest>
<remote name="Max" fetch="ssh://[email protected]"/>
<default remote="Max" revision="main" sync-j="4" />
<project name="Max/dev/kernel" path="kernel" remote="Max"/>
<project name="Max/dev/framework" path="framework" remote="Max"/>
<project name="Max/dev/device" path="device" remote="Max"/>
解析如下:
remote :远端服务器的名字,name=“Max” 可随便起名,后面提交代码会用到。fetch 为服务区地址
default : 默认服务器配置,remote有多个的时候,这里来指定用哪一个。revision 为分支名字,这里新建的工程在gitlab后台看到的仅有一个分支为main,那这里就写main。
project :name 为Max/dev分组下的工程名称,path为repo下载代码的路径名称。
我们在这里定义了三个project,所以我们要在Max/dev分组下新建三个工程。名字为kernel framework device。
建好如图所示
3、下载代码
1、使用如下命令init仓库,前提要配置好python环境。
repo init -u ssh://[email protected]/Max/dev/manifest.git --repo-url=https://gerrit-googlesource.proxy.ustclug.org/git-repo
storage@storage-PowerEdge-R740xd:~/demo$repo init -u ssh://git@192.168.47.97/Max/dev/manifest.git --repo-url=https://gerrit-googlesource.proxy.ustclug.org/git-repo
Downloading Repo source from https://gerrit-googlesource.proxy.ustclug.org/git-repo
remote: Finding sources: 100% (13/13)
remote: Total 13 (delta 1), reused 10 (delta 1)
repo: Updating release signing keys to keyset ver 2.3
Your identity is: Max 738270732@qq.com
If you want to change this, please re-run ‘repo init’ with --config-name
Testing colorized output (for ‘repo diff’, ‘repo status’):
black red green yellow blue magenta cyan white
bold dim ul reverse
Enable color display in this user account (y/N)? y
repo has been initialized in /home/storage/demo/
If this is not the directory in which you want to initialize repo, please run:
rm -r /home/storage/demo//.repo
and try again.
storage@storage-PowerEdge-R740xd:/demo$/demo$
storage@storage-PowerEdge-R740xd:
storage@storage-PowerEdge-R740xd:/demo$/demo$ cat .repo/manifest
storage@storage-PowerEdge-R740xd:
manifests/ manifests.git/ manifest.xml
2、使用repo sync 同步代码
repo sync -j4
storage@storage-PowerEdge-R740xd:/demo$ repo sync/demo$ ls
Fetching: 100% (3/3), done in 1.608s
Garbage collecting: 100% (3/3), done in 0.013s
repo sync has finished successfully.
storage@storage-PowerEdge-R740xd:
device framework kernel
storage@storage-PowerEdge-R740xd:~/demo$
此时,demo目录下是不是已经有了我们在gitlab后台建立的三个工程了呢。
4、提交代码
1、提交代码到服务器端
storage@storage-PowerEdge-R740xd:/demo/kernel$ touch change.log/demo/kernel$ git add change.log
storage@storage-PowerEdge-R740xd:
storage@storage-PowerEdge-R740xd:/demo/kernel$ git commit -m “add chanle.log”/demo/kernel$ git push Max HEAD:main
[detached HEAD 6788658] add chanle.log
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 change.log
storage@storage-PowerEdge-R740xd:
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 40 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 273 bytes | 273.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To ssh://192.168.47.97/Max/dev/kernel
3186640…6788658 HEAD -> main
storage@storage-PowerEdge-R740xd:~/demo/kernel$
我们新建了一个change.log文件,使用如下命令推到服务器端。
touch change.log
git add change.log
git commit -m "add chanle.log"
git push Max HEAD:main。 #Max就是我们在default.xml定义的remode name。main就是default定义的revision
看看gitlab后台是不是又这个文件呢?
5、新建分支
storage@storage-PowerEdge-R740xd:/demo$ repo forall -c pwd/demo$ repo forall -c git checkout -b new_branch
/home/storage/demo/device
/home/storage/demo/framework
/home/storage/demo/kernel
storage@storage-PowerEdge-R740xd:
storage@storage-PowerEdge-R740xd:/demo$ cd kernel//demo/kernel$ git branch -a
storage@storage-PowerEdge-R740xd:
- new_branch remotes/Max/main remotes/m/main -> Max/main storage@storage-PowerEdge-R740xd:~/demo/kernel$ repo forall -c git push Max HEAD:new_branch
去后台看看是不是多了一个branch呢
如果想要下载new_branch的代码,那么manifest也得添加个新分支,这里我们直接在gitlab后台操作吧
然后修改max_new_branch中default.xml
我在后台kernel device framework 工程的new_branch分支各增加个一个文件,我们看如何拉new_branch分支的代码。
1、repo init
repo init -u ssh://[email protected]/Max/dev/manifest.git -b max_new_branch --repo-url=https://gerrit-googlesource.proxy.ustclug.org/git-repo
2 、repo sync
repo sync
storage@storage-PowerEdge-R740xd:~/branch$repo init -u ssh://git@192.168.47.97/Max/dev/manifest.git -b max_new_branch --repo-url=https://gerrit-googlesource.proxy.ustclug.org/git-repo
Downloading Repo source from https://gerrit-googlesource.proxy.ustclug.org/git-repo
remote: Finding sources: 100% (13/13)
remote: Total 13 (delta 1), reused 10 (delta 1)
Your identity is: Max 738270732@qq.com
If you want to change this, please re-run ‘repo init’ with --config-name
repo has been initialized in /home/storage/branch/
If this is not the directory in which you want to initialize repo, please run:
rm -r /home/storage/branch//.repo
and try again.
storage@storage-PowerEdge-R740xd:/branch$ repo sync/branch$ ls -l kernel/
Fetching: 100% (3/3), done in 1.406s
Garbage collecting: 100% (3/3), done in 0.015s
repo sync has finished successfully.
storage@storage-PowerEdge-R740xd:
total 8
-rw-rw-r-- 1 storage storage 0 7月 26 12:40 change.log
-rw-rw-r-- 1 storage storage 1 7月 26 12:40 new_branch.txt
-rw-rw-r-- 1 storage storage 10 7月 26 12:40 README.md
storage@storage-PowerEdge-R740xd:/branch$ ls -l device//branch$ ls -l framework/
total 8
-rw-rw-r-- 1 storage storage 1 7月 26 12:40 new_branch.txt
-rw-rw-r-- 1 storage storage 10 7月 26 12:40 README.md
storage@storage-PowerEdge-R740xd:
total 8
-rw-rw-r-- 1 storage storage 1 7月 26 12:40 new_branch.txt
-rw-rw-r-- 1 storage storage 13 7月 26 12:40 README.md
storage@storage-PowerEdge-R740xd:~/branch$
这样是不是拉取了一份分支代码呢!
如果要提交代码命令如下
git push Max HEAD:new_branch
6、tag使用
版权归原作者 夏侯城临 所有, 如有侵权,请联系我们删除。