centos7 under Gitlab + Jenkins continuous integration deployment environment CI

1. Basic environment

Host: win10, IP: 192.168.0.111; the deployment machine centos7, IP: 192.168.0.65;
RAM recommended to 8G, 6G measured above, in order to avoid insufficient memory error by.

2. Install the required components gitlab

[root@localhost ~]# yum -y install curl policycoreutils-python openssh-server openssh-clients postfix wget vim lrzsz
Launch Mail function

[root@localhost ~]# systemctl restart postfix
[root@localhost ~]# systemctl enable postfix

Open firewall http service

[root@localhost ~]# firewall-cmd --permanent --add-service=http
success
[root@localhost ~]# firewall-cmd --reload
success

3. Install gitlab

We installed gitlab-ce version, available online yum install yum -y install gitlab-ce, but more slowly, you can also download the rpm package.

[root@localhost ~]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-11.11.1-ce.0.el7.x86_64.rpm
[root@localhost ~]# rpm -ivh gitlab-ce-11.11.1-ce.0.el7.x86_64.rpm

4. Configure gitlab domain

[root @ localhost ~] # vim /etc/gitlab/gitlab.rb
change: 13 is external_url ' http://gitlab.example.com '
is: 13 is external_url ' http://192.168.0.65 '
public network, the public network domain name to

5. Initialization gitlab

[root@localhost ~]# gitlab-ctl reconfigure #时间比较长,出现如下信息则OK
Running handlers:
Running handlers complete
Chef Client finished, 481/1275 resources updated in 05 minutes 02 seconds
gitlab Reconfigured!
[root@localhost ~]# gitlab-ctl status  #查看gitlab状态
run: alertmanager: (pid 31394) 188s; run: log: (pid 31014) 232s
run: gitaly: (pid 31298) 192s; run: log: (pid 20450) 385s
run: gitlab-monitor: (pid 31297) 192s; run: log: (pid 30781) 250s
run: gitlab-workhorse: (pid 31271) 193s; run: log: (pid 30467) 285s
run: logrotate: (pid 30598) 270s; run: log: (pid 30618) 267s
run: nginx: (pid 30535) 281s; run: log: (pid 30554) 277s
run: node-exporter: (pid 31285) 193s; run: log: (pid 30708) 259s
run: postgres-exporter: (pid 31412) 188s; run: log: (pid 31131) 226s
run: postgresql: (pid 20668) 375s; run: log: (pid 20723) 372s
run: prometheus: (pid 31339) 191s; run: log: (pid 30919) 238s
run: redis: (pid 20318) 392s; run: log: (pid 20339) 390s
run: redis-exporter: (pid 31326) 192s; run: log: (pid 30841) 244s
run: sidekiq: (pid 30364) 293s; run: log: (pid 30385) 290s
run: unicorn: (pid 30274) 299s; run: log: (pid 30310) 296s

6. Log gitlab

http://192.168.0.65
first login gitlab, you need to set a password for the root user, the root user is gitlab super administrator, set here 12345678 (own setting)
centos7 under Gitlab + Jenkins continuous integration deployment environment CI
is set to continue after the completion of login password
centos7 under Gitlab + Jenkins continuous integration deployment environment CI
centos7 under Gitlab + Jenkins continuous integration deployment environment CI
set gitlab boot start

[root@localhost ~]# systemctl enable gitlab-runsvdir
[root@localhost ~]# systemctl restart gitlab-runsvdir

So far, gitlab environment to get

7. Management gitlab

关闭 gitlab: # gitlab-ctl stop
启劢 gitlab: # gitlab-ctl start
重启 gitlab: # gitlab-ctl restart
gitlab 主配置文件:/etc/gitlab/gitlab.rb //可以自定义一些邮件服务等
日志地址:/var/log/gitlab/ // 对应各服务
服务地址:/var/opt/gitlab/ // 对应各服务的主目录
仓库地址:/var/opt/gitlab/git-data //记录项目仓库等提交信息

8.注册新用户和关闭 gitlab 注册功能

默认情况下是可以注册新账号的,但为了安全管理通常关闭了注册功能
centos7 under Gitlab + Jenkins continuous integration deployment environment CI
关闭步骤如:
centos7 under Gitlab + Jenkins continuous integration deployment environment CI
点 Admin area -》 setting -》 取消 sign-up enabled 标签前对勾,最后点击页面下方的Save Changes。再次推出刷新登录的时候发现注册功能已经被关闭。

9.gitlab使用

