持续集成之Gitlab安装与应用

Gitlab 是一个利用 Ruby on Rails 开发的开源应用程序,实现一个自托管的 Git 项目仓库,可通过Web 界面进行访问公开的或者私人的项目 Gitlab 拥有与 Github 类似的功能,能够浏览源代码,管理缺陷和注释。可以管理团队对仓库的访问,他非常易于浏览提交过的版本并提供一个文件历史库。他还提供一个代码片段收集功能可以轻松实现代码复用,便于日后有需要的时候进行查找

一、环境准备
如果是测试环境,其内存建议2G及以上,可以去清华开源镜像站下载所需gitlab版本,其安装后,会自动安装nginx提供web界面,所以要避免80端口占用。

二、安装部署gitlab

1. 安装gitlab

[[email protected] /]# mkdir git
[[email protected] /]# cd git/
[[email protected] git]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-11.9.8-ce.0.el7.x86_64.rpm
[[email protected] git]# rpm -ivh gitlab-ce-11.9.8-ce.0.el7.x86_64.rpm
#当gitlab安装完毕后会有一个大狐狸头
#由于我不打算做域名解析,所以需要修改其配置文件
[[email protected] git]# vim /etc/gitlab/gitlab.rb
external_url ‘http://192.168.171.134‘                # 将原本的域名改为本机IP
[[email protected] /]# gitlab-ctl reconfigure                 #重新配置gitlab,就算不修改配置文件,也需要在安装后重新配置gitlab,此处会等一会
[[email protected] /]# netstat -anput | grep -w 80          #确定nginx在监听80端口

2.配置gitlab
客户端访问服务器的IP地址,可以看到以下界面(配置密码并登陆):


上传服务器公钥(接下来的操作与在github上大同小异),先在服务器上生成密钥对:

[[email protected] /]# ssh-keygen -t rsa -C "[email protected]"
[[email protected] /]# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7mTAcNqGbsRoPPAd2M+xfdVfsa4lLPVs37WHbM+iept0DdSIgVaoz++w4oHTccWcH6K5hvCJo5AvTzIuSn3rAV8E6sROL2yCafcLE+//rSpW5/cenvsuYhNV5jkqfuCs1moVev0BHnW//9XZJpyQFIQ8JNlASFGmRXOycNuP9NeacSo5IoXjRzHTA28674+LZwo6D6+klfAYo75rD7JQ61DhrAEq/xBHQ+5zigo6i95xsRdUMPpKRsb0fruTBYUC0jwNfKzgal1CuGTmqJ+l9MI4cnbHNYk2TYP74NgcUYHfp0n6Lr7HkUDAtsyGbT9zPfatU3nVOihN8efdupb8d [email protected]

然后回到web界面:


添加后如下:

创建一个库:



回到服务器上进行克隆刚刚创建的库:

[[email protected] /]# git clone [email protected]:root/test1.git           # 进行克隆
[[email protected] /]# cd test1/                 # 可以看到这里和刚才库里的东西一样
[[email protected] test1]# ls
README.md
#定义用户名及email
[[email protected] test1]# git config --global user.name "test"
[[email protected] test1]# git config --global user.email "[email protected]"
#创建测试文件,并推送到远端库中测试
[[email protected] test1]# echo "aaaa" > test.txt
[[email protected] test1]# git add test.txt
[[email protected] test1]# git commit -m "alter from 192.168.171.134"
[master 81dc1f8] alter from 192.168.171.134
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
[[email protected] test1]# git push origin master 

刷新web界面的库页面:

三、远端库的基本操作

当你从远端仓库克隆时,实际上git自动把本地的master分支和远端的master分支对应起来了,并且远程仓库的默认名称是origin。
要查看远程库的信息,使用以下命令

[[email protected] test1]# git remote                  # 简略信息
origin
[[email protected] test1]# git remote -v                # 详细信息
origin  [email protected]:root/test1.git (fetch)
origin  [email protected]:root/test1.git (push)

推送分支:

[[email protected] test1]# git push origin master        #推送本地的master分支
[[email protected] test1]# git push origin dev            #推送本地的dev分支,若远端没有dev分支,会自动创建

抓取分支:

[[email protected] test1]# git pull origin dev          #根据提示将远端的dev分支抓取下来
当我们从远程库克隆时,默认情况下,只能看到master分支,可以使用git branch命令确认。
解决多人协作容易产生的问题
当我们整个小组对同一个分支进行开发时,如果在你提交之前,你的同事已经修改了分支的内容并推送到远端仓库,而碰巧你也对同样的文件做了修改,并试图推送,那么会推送失败,因为你的同事的最新提交的数据和你试图提交的数据有冲突(你本地的内容比远端仓库的旧了),解决的办法会在提示你推送失败的返回信息中给出,这里我们模拟一下这一过程。

