1)持续集成流程说明
192.168.12.131 | 内存3G,2核 | JDK1.8,Tomcat8.5 |
---|---|---|
192.168.12.132 | 内存3G,2核 | Gitlab-12.4.2 |
192.168.12.133 | 内存2G,2核 | Jenkins-2.190.3,JDK1.8,Maven3.6.2,Git, SonarQube |
2)gitlab服务器
Gitlab简介
官网: https://about.gitlab.com/
GitLab 是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的
web服务。
GitLab和GitHub一样属于第三方基于Git开发的作品,免费且开源(基于MIT协议),与Github类似,
可以注册用户,任意提交你的代码,添加SSHKey等等。不同的是,GitLab是可以部署到自己的服务器
上,数据库等一切信息都掌握在自己手上,适合团队内部协作开发,你总不可能把团队内部的智慧总放
在别人的服务器上吧?简单来说可把GitLab看作个人版的GitHub。
Gitlab安装
安装的最低标准是4g内存(购买的阿里云服务器不支持)
可以官网作为仓库
仓库地址:https://gitlab.com/tools-study/hello-world-web-jenkins
yum -y install policycoreutils openssh-server openssh-clients postfix
2、启动ssh服务&设置为开机启动
systemctl enable sshd && sudo systemctl start sshd
3、 设置postfix开机自启,并启动,postfix支持gitlab发信功能
systemctl enable postfix && systemctl start postfix
4、开放ssh以及http服务,然后重新加载防火墙列表
firewall-cmd --add-service=ssh --permanent
firewall-cmd --add-service=http --permanent
firewall-cmd --reload
如果关闭防火墙就不需要做以上配置
5、下载gitlab包,并且安装
1)在线下载安装包:
wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el6/gitlab-ce-12.4.2-ce.0.el6.x86_64.rpm --no-check-certificate
2)安装
rpm -i gitlab-ce-12.4.2-ce.0.el6.x86_64.rpm
6、修改gitlab配置
修改gitlab访问地址和端口,默认为80,我们改为82vi /etc/gitlab/gitlab.rb
external_url 'http://192.168.12.132:82'
nginx['listen_port'] = 82
7、重载配置及启动gitlab
gitlab-ctl reconfigure
gitlab-ctl restart
8、把端口添加到防火墙
firewall-cmd --zone=public --add-port=82/tcp --permanent
firewall-cmd --reload
9、gitlab常用命令
sudo gitlab-ctl start # 启动所有 gitlab 组件;
sudo gitlab-ctl stop # 停止所有 gitlab 组件;
sudo gitlab-ctl restart # 重启所有 gitlab 组件;
sudo gitlab-ctl status # 查看服务状态;
sudo gitlab-ctl reconfigure # 启动服务;
sudo vim /etc/gitlab/gitlab.rb # 修改默认的配置文件;
gitlab-rake gitlab:check SANITIZE=true --trace # 检查gitlab;
sudo gitlab-ctl tail # 查看日志;
sudo systemctl disable gitlab-runsvdir.service # 禁止 Gitlab 开机自启动
sudo systemctl enable gitlab-runsvdir.service # 启用 Gitlab 开机自启动
10、卸载
# 1.停止gitlab
sudo gitlab-ctl stop
#2.卸载gitlab(这块注意了,看看是gitlab-ce版本还是gitlab-ee版本,别写错误了)
sudo rpm -e gitlab-ce
# 3.查看gitlab进程
ps -ef|grep gitlab
#杀掉第一个守护进程(runsvdir -P /opt/gitlab/service log)
kill -9 4473
# 再次查看gitlab进程是否存在
# 4.删除gitlab文件
# 删除所有包含gitlab的文件及目录
find / -name *gitlab*|xargs rm -rf
find / -name gitlab |xargs rm -rf
#删除gitlab-ctl uninstall时自动在root下备份的配置文件(ls /root/gitlab* 看看有没有,有也删除)
完整过程:
[root@zengqingfa sonar-compose]# yum -y install policycoreutils openssh-server openssh-clients postfix
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package policycoreutils-2.5-34.el7.x86_64 already installed and latest version
Package openssh-server-7.4p1-21.el7.x86_64 already installed and latest version
Package openssh-clients-7.4p1-21.el7.x86_64 already installed and latest version
Package 2:postfix-2.10.1-9.el7.x86_64 already installed and latest version
Nothing to do
[root@zengqingfa sonar-compose]# systemctl enable sshd && sudo systemctl start sshd
[root@zengqingfa sonar-compose]# systemctl enable postfix && systemctl start postfix
[root@zengqingfa sonar-compose]# firewall-cmd --add-service=ssh --permanent
FirewallD is not running
[root@zengqingfa sonar-compose]# firewall-cmd --add-service=http --permanent
FirewallD is not running
[root@zengqingfa sonar-compose]# firewall-cmd --reload
FirewallD is not running
[root@zengqingfa sonar-compose]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el6/gitlab-ce-12.4.2-ce.0.el6.x86_64.rpm
--2021-02-28 14:56:08-- https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el6/gitlab-ce-12.4.2-ce.0.el6.x86_64.rpm
Resolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8.193, 2402:f000:1:408:8100::1
Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.8.193|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 702935706 (670M) [application/x-redhat-package-manager]
Saving to: ‘gitlab-ce-12.4.2-ce.0.el6.x86_64.rpm’
100%[============================================================================>] 702,935,706 8.66MB/s in 92s
2021-02-28 14:57:41 (7.32 MB/s) - ‘gitlab-ce-12.4.2-ce.0.el6.x86_64.rpm’ saved [702935706/702935706]
[root@zengqingfa sonar-compose]# ll
total 686472
-rw-r--r-- 1 root root 795 Feb 28 12:21 docker-compose.yml
-rw-r--r-- 1 root root 702935706 Nov 5 2019 gitlab-ce-12.4.2-ce.0.el6.x86_64.rpm
[root@zengqingfa sonar-compose]# mkdir /opt/gitlab
[root@zengqingfa sonar-compose]# mv gitlab-ce-12.4.2-ce.0.el6.x86_64.rpm /opt/gitlab/
[root@zengqingfa sonar-compose]# cd /opt/gitlab/
[root@zengqingfa gitlab]# ll
total 686468
-rw-r--r-- 1 root root 702935706 Nov 5 2019 gitlab-ce-12.4.2-ce.0.el6.x86_64.rpm
[root@zengqingfa gitlab]# rpm -i gitlab-ce-12.4.2-ce.0.el6.x86_64.rpm
warning: gitlab-ce-12.4.2-ce.0.el6.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID f27eab47: NOKEY
It looks like GitLab has not been configured yet; skipping the upgrade script.
*. *.
*** ***
***** *****
.****** *******
******** ********
,,,,,,,,,***********,,,,,,,,,
,,,,,,,,,,,*********,,,,,,,,,,,
.,,,,,,,,,,,*******,,,,,,,,,,,,
,,,,,,,,,*****,,,,,,,,,.
,,,,,,,****,,,,,,
.,,,***,,,,
,*,.
_______ __ __ __
/ ____(_) /_/ / ____ _/ /_
/ / __/ / __/ / / __ `/ __ \
/ /_/ / / /_/ /___/ /_/ / /_/ /
\____/_/\__/_____/\__,_/_.___/
Thank you for installing GitLab!
GitLab was unable to detect a valid hostname for your instance.
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
[root@zengqingfa gitlab]# vi /etc/gitlab/gitlab.rb
[root@zengqingfa gitlab]# vim /etc/gitlab/gitlab.rb
[root@zengqingfa gitlab]# gitlab-ctl reconfigure
...
...
Running handlers:
Running handlers complete
Chef Client finished, 526/1425 resources updated in 02 minutes 17 seconds
gitlab Reconfigured!
[root@zengqingfa gitlab]# firewall-cmd --zone=public --add-port=82/tcp --permanent
FirewallD is not running
[root@zengqingfa gitlab]# gitlab-ctl restart
ok: run: alertmanager: (pid 32072) 1s
ok: run: gitaly: (pid 32095) 0s
ok: run: gitlab-exporter: (pid 32117) 0s
ok: run: gitlab-workhorse: (pid 32131) 1s
ok: run: grafana: (pid 32147) 0s
ok: run: logrotate: (pid 32168) 1s
ok: run: nginx: (pid 32251) 0s
ok: run: node-exporter: (pid 32265) 0s
ok: run: postgres-exporter: (pid 32272) 1s
ok: run: postgresql: (pid 32290) 0s
ok: run: prometheus: (pid 32303) 1s
ok: run: redis: (pid 32318) 0s
ok: run: redis-exporter: (pid 32386) 1s
ok: run: sidekiq: (pid 32407) 0s
ok: run: unicorn: (pid 32424) 1s
[root@zengqingfa gitlab]# ps -ef|grep gitlab
gitlab耗内存,可修改参数:
#减少进程数
#修改配置文件/etc/gitlab/gitlab.rb中的worker_processes:
unicorn['work_processes'] = 2
# 减少数据库缓存 128M
postgresql['shared_buffers'] = "256MB"
# 减少数据库并发数 4
postgresql['max_worker_processes'] = 8
# 减少sidekiq并发数 12
sidekiq['concurrency'] = 25
# 修改完成后执行配置命令
gitlab-ctl reconfigure
# 重启
gitlab-ctl restart
启动成功后,看到以下修改管理员root密码的页面,修改密码后,然后登录即可
http://192.168.12.132:82/
Gitlab添加组、创建用户、创建项目
1)创建组
使用管理员 root 创建组,一个组里面可以有多个项目分支,可以将开发添加到组里面进行设置权限,
不同的组就是公司不同的开发项目或者服务模块,不同的组添加不同的开发即可实现对开发设置权限的
管理
- Guest:可以创建issue、发表评论,不能读写版本库
- Reporter:可以克隆代码,不能提交,QA、PM可以赋予这个权限
- Developer:可以克隆代码、开发、提交、push,普通开发可以赋予这个权限
- Maintainer:可以创建项目、添加tag、保护分支、添加项目成员、编辑项目,核心开发可以赋予这个权限
- Owner:可以设置项目访问权限 - Visibility Level、删除项目、迁移项目、管理组成员,开发组组长可以赋予这个权限
4)在用户组中创建项目
以刚才创建的新用户身份登录到Gitlab,然后在用户组中创建新的项目
源码上传到Gitlab仓库
下面来到IDEA开发工具,我们已经准备好一个简单的Web应用准备到集成部署。
我们要把源码上传到Gitlab的项目仓库中。
1)项目结构说明
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.slfx</groupId>
<artifactId>web_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
</project>
servlet
package com.slfx;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
*
*/
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//模拟代码错误
int i = 100/0;
//模拟冗余代码
int a = 100;
a = 200;
resp.getWriter().write("hello Servlet!");
}
}
jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>演示项目主页</title>
</head>
<body>
如果看到此页面,代表项目部署成功啦!--这是master分支
<a href="/addUser">添加用户444</a>
<hr/>
<a href="/updateUser">修改用户</a>
<hr>
<a href="deleteUser">删除用户</a>
<a href="/findUser">查询用户</a>
</body>
</html>
2)开启版本控制
3)持续集成环境-Jenkins安装
服务器:192.168.12.133
a.安装jdk
Jenkins需要依赖JDK,所以先安装JDK1.8
yum install java-1.8.0-openjdk* -y
# 获取到jdk的安装路径
[root@k8snode1 ~]# java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
[root@k8snode1 ~]# echo $JAVA_HOME
[root@k8snode1 ~]# which java
/usr/bin/java
[root@k8snode1 ~]# whereis java
java: /usr/bin/java /usr/share/java /usr/share/man/man1/java.1
[root@k8snode1 ~]# ls -lrt /usr/bin/java
lrwxrwxrwx. 1 root root 22 11月 29 2018 /usr/bin/java -> /etc/alternatives/java
[root@k8snode1 ~]# ls -lrt /etc/alternatives/java
lrwxrwxrwx. 1 root root 41 11月 29 2018 /etc/alternatives/java -> /usr/java/jdk1.8.0_171-amd64/jre/bin/java
b.获取jenkins安装包
下载页面:https://jenkins.io/zh/download/
安装文件: jenkins-2.339-1.1.noarch.rpm
http://mirrors.jenkins-ci.org/redhat/
c.把安装包上传到服务器进行安装
[root@k8snode1 jenkins]# pwd
/opt/jenkins
[root@k8snode1 jenkins]# rpm -ivh jenkins-2.339-1.1.noarch.rpm
d.修复jenkins配置
vim /etc/sysconfig/jenkins
修改端口和用户:
JENKINS_USER="root"
JENKINS_PORT="8888"
e.重新启动jenkins
加入jenkins启动脚本jdk的位置:加入第一行
vim /etc/rc.d/init.d/jenkins
candidates="
/etc/alternatives/java
/usr/java/jdk1.8.0_171-amd64/jre/bin/java
/usr/lib/jvm/java-1.8.0/bin/java
/usr/lib/jvm/jre-1.8.0/bin/java
/usr/lib/jvm/java-11.0/bin/java
/usr/lib/jvm/jre-11.0/bin/java
/usr/lib/jvm/java-11-openjdk-amd64
/usr/bin/java
"
启动jenkins
systemctl start jenkins
# 查看jenkins的启动状态
systemctl status jenkins.service
# 重启
service jenkins restart
#停止
service jenkins stop
问题1:jenkins配置文件不生效等踩坑日志(jenkins版本:2.339)
原因:启动命令使用 systemctl start jenkins 。 systemd的资源Unit ,在 目录/usr/lib/systemd/system(Centos)或/etc/systemd/system(Ubuntu)。启动未加载/etc/sysconfig/jenkins 配置。
查看启动用户:为jenkins非root用户
[root@zqf ~]# ps -ef | grep jenkins
jenkins 28075 1 0 12:14 ? 00:03:26 /usr/bin/java -Djava.awt.headless=true -jar /usr/share/java/jenkins.war --webroot=%C/jenkins/war --httpPort=8080
root 37041 29016 0 19:32 pts/3 00:00:00 grep --color=auto jenkins
解决:
vim /usr/lib/systemd/system/jenkins.service
#修改USER GROUP JENKINS_PORT
User=root
Group=root
Environment="JENKINS_PORT=8888"
#执行命令
[root@zqf ~]# systemctl restart jenkins
Warning: jenkins.service changed on disk. Run 'systemctl daemon-reload' to reload units.
[root@zqf ~]# ps -ef | grep jenkins
root 37205 1 63 19:42 ? 00:00:36 /usr/bin/java -Djava.awt.headless=true -jar /usr/share/java/jenkins.war --webroot=%C/jenkins/war --httpPort=8888
root 37315 29016 0 19:43 pts/3 00:00:00 grep --color=auto jenkins
问题2:Failed to start jenkins.service: Unit is masked,jenkins服务被锁住了
[root@aubin ~]# systemctl start jenkins
Failed to start jenkins.service: Unit is masked.
[root@aubin ~]# systemctl status jenkins
● jenkins.service
Loaded: masked (/usr/lib/systemd/system/jenkins.service; masked; vendor preset: disabled)
Active: inactive (dead)
cd /lib/systemd/system
rm -rf jenkins.service
systemctl daemon-reload
systemctl start jenkins
f.浏览器访问
http://192.168.12.133:8888/login?from=%2F
[root@k8snode1 sysconfig]# cat /var/lib/jenkins/secrets/initialAdminPassword
3e5aeb5ab0f9483fa4edd7ef7cc32b14
g.跳过插件安装
因为Jenkins插件需要连接默认官网下载,速度非常慢,而且经过会失败,所以我们暂时先跳过插件安
装
h.创建账户
添加一个管理员账户,并进入Jenkins后台
zengqingfa/123456
i)jenkins卸载
# 1、rpm卸载
rpm -e jenkins
# 2、检查是否卸载成功
rpm -ql jenkins
#3、彻底删除残留文件:
find / -iname jenkins | xargs -n 1000 rm -rf
j)jenkins升级
修改jenkins.war包即可
下载地址:https://get.jenkins.io/war-stable/
linux中的安装目录地址:
[root@aubin jenkins]# cd /usr/lib/jenkins
[root@aubin jenkins]# pwd
/usr/lib/jenkins
[root@aubin jenkins]# ll
total 267688
-rw-r--r--. 1 root root 91373543 Apr 14 16:54 jenkins.war
-rw-r--r--. 1 root root 91362983 Apr 8 17:38 jenkins.war.bak
-rw-r--r--. 1 root root 91373543 Apr 14 16:54 jenkins.war.tmp
war包备份,替换war包重启jenkins即可
systemctl start jenkins
jar包直接启动的方式启动jenkins:
nohup java -jar jenkins_2.235.5.war --httpPort=8888 -- > jenkins.log>&1 &