9.1新建项目
新建项目前,先创建项目所在的组(也就是说这个项目文件是保存在哪个组里)
选择 Admin Area-----> Groups——New Group
centos7 under Gitlab + Jenkins continuous integration deployment environment CI
填写组名称和描述,根据需要设置群组看代码的权限,这里选择Internal(登录可见)
centos7 under Gitlab + Jenkins continuous integration deployment environment CI
点击页面下方的Create Group创建组
9.2新建项目
选择 Admin Area-----> Projects——New Project
centos7 under Gitlab + Jenkins continuous integration deployment environment CI
centos7 under Gitlab + Jenkins continuous integration deployment environment CI
9.3新建用户
选择 Admin Area-----> Users——New User
centos7 under Gitlab + Jenkins continuous integration deployment environment CI
centos7 under Gitlab + Jenkins continuous integration deployment environment CI
选择 Create user,基本资料可以丌用写
其中 Name 为对方的中文名,Username 是登录用户名,一般可以设置成邮箱的前缀,Email 为公司邮箱。
用户邮箱会收到修改密码连接,进入连接设置密码即可登录
也可由管理员设置用户初始密码
Admin Area---Users---Edit
centos7 under Gitlab + Jenkins continuous integration deployment environment CI
用户使用初始密码进行登录,根据页面提示设置自有密码即可。
配置 STMP 邮件发送服务 这个方法更可靠,但是有时也会收丌到邮件
编辑/etc/gitlab/gitlab.rb 文件,配置 STMP 邮件发送服务。前提你的邮件已经开通 stmp/pop3收发功能
[root@localhost ~]# vim /etc/gitlab/gitlab.rb #在 430 行插入
432 gitlab_rails['smtp_enable'] = true
433 gitlab_rails['smtp_address'] = "smtp.163.com"
434 gitlab_rails['smtp_port'] = 25
435 gitlab_rails['smtp_user_name'] = "[email protected]"
436 gitlab_rails['smtp_password'] = "自己邮箱的密码"
437 gitlab_rails['smtp_authentication'] = "login"
438 gitlab_rails['smtp_enable_starttls_auto'] = true
439 gitlab_rails['smtp_tls'] = true
440 gitlab_rails['gitlab_email_from'] = '[email protected]'
9.4将用户关联到组
将用户zhuo添加到 dxd 组中,身份选择"主程序员",这样后期就可以提交代码了。
选择“开发人员”会没有权限提交代码
Admin Area---Groups---dxd
centos7 under Gitlab + Jenkins continuous integration deployment environment CI
9.5给项目添加一个文件
centos7 under Gitlab + Jenkins continuous integration deployment environment CI
centos7 under Gitlab + Jenkins continuous integration deployment environment CI
页面下方commit提交

10.安装git客户端并使用gitlab

[root@localhost ~]# yum -y install git
克隆gitlab代码
代码地址可从gitlab查询到,这里使用http模式
centos7 under Gitlab + Jenkins continuous integration deployment environment CI
代码地址:http://192.168.0.65/dxd/dxd-front.git

[root@localhost ~]# git clone http://192.168.0.65/dxd/dxd-front.git  #克隆代码
正克隆到 'dxd-front'...
Username for 'http://192.168.0.65': zhuo    #这里会提示输入账号
Password for 'http://[email protected]':  #这里会提示输入密码
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.
[root@localhost ~]# cat dxd-front/index.html  #查看克隆下来的代码
前台代码aaa

11.运行git前配置

一般在新的系统上,我们都需要先配置下自己的 Git 工作环境。配置工作只需一次,以后升级时还会沿用现在的配置。
第一个要配置的是你个人的用户名称和电子邮件地址。这两条配置很重要,每次 Git 提交时都会引用这两条信息,说明是谁提交了更新,所以会随更新内容一起被永久纳入历叱记录。
git 运行的环境变量有点像.bashrc,决定了 Git 在各个环节的具体工作方式和行为。这些变量可以存放在以下两个的地方:
11.1 ~/.gitconfig 文件:用户目录下的配置文件只适用于该用户。若使用 git config 时用 --global选项,读写的就是这个文件。
设置用户信息

[root@localhost ~]# git config --global user.name "zhuo"
[root@localhost ~]# git config --global user.email "[email protected]"
[root@localhost ~]# cat ~/.gitconfig 
[user]
    name = zhuo
    email = [email protected]

11.2 当前项目的 Git 目录中的配置文件(也就是工作目录中的 .git/config 文件):这里的配置仅仅针对当前项目有效。每一个级别的配置都会覆盖上层的相同配置,所以 .git/config 里的配置会覆盖~/.gitconfig 中的同名变量。
如果要在某个特定的项目中使用其他名字戒者邮件地址,先迚到项目上下,然后只要去掉 --global 选项重新配置即可。 最后配置的用户和邮件地址会保存在当前项目的 .git/config 文件里。
配置项目下的环境变量