[[email protected] /]# mkdir test2
[[email protected] /]# cd test2/
[[email protected] test2]# git clone [email protected]:root/test1.git
[[email protected] test2]# cd test1/
[[email protected] test1]# git checkout -b dev
Switched to a new branch ‘dev‘
[[email protected] test1]# echo "bbbb" > tmp.txt
[[email protected] test1]# git add tmp.txt
[[email protected] test1]# git commit -m "commit from /test2"
[dev ba44ec3] commit from /test2
 1 file changed, 1 insertion(+)
 create mode 100644 tmp.txt
[[email protected] test1]# git push origin dev

回到web界面进行刷新,即可看到新提交的分支:

上面的操作是在/test2目录下进行操作的,那么现在操作/root目录下的远程仓库:

[[email protected] /]# cd root/test1/                #进入root目录下的远程仓库并创建dev分支,推送内容至远端仓库
[[email protected] test1]# git checkout -b dev
Switched to a new branch ‘dev‘
[[email protected] test1]# echo "ccccc" > root.txt
[[email protected] test1]# git add root.txt
[[email protected] test1]# git commit -m "commit from /root"
[dev f8fd78d] commit from /root
 1 file changed, 1 insertion(+)
 create mode 100644 root.txt
[[email protected] test1]# git push origin dev             #此时我们推送,就会提示以下错误
To [email protected]:root/test1.git
 ! [rejected]        dev -> dev (fetch first)
error: failed to push some refs to ‘[email protected]:root/test1.git‘
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: ‘git pull‘) before pushing again.
hint: See the ‘Note about fast-forwards‘ in ‘git push --help‘ for details.
#无法推送一些引用到‘[email protected]:root/test1.git‘
#提示远程版本库有我们本地版本库没有的提交,所以需要先将远端版本库pull下来,再提交
[[email protected] test1]# git pull origin dev            #根据提示将远端的dev分支pull下来
[[email protected] test1]# ls
README.md  root.txt  test.txt  tmp.txt
[[email protected] test1]# git push origin dev           #然后再次将本地的dev分支推送到gitlab,即可成功

此时,web界面的dev分支就有了我们在/test2目录和/root目录下提交的所有内容,如下:

但是master分支,仍然是最初的文件,如下:

现在进行远程版本库的分支合并,如下


[[email protected] test1]# git checkout master            # 切换到master分支
Switched to branch ‘master‘
[[email protected] test1]# git merge dev            # 合并dev分支
Updating 81dc1f8..386d906
Fast-forward
 root.txt | 1 +
 tmp.txt  | 1 +
 2 files changed, 2 insertions(+)
 create mode 100644 root.txt
 create mode 100644 tmp.txt
[[email protected] test1]# git pull
Already up-to-date.
[[email protected] test1]# git add *
[[email protected] test1]# git commit -m "提交"
[[email protected] test1]# git push origin master          # 推送到远端库


现在已经拥有了dev分支的所有内容,那么接下来就演示如何删除远程版本库的dev分支:

[[email protected] test1]# git branch -d dev              # 删除本地的dev分支
Deleted branch dev (was 386d906).
[[email protected] test1]# git branch -r -d origin/dev           # #删除指定的远程分支
Deleted remote branch origin/dev (was 386d906).
[[email protected] test1]# git push origin :dev          # #将删除的分支提交到远程版本库中
To [email protected]:root/test1.git
 - [deleted]         dev

至此,远端版本库中的dev分支就被删除了,如下:

四、重置gitlab管理员密码

[[email protected] /]# gitlab-rails console production          #执行该命令,只有第一个命令字可以tab出来
-------------------------------------------------------------------------------------
 GitLab:       11.9.8 (48528bc)
 GitLab Shell: 8.7.1
 postgresql:   9.6.11
-------------------------------------------------------------------------------------
Loading production environment (Rails 5.0.7.1)
irb(main):001:0> user = User.where(id:1).first
=> #<User id:1 @root>
irb(main):002:0> user.password=‘test1234‘
=> "test1234"
irb(main):003:0> user.password_confirmation=‘test1234‘
=> "test1234"
irb(main):004:0> user.save
Enqueued ActionMailer::DeliveryJob (Job ID: 86d1357c-d974-4bd9-ad0c-03b4496d9327) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", #<GlobalID:0x00007f33612fa170 @uri=#<URI::GID gid://gitlab/User/1>>
=> true
irb(main):005:0> true
=> true
irb(main):006:0> exit
至此,再次登录,就需要使用新密码test1234进行登录了。

原文地址:https://blog.51cto.com/14306186/2487533

时间: 2024-08-29 16:49:17

持续集成之Gitlab安装与应用的相关文章

