一.简介

GitLab 概述: 是一个利用 Ruby on Rails 开发的开源应用程序,可以使你开发、部署、维护 web 应用程序变得简单的框架。用于搭建私有的代码托管平台。

二.安装配置

0.gitlab社区版rpm包 下载地址。官方 安装与使用文档地址。安装配置要求,内存大于4G。

1.rpm包安装   默认安装地址/opt/gitlab

#安装依赖
yum -y install curl policycoreutils openssh-server openssh-clients postfix
#默认postfix发送邮件
systemctl enable postfix
systemctl start postfix
#关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
#上传rpm包到任意目录
rz gitlab-ce-15.9.3-ce.0.el7.x86_64.rpm
#安装
yum localinstall -y gitlab-ce-15.9.3-ce.0.el7.x86_64.rpm
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

2.配置

#配置域名
egrep -v '^$|#' /etc/gitlab/gitlab.rb
vim /etc/gitlab/gitlab.rb
改
32 external_url 'http://gitlab.example.com'
为
32 external_url 'http://192.168.77.147'
#重新加载配置文件,过程大约5分钟
gitlab-ctl reconfigure
#提示初始用户root和口令位置
#######################################
Username: root
Password: You didn't opt-in to print initial root password to STDOUT.
Password stored to /etc/gitlab/initial_root_password. 
########################################
#服务启动
gitlab-ctl start
#服务启动/停止/重启/状态查看
gitlab-ctl start/stop/restart/status
#日志查看
gitlab-ctl tail
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

3.登录 http://192.168.77.147

默认管理员用户root 默认口令在/etc/gitlab/initial_root_password,登录后修改,有效期24小时。

#口令忘记可重置口令
gitlab-rake "gitlab:password:reset"
#按提示输入用户名、新的口令
  • 1.
  • 2.
  • 3.

代码发布之git与gitlab_代码上线

中文语言更改

代码发布之git与gitlab_gitlab_02

登录密码修改  初始密码24小时有效期

代码发布之git与gitlab_代码上线_03

停用新的注册,取消勾选 已启用注册功能  保存更改

代码发布之git与gitlab_代码上线_04

代码发布之git与gitlab_代码上线_05

切换到仪表盘  可以从这里创建项目、用户和组

代码发布之git与gitlab_代码上线_06

三.项目创建 

0.用户和组并关联

创建组 dev为例

代码发布之git与gitlab_gitlab_07

创建用户 alibaby为例

代码发布之git与gitlab_代码上线_08

管理员root编辑用户  修改alibaby用户密码

代码发布之git与gitlab_代码上线_09

用户和组关联

仪表盘---群组---选dev群组---管理权限---邀请成员---填写成员信息---邀请

代码发布之git与gitlab_gitlab_10

1.新建项目

代码发布之git与gitlab_gitlab_11

2.添加SSH秘钥

这里为alibaby用户配置秘钥,alibaby用户登录gitlab

偏好设置---SSH秘钥

#使用ssh-keygen 默认3个回车
ssh-keygen 
#复制公钥内容
cat ~/.ssh/id_rsa.pub
  • 1.
  • 2.
  • 3.
  • 4.

代码发布之git与gitlab_gitlab_12

3.命令行指引

Git 全局设置

git config --global user.name "alibaby"
git config --global user.email "@qq.com"
  • 1.
  • 2.

创建一个新仓库

git clone git@192.168.77.147:dev/ali-test-app.git
cd ali-test-app
git switch -c main
touch README.md
git add README.md
git commit -m "add README"
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

推送现有文件夹

cd existing_folder
git init --initial-branch=main
git remote add origin git@192.168.77.147:dev/ali-test-app.git
git add .
git commit -m "Initial commit"
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

推送现有的 Git 仓库

cd existing_repo
git remote rename origin old-origin
git remote add origin git@192.168.77.147:dev/ali-test-app.git
  • 1.
  • 2.
  • 3.

4.代码上传示例

#全局配置用户
git config --global user.name "alibaby"
git config --global user.email "xxxx@qq.com"
#推送现有的git仓库
#进入现有的本地仓库地址
cd /data/app/test-app
#重命名
git remote rename origin old-origin
#添加新的仓库地址
git remote add origin git@192.168.77.147:dev/ali-test-app.git
#推送所有分支到远程仓库
git push -u origin --all
#推送标签到远端仓库
git push -u origin --tags
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

报错1

#推送所有分支到远程仓库
git push -u origin --all
#报错
####################################################################
To 192.168.77.147:dev/ali-test-app.git
 ! [remote rejected] dev -> dev (pre-receive hook declined)
 ! [remote rejected] master -> master (pre-receive hook declined)
error: 推送一些引用到 '192.168.77.147:dev/ali-test-app.git' 失败
#####################################################################
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

原因:alibaby用户的developer权限不足,更改为owner权限

解决:

代码发布之git与gitlab_代码上线_13

#推送所有分支到远程仓库
git push -u origin --all
#推送标签到远端仓库
git push -u origin --tags
  • 1.
  • 2.
  • 3.
  • 4.

gitlab平台可以看到推送完成

代码发布之git与gitlab_代码上线_14