[root@localhost ~]# cd dxd-front/
[root@localhost dxd-front]# cat ./.git/config 
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = http://192.168.0.65/dxd/dxd-front.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
[root@localhost dxd-front]# 
[root@localhost dxd-front]# git config user.name "zhuo"
[root@localhost dxd-front]# git config user.email "[email protected]"
[root@localhost dxd-front]# cat ./.git/config  #查看name和email配置情况
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = http://192.168.0.65/dxd/dxd-front.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
[user]
    name = zhuo
    email = [email protected]

11.3 git常用命令
git config --global user.name “name“ #设置全局用户名
git config --global user.email mail #设置全局邮箱
git config --global --list #列出用户全局设置
git add index.html #添加文件到暂存区
git commit -m “描述内容“ #提交文件到工作区
git status #查看工作区的状态
git push #提交代码到 git 服务器上
git pull #获取代码到本地
git log #查看操作日志
vim .gitignore #定义忽略文件
git reset --hard HEAD^ #git 版本回滚, HEAD 为当前版本,加一个^为上一个,^^为上上一个版本
git reflog # #获取每次提交的 ID,可以使用--hard 根据提交的 ID 迚行版本回退
git reset --hard 4c83906 #回退到指定 id 的版本
git branch #查看当前所处的分支
git checkout #切换到指定分支戒标签
centos7 under Gitlab + Jenkins continuous integration deployment environment CI
11.4提交和回滚代码

[root@localhost ~]# cd dxd-front/
[root@localhost dxd-front]# echo "aaa.dxd.com" >> index.html  #修改index.html
[root@localhost dxd-front]# git add index.html  #添加index.html到暂存区
[root@localhost dxd-front]# git commit -m "add aaa.dxd.com" #提交文件到工作区
[master 77890e6] add aaa.dxd.com
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@localhost dxd-front]# git push  #提交代码到gitlab服务器
warning: push.default 未设置,它的默认值将会在 Git 2.0 由 'matching'
修改为 'simple'。若要不再显示本信息并在其默认值改变后维持当前使用习惯,
进行如下设置:

  git config --global push.default matching

若要不再显示本信息并从现在开始采用新的使用习惯,设置:

  git config --global push.default simple    #提示设置默认习惯,可执行此条命令即可完成设置。

参见 'git help config' 并查找 'push.default' 以获取更多信息。
('simple' 模式由 Git 1.7.11 版本引入。如果您有时要使用老版本的 Git,
为保持兼容,请用 'current' 代替 'simple' 模式)

Username for 'http://192.168.0.65': zhuo    #提示输入账号
Password for 'http://[email protected]':  #提示输入密码
Counting objects: 5, done.
Writing objects: 100% (3/3), 260 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://192.168.0.65/dxd/dxd-front.git
   4c83906..77890e6  master -> master
[root@localhost dxd-front]# cat index.html   #查看代码内容
前台代码aaaaaa.dxd.com
[root@localhost dxd-front]# rm -rf index.html  #模拟本地修改错误或删除文件
[root@localhost dxd-front]# ls   #index.html文件已经被删除
[root@localhost dxd-front]# git reset --hard HEAD   #回滚到服务器最新代码
HEAD 现在位于 77890e6 add aaa.dxd.com
[root@localhost dxd-front]# cat index.html   #查看
前台代码aaaaaa.dxd.com
[root@localhost dxd-front]# git reflog    #查看版本ID
77890e6 HEAD@{0}: commit: add aaa.dxd.com
4c83906 HEAD@{1}: clone: from http://192.168.0.65/dxd/dxd-front.git
[root@localhost dxd-front]# git reset --hard 4c83906   #回滚到指定ID版本
HEAD 现在位于 4c83906 Add new file
[root@localhost dxd-front]# cat index.html 
前台代码aaa

11.5 暂存区、工作区及分支
工作区就是编辑文件的目录区域,需要将工作区的修改好的文件 add 到暂存区才能提交到 git 服务器,在工作区有多个文件的时候可以将一个及多个文件添加至暂存区,再提交到 git 服务器
创建分支并提交到服务器

[root@localhost dxd-front]# git branch bbs     #创建分支
[root@localhost dxd-front]# git checkout bbs  #切换到分支,可做代码管理
切换到分支 'bbs'
[root@localhost dxd-front]# git branch            #查看当前所处的分支
* bbs
  master
[root@localhost dxd-front]# echo aa.cn>a.txt  #代码更新
[root@localhost dxd-front]# git add a.txt          #添加到暂存区
[root@localhost dxd-front]# git commit -m "add a.txt"   #提交到工作区
[bbs 0591f78] add a.txt
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt
[root@localhost dxd-front]# git push -u origin bbs         #服务器创建分支并,上传工作区更新代码到gitlab服务器
Username for 'http://192.168.0.65': zhuo
Password for 'http://[email protected]': 
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 269 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: 
remote: To create a merge request for bbs, visit:
remote:   http://192.168.0.65/dxd/dxd-front/merge_requests/new?merge_request%5Bsource_branch%5D=bbs
remote: 
To http://192.168.0.65/dxd/dxd-front.git
 * [new branch]      bbs -> bbs
分支 bbs 设置为跟踪来自 origin 的远程分支 bbs。

centos7 under Gitlab + Jenkins continuous integration deployment environment CI
gitlab服务器页面已经可以看见分支
11.6合并分支内容到master
[root@localhost dxd-front]# git checkout master
[root@localhost dxd-front]# git merge bbs
[root@localhost dxd-front]# git push

12.搭建Jenkins持续集成

12.1下载安装jdk
https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
centos7 under Gitlab + Jenkins continuous integration deployment environment CI
上传到服务器,安装jdk

[root@localhost ~]# ls
anaconda-ks.cfg  dxd-front  gitlab-ce-11.11.1-ce.0.el7.x86_64.rpm  jdk-8u212-linux-x64.rpm
[root@localhost ~]# rpm -ivh jdk-8u212-linux-x64.rpm

12.2下载安装Jenkins
https://pkg.jenkins.io/redhat/
centos7 under Gitlab + Jenkins continuous integration deployment environment CI
下方有很多更新版本的Jenkins,可以点击下载后安装
也可以在线安装

[root@localhost ~]# wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo   #安装源
[root@localhost ~]# rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
[root@localhost ~]# yum -y install jenkins

12.3设置Jenkins页面访问端口及用户
[root@localhost ~]# vim /etc/sysconfig/jenkins
改:29 JENKINS_USER="jenkins"
为:29 JENKINS_USER="root" #以 root
改:56 JENKINS_PORT="8080"
为:56 JENKINS_PORT="8888"
[root@localhost ~]# systemctl restart jenkins
[root@localhost ~]# systemctl enable jenkins
12.4访问Jenkins页面并安装插件
http://192.168.0.65:8888
如果访问被提示防火墙受阻,则关闭防火墙或者开启防火墙端口

[root@localhost ~]# firewall-cmd --permanent --add-port=8888/tcp
success
[root@localhost ~]# firewall-cmd --reload
success
根据页面提示查看初始密码 
[root@localhost ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
4f8f5c9d814c43eebeb217ff7f225728

centos7 under Gitlab + Jenkins continuous integration deployment environment CI
centos7 under Gitlab + Jenkins continuous integration deployment environment CI
安装默认推荐社区插件
centos7 under Gitlab + Jenkins continuous integration deployment environment CI
如果出现如下情况
centos7 under Gitlab + Jenkins continuous integration deployment environment CI
个别插件红叉了,安装失败,点击右下角Retry重试,多连接几次通常能安装OK。
要是确实不行,还提示你网络不行,需要做http代理,才能安装,那找一个HTTP代理IP,在系统管理->插件管理->高级->代理设置里配置好代理ip/端口,再安装,通常也能搞定。
如果网络不是很OK,安装不了,则可以找一个可以联网的机器,下载插件
http://updates.jenkins-ci.org/download/plugins
下载好,然后再从本地上传
系统管理->插件管理->高级->选择文件,上传后,重启Jenkins安装已经上传的插件。
也可以直接把一台安装好 jenkins 插件服务器的/var/lib/jenkins/plugins 目录下的文件复制到新的 jenkins 中重启Jenkins即可
[root@localhost ~]# cd /var/lib/jenkins
[root@localhost jenkins]# tar czvf plugins.tar.gz plugins/ #把安装好的插件打包
将 xuegod64 上的 plugins.tar.gz 上传到需要安装Jenkins新服务器 的/root/目录下
[root@localhost ~]# rm -rf /var/lib/jenkins/plugins
[root@localhost ~]# tar -zxvf /root/plugins.tar.gz -C /var/lib/jenkins/ #把插件解出来重启Jenkins,不重启插件不生效

So far, gitlab + Jenkins continuous integration environment CI basically completed, please pay attention to the following chapters automatically published

Guess you like

Origin blog.51cto.com/11573159/2406532