Jenkins持续集成 之 GitLab安装

一.安装相应所需依赖包 yum install lokkit yum install curl openssh-server openssh-clients postfix cronie -y service postfix start chkconfig postfix on lokkit -s http -s ssh curl -sS http://packages.gitlab.cc/install/gitlab-ce/script.rpm.sh | sudo bash 二.添加gitla

一步一步构建iOS持续集成:Jenkins+GitLab+蒲公英+FTP

什么是持续集成 持续集成是一种软件开发实践,即团队开发成员经常集成它们的工作,通过每个成员每天至少集成一次,也就意味着每天可能会发生多次集成.每次集成都通过自动化的构建(包括编译,发布,自动化测试)来验证,从而尽早地发现集成错误. 为什么使用持续集成 1.减少风险2.减少重复过程3.任何时间.任何地点生成可部署的软件4.增强项目的可见性 常用的持续集成工具 Jenkins CI Travis CI Hudson CI Circle CI 市面上的持续集成工具有很多,考虑到Jenkins的稳定性,

Jenkins持续集成之Jenkins 安装部署

1.1 Jenkins概念: Jenkins是一个功能强大的应用程序,允许持续集成和持续交付项目,无论用的是什么平台.这是一个免费的源代码,可以处理任何类型的构建或持续集成.集成Jenkins可以用于一些测试和部署技术.Jenkins是一种软件允许持续集成. 1.2 Jenkins目的: 1.持续.自动地构建/测试软件项目. 2.监控软件开放流程,快速问题定位及处理,提示开放效率. 这些概念我没去理解,我现在只知道用它可以减轻我的工作量. 因为代码发布真的是很繁琐的活,像 nodejs 是先从

Linux进阶之Jenkins持续集成介绍及安装演示

一.Jenkins介绍 Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能. Jenkins功能包括: 1.持续的软件版本发布/测试项目. 2.监控外部调用执行的工作. 特点: 1. jenkins就是基于Java开发的一种持续集成的工具 2. 可以将运维用到的各个脚本整合起来,并且可以通过页面方式集中管理,而且也可以实现和gitlab.github交互,也可以实现自动编译.部署程序等. 二.持

持续集成篇--Hudson持续集成服务器的安装配置与使用

IP:192.168.4.221  8G内存(Hudson多个工程在同时构建的情况下比较耗内存) 环境:CentOS 6.6.JDK7 Hudson不需要用到数据库 参考:http://www.roncoo.com/index.html Hudson只是一个持续集成服务器(持续集成工具),要想搭建一套完整的持续集成管理平台,还需要用到前面课程中所讲到的SVN.Maven.Sonar等工具,按需求整合则可. 1.  安装JDK并配置环境变量(略) JAVA_HOME=/usr/local/java

持续集成之④:GitLab触发jenkins构建项目

一:目的为在公司的测试环境当中一旦开发向gitlab仓库提交成功代码,gitlab通知jenkins进行构建项目.代码质量测试然后部署至测试环境,注意这只是测试环境,而生产环境依然需要手动部署代码: 1.1:jenkins配置:1.1.1:安装Gitlab Hook Plugin插件:#系统管理-管理插件-可选插件-Gitlab Hook Plugin和Build Authorization Token Root Plugin 1.1.2:生成随机token: # openssl rand -h

Jenkins持续集成 之 Jenkins安装

一.安装JDK与TOMCAT8 参考地址: http://ibm.chick.blog.163.com/blog/static/144201610201652811537410/ 二.下载Jenkins安装包 wget http://mirrors.jenkins.io/war/latest/jenkins.war 三.把jenkins.war放到TOMCAT下的webapps 四.启动tomcat并访问 http://10.3.152.50:8080/jenkins/ 备注:初始登陆,需要按指

持续集成 Jenkins +Gitlab + SSH 自动发布 HTML 代码

一.整体流程 二.Jenkins 配置 2.1.首先安装插件 Gitlab Hook Plugin GitLab Plugin Publish Over SSH 2.2.配置目标服务器 系统管理 ---> 系统设置 ---> Publish over SSH 如果不想使用密钥认证登陆,也可以使用 账户密码登陆. 2.3.创建 job 我们创建一个自由风格的 job,配置好 git 源,这里也可以配置无密钥登陆,我这里使用账户密码. 创建触发器,用于自动构建,我们需要把其中的 url 添加到我们

持续集成工具Hudson安装实例

安装maven 下载maven,解压 [[email protected] local]# pwd /usr/local [[email protected] local]# tar -zxvf apache-maven-3.0.5-bin.tar.gz [[email protected] local]# cd apache-maven-3.0.5/ [[email protected] apache-maven-3.0.5]# pwd /usr/local/apache-maven-3.0.