1、什么是持续集成
持续集成(Continuous Integration,CI):代码合并、构建、部署、测试都在一起,不断地执行这个过程,并对结果反馈。
持续部署(Continuous Deployment,CD):部署到测试环境、预生产环境、生产环境。
持续交付(Continuous Delivery,CD):将最终产品发布到生产环境,给用户使用。
CI设计流程图:
工作流程:
1. 开发人员提交代码到Git版本仓库;
2. Jenkins人工/定时触发项目构建;
3. Jenkins拉取代码、代码编码、打包镜像、推送到镜像仓库;
4. Jenkins在Docker主机创建容器并发布
2、特点
- 及时发现问题
- 大幅度减少故障率
- 加快迭代速度
- 减少时间成本
3、安装gitlab
docker run -d \
--name gitlab \
-p 8443:443 \
-p 9999:80 \
-p 9998:22 \
-v $PWD/conf:/etc/gitlab \
-v $PWD/logs:/var/log/gitlab \
-v $PWD/data:/var/opt/gitlab \
-v /etc/localtime:/etc/localtime \
-e LANG=C.UTF-8 \
--restart=always \
gitlab/gitlab-ce
3.1、gitlab优化
由于gitlab启动时占用内存过高,关闭一些不需要地服务
docker exec -it gitlab bash
配置文件在/etc/gitlab/gitlab.rb
1.减少数据库缓存
postgresql['shared_buffers'] = "64MB"
2.减少数据库并发数
postgresql['max_worker_processes'] = 1
3.减少sidekiq并发数
sidekiq['concurrency'] = 1
4.减少进程数
nginx['worker_processes'] = 2
puma['worker_processes'] = 1
5.禁用prometheus
prometheus_monitoring['enable'] = false
6.关闭一些监控和性能基准相关的功能
prometheus['enable'] = false
prometheus_monitoring['enable'] = false
alertmanager['enable'] = false
node_exporter['enable'] = false
redis_exporter['enable'] = false
postgres_exporter['enable'] = false
pgbouncer_exporter['enable'] = false
gitlab_exporter['enable'] = false
grafana['enable'] = false
sidekiq['metrics_enabled'] = false
3.3、端口配置
我这里直接用ip+端口了,修改配置如下,这样仓库地址就带上端口了
external_url 'http://xxx.xxx.xxx.xxx:9999'
gitlab_rails['gitlab_shell_ssh_port'] = 9998
gitlab_rails['time_zone'] = 'Asia/Shanghai
3.4、重新加载gitlab
gitlab-ctl reconfigure
默认密码是在/etc/gitlab/initial_root_password下
Password: lthOtqJ8+XjgliMkNFZ6q8ESmRWxMJxrO6ZEfF59rmc=
访问地址:http://ip:9999
访问页面可能会出现502,需要等一会
3.5、创建项目、提交测试代码
进入后先创建项目,提交代码,以便后面测试。
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
git clone https://github.com/dingkai163/tomcat-java-demo.git
# 进入代码文件夹,修改远程代码仓库地址
cat .git/config
修改:
url = http://10.0.0.11:9999/root/java-demo.git # 改成你的gitlab仓库地址
git add . # 将代码添加到暂存区
git commit -m "all" # 提交代码
git push origin master # 推送到远程仓库
4、安装Harbor镜像仓库
4.1、安装docker于docker-compose
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker
# 设置阿里云镜像加速器
cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://bxfx2kaa.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
}
}
sudo systemctl daemon-reload
sudo systemctl restart docker
4.2、解压离线包部署Harbor
rz harbor-offline-installer-v2.2.0-rc3.tgz
tar xf harbor-offline-installer-v2.2.0-rc3.tgz
cd harbor
cp harbor.yml.tmpl harbor.yml
vi harbor.yml
hostname: 10.0.0.11
# 注释https
#https:
# # https port for harbor, default is 443
# port: 443
# # The path of cert and key files for nginx
# certificate: /your/certificate/path
# private_key: /your/private/key/path
./prepare
./install.sh
访问地址:10.0.0.11
4.3、在Jenkins主机配置Docker可信任
由于Harbor未配置https,还需要在docker配置可信任
配置文件完整如下:
{
"registry-mirrors": ["https://bxfx2kaa.mirror.aliyuncs.com"],
"insecure-registries": ["10.0.0.11"],
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
}
}
sudo systemctl daemon-reload
sudo systemctl restart docker
5、部署Jenkins
5.1、准备JDK和Maven环境
rz jdk-8u241-linux-x64.tar.gz
tar xf jdk-8u241-linux-x64.tar.gz
mv jdk1.8.0_241/ /usr/local/jdk
rz apache-maven-3.8.1-bin.tar.gz
tar -xf apache-maven-3.8.1-bin.tar.gz
mv apache-maven-3.8.1 /usr/local/maven
5.2、安装Jenkins
docker run -d --name jenkins -p 81:8080 -p 50000:50000 -u root -m 1024m \
-v /opt/jenkins_home:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /usr/bin/docker:/usr/bin/docker \
-v /usr/local/maven:/usr/local/maven \
-v /usr/local/jdk:/usr/local/jdk \
-v /etc/localtime:/etc/localtime \
--restart always jenkins/jenkins:2.387.2-lts
# 设置Jenkins清华源
cd jenkins_home/updates
sed -i 's/https:\/\/updates.jenkins.io\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json && \
sed -i 's/https:\/\/www.google.com/https:\/\/www.baidu.com/g' default.json
#重启Jenkins
docekr restart jenkins
访问地址:10.0.0.11:81
注意:什么插件都不要安装,用什么插件安装什么插件
5.3、安装插件
系统管理—>插件管理—>AVailable
搜索git\pipeline\Git Parameter,点击安装
6、创建项目
创建Pipeline项目
添加构建话参数
Pipeline脚本如下
#!/usr/bin/dev groovy
def registry = "10.0.0.11" // harbor地址
def project = "dev" // harbor仓库项目
def app_name = "java-demo" // 容器名字
def image_name = "${registry}/${project}/${app_name}:${Branch}-${BUILD_NUMBER}" //镜像名称
def git_address = "http://10.0.0.11:9999/root/java-demo.git" // 仓库地址
def docker_registry_auth = "b99f4a19-87e9-47b3-a649-ac02c30d39d5" //harbor仓库凭据ID
def git_auth = "052883f5-333b-4805-9847-0cfbd413e139" //gitlab仓库凭据ID
def host_port = "88"
def container_port = "8080"
pipeline {
agent any
stages {
stage('拉取代码') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '$branch']], userRemoteConfigs: [[credentialsId: "$git_auth", url: "$git_address"]]])
}
}
stage('代码编译') {
steps {
sh """
pwd
JAVA_HOME=/usr/local/jdk
PATH=$JAVA_HOME/bin:/usr/local/maven/bin:$PATH
mvn clean package -Dmaven.test.skip=true
"""
}
}
stage('构建镜像') {
steps {
withCredentials([usernamePassword(credentialsId: "${docker_registry_auth}",passwordVariable: 'password',usernameVariable: 'username')]) {
sh """
echo '
FROM majiajue/jdk1.8
LABEL maitainer zhanghuitao
COPY target/*.jar /usr/local/
' > Dockerfile
docker build -t ${image_name} .
docker login -u ${username} -p ${password} ${registry}
docker push ${image_name}
"""
}
}
}
stage('部署到docker') {
steps {
sh """
REPOSITORY=${image_name}
docker rm -f ${app_name} | true
docker run -d --name ${app_name} -p ${host_port}:${container_port} ${image_name} bash
"""
}
}
}
}
点击build
成功构建会提示: SUCCESS
可以看到可以正常访问,至此基于 Jenkins+Docker+Gitlab 的CI流程已经成功部署了。