一、服务器环境配置 docker、gitlab-runner、maven、node
1.新增用户和项目目录规划:
groupadd xxx
useradd -g xxx xxx
passwd xxx
添加sudo权限
chmod u+w /etc/sudoers
vi /etc/sudoers
找到这行 root ALL=(ALL) ALL,在他下面添加
%xxx ALL=(ALL) ALL
chmod u-w /etc/sudoers
su xxx
#项目根目录
cd /home/xxx
#环境根目录
mkdir global
#工程根目录
mkdir project
#maven 本地仓库
mkdir global/maven-repo
#nginx 配置
mkdir global/nginx
#前端项目打包后的根目录
mkdir project/html
#文件目录
mkdir project/uploads
#设置用户组可访问
chmod -R 775 /home/xxx
2.安装gitlab-runner
sudo curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash
sudo yum -y install gitlab-runner
#修改密码
su
passwd gitlab-runner
#添加到组xxx
sudo gpasswd -a gitlab-runner xxx
3.安装 docker、docker-compose
切换到gitlab-runner
su gitlab-runner
sudo yum -y install docker
curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
【废弃sudo yum -y install epel-release
sudo yum -y install python-pip
sudo pip install --upgrade pip
sudo pip install docker-compose】
docker-compose -v
#给gitlab-runner 账户授权 docker
su
sudo groupadd docker
sudo gpasswd -a gitlab-runner docker
验证
su gitlab-runner
systemctl restart docker
sudo -u gitlab-runner -H docker info
sudo gpasswd -a xxx docker
systemctl restart docker
验证
sudo -u xxx -H docker info
4.安装maven
su gitlab-runner
sudo wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo
sudo yum -y install apache-maven
修改maven本地仓库地址
sudo vi /etc/maven/settings.xml
5.安装node
cd /home/xxx/global
wget https://nodejs.org/dist/v12.1.0/node-v12.1.0-linux-x64.tar.xz
tar -vxf node-v12.1.0-linux-x64.tar.xz
mv node-v12.1.0-linux-x64 nodeserver
cd nodeserver
sudo vi /etc/profile
export NODE_HOME=/home/xxx/global/nodeserver
export PATH=$NODE_HOME/bin:$PATH
source /etc/profile
npm install -g cnpm --registry=https://registry.npm.taobao.org
二、项目依赖环境(nginx、mysql、rabbit、redis 根据项目是否用到进行可选安装)
1.利用docker-compose 安装项目依赖环境,vi /home/xxx/global/docker-compose.yml
version: '3'
services:
redis:
image: redis
container_name: redis
restart: always
ports:
- 6379:6379
deploy:
resources:
limits:
cpus: '0.1'
memory: 1000M
volumes:
- ./redis/conf/redis.conf:/usr/local/etc/redis/redis.conf
- /etc/localtime:/etc/localtime:ro
- ./redis/data:/data
command: redis-server /usr/local/etc/redis/redis.conf
rabbitmq:
image: rabbitmq:management
container_name: rabbitmq
restart: always
ports:
- 5672:5672
- 15672:15672
deploy:
resources:
limits:
cpus: '0.1'
memory: 1000M
volumes:
- /etc/localtime:/etc/localtime:ro
environment:
RABBITMQ_DEFAULT_USER: admin
RABBITMQ_DEFAULT_PASS: admin
nginx:
image: nginx:latest
container_name: nginx
restart: always
ports:
- 80:80
- 81:81
deploy:
resources:
limits:
cpus: '0.1'
memory: 1000M
volumes:
- ./nginx:/etc/nginx/conf.d
- ../project/html:/usr/share/nginx/html
- ../project/uploads:/uploads
mysql-3306:
image: mysql:5.7.23
container_name: mysql-3306
restart: always
ports:
- 3306:3306
deploy:
resources:
limits:
cpus: '0.1'
memory: 1000M
volumes:
- ./mysql/3306:/var/lib/mysql
- /etc/localtime:/etc/localtime:ro
environment:
MYSQL_ROOT_PASSWORD: root
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
mysql-3307:
image: mysql:5.7.23
container_name: mysql-3307
restart: always
ports:
- 3307:3306
deploy:
resources:
limits:
cpus: '0.1'
memory: 1000M
volumes:
- ./mysql/3307:/var/lib/mysql
- /etc/localtime:/etc/localtime:ro
environment:
MYSQL_ROOT_PASSWORD: root
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
zoo1:
image: zookeeper
restart: always
hostname: zoo1
ports:
- 2181:2181
deploy:
resources:
limits:
cpus: '0.1'
memory: 1000M
#activemq容器
activemq:
#name: myactivemq
#定义主机名
hostname: myactivemq
#使用的镜像
image: webcenter/activemq
#容器的映射端口
ports:
- 61616:61616
- 8161:8161
deploy:
resources:
limits:
cpus: '0.1'
memory: 1000M
restart: always
#定义挂载点
volumes:
- ./activemq:/data/activemq
- ./var/log/activemq:/var/log/activemq
networks:
default:
external:
name: xxx
2.nginx配置文件修改(vue 版)
vi /home/xxx/global/nginx/vue.conf
server{
client_max_body_size 100m;
listen 80;
# server_name www.mycompany.com;
root /usr/share/nginx/html;
index index.html index.htm;
#超时配置
keepalive_timeout 1800s;
send_timeout 1800s;
proxy_connect_timeout 1800s;
proxy_send_timeout 1800s;
proxy_read_timeout 1800s;
#文件上传
location ^~ /api/upload {
proxy_pass http://sc-upload:69/upload;
}
#上传显示
location /uploads {
alias /uploads;
break;
}
#前端访问
location ^~ /admin {
try_files $uri $uri/ /admin/index.html;
}
#后端 api服务配置
location ^~ /api/sys {
proxy_pass http://sys:68/sys;
}
location ^~ /api/demo {
proxy_pass http://demo:67/demo;
}
# location ^~ /api/ {
# proxy_set_header Host $host;
# proxy_set_header X-real-ip $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_pass http://sc-zuul/;
# }
}
2.nginx配置文件修改(react 版)
vi /home/xxx/global/nginx/react.conf
server{
client_max_body_size 100m;
listen 81;
#server_name www.mycompany.com;
root /usr/share/nginx/html;
index index.html index.htm;
#文件上传
location ^~ /api/upload {
proxy_pass http://sc-upload:69/upload;
}
#上传显示
location /uploads {
alias /uploads;
break;
}
#前端访问
location ^~ /admin {
try_files $uri $uri/ /admin/index.html;
}
#后端 api访问
# location ^~ /api/demo/sys {
# proxy_pass http://sys:68/sys;
# }
# location ^~ /api/demo {
# proxy_pass http://demo:67/demo;
# }
location ^~ /api/demo {
proxy_pass http://sc-zuul/demo;
}
location ^~ /api/demo/sys {
proxy_pass http://sc-zuul/sys;
}
location ^~ /api/ {
proxy_pass http://sc-zuul/;
}
}
3.下载并运行镜像
sudo vi /etc/selinux/config
SELINUX=disabled
docker network create xxx
su
docker-compose -f docker-compose.yml --compatibility up -d
三、后台工程自动发版
1.添加镜像打包插件
a.根项目添加如下配置
...
${project.basedir}
b.需要打包项目添加
...
2.工程classpath下添加Dockerfile文件,内容:
FROM java:8-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ./${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Duser.timezone=GMT+08","-jar","/app.jar"]
3.添加本地环境变量(可选,该步骤可以本地手动打包镜像到远端服务器)
服务的开启tcp端口
sudo vi /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd-current -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock \
DOCKER_HOST=tcp://xxx.xxx.xxx.xxx:2375
4.自动发版-->>gitlab-ci脚本-->>根目录添加.gitlab-ci.yml,内容:
variables:
GIT_STRATEGY: fetch
stages:
- build
- deploy
before_script:
- pwd
scda-build:
stage: build
script:
- echo "开始执行 build.sh"
- sh ./build.sh
- echo "结束执行 build.sh"
only:
- master
tags:
- scda-product
scda-deploy:
stage: deploy
script:
- echo "开始执行 deploy.sh"
- sh ./deploy.sh
- echo "结束执行 deploy.sh"
only:
- master
tags:
- scda-product
dependencies:
- scda-build
5.自动发版-->>项目构建脚本-->>根目录添加build.sh,内容:
#!/bin/bash
if [[ ${CI_COMMIT_MESSAGE} =~ "@base" ]]
then
echo '提交信息包含@base,需要重新构建整个工程镜像'
mvn -DskipTests=true clean install -f pom.xml
else
echo '构建业务模块服务镜像'
cd ./sc-modules/
mvn -DskipTests=true clean package -f pom.xml
fi
6.自动发版-->>项目部署脚本-->>根目录添加deploy.sh,内容:
#!/bin/bash
echo '重新部署模块服务'
docker-compose -f docker-compose-service.yml --compatibility up -d
echo '清理无用镜像'
docker images|grep none|awk '{print $3 }'|xargs docker rmi
7.自动发版-->>项目运行镜像脚本-->>根目录添加docker-compose-base.yml,内容:
version: '3'
services:
sc-upload:
container_name: sc-upload
image: scda/sc-upload
restart: always
ports:
- 69:69
environment:
- EUREKA_HOST
- EUREKA_PORT
volumes:
- /home/scda/project/logs/sc-upload:/logs
- /home/scda/project/uploads:/uploads
deploy:
resources:
limits:
cpus: '0.3'
memory: 1000M
# depends_on:
# - xxx
networks:
default:
external:
name: xxx
8.自动发版-->>项目运行镜像脚本-->>根目录添加docker-compose-service.yml,内容:
version: '3'
services:
sys:
container_name: sys
image: scda/sys
restart: always
ports:
- 68:68
environment:
- EUREKA_HOST
- EUREKA_PORT
volumes:
- /home/scda/project/logs/sys:/logs
deploy:
resources:
limits:
cpus: '0.5'
memory: 1000M
demo:
container_name: demo
image: scda/demo
restart: always
ports:
- 67:67
environment:
- EUREKA_HOST
- EUREKA_PORT
volumes:
- /home/scda/project/logs/demo:/logs
deploy:
resources:
limits:
cpus: '0.5'
memory: 1000M
networks:
default:
external:
name: xxx
四、前台工程自动发版(react 版)
1.修改打包配置 app-manager\config\config.js
export default {
base: 'admin',
publicPath: '/admin/',
exportStatic: {},
//outputPath: './dist',
...
}
2.自动发版-->>gitlab-ci脚本-->>根目录添加.gitlab-ci.yml,内容:
variables:
GIT_STRATEGY: fetch
stages:
- build
- deploy
cache:
paths:
- node_modules
scda-admin-build:
stage: build
script:
- echo "当前操作目录:"
- echo "$(pwd)"
- echo "开始执行 build.sh"
- sh ./build.sh
- echo "结束执行 build.sh"
only:
- master
tags:
- admin-product
scda-admin-deploy:
stage: deploy
script:
- echo "开始执行 deploy.sh"
- sh ./deploy.sh
- echo "结束执行 deploy.sh"
only:
- master
tags:
- admin-product
dependencies:
- scda-admin-build
3.自动发版-->>项目构建脚本-->>根目录添加build.sh,内容:
#!/bin/bash
if [[ ${CI_COMMIT_MESSAGE} =~ "@install" ]]
then
echo '提交信息包含@install,需要更新安装依赖'
cnpm i
else
echo '不需要更新依赖'
fi
echo '开始构建项目'
cnpm run build
echo '复制构建文件到静态文件目录'
rm -rf /home/xxx/project/html/admin
mv dist /home/xxx/project/html/admin
4.自动发版-->>项目部署脚本-->>根目录添加deploy.sh,内容:
#!/bin/bash
echo '部署模块服务'
docker restart nginx
四、前台工程自动发版(vue 版)
1.修改打包配置 vue.config.js
module.exports = {
publicPath: '/vue/',
...
}
2.自动发版-->>gitlab-ci脚本-->>根目录添加.gitlab-ci.yml,内容:
variables:
GIT_STRATEGY: fetch
stages:
- build
- deploy
cache:
paths:
- node_modules
scda-admin-build:
stage: build
script:
- echo "当前操作目录:"
- echo "$(pwd)"
- echo "开始执行 build.sh"
- sh ./build.sh
- echo "结束执行 build.sh"
only:
- master
tags:
- admin-product-vue
scda-admin-deploy:
stage: deploy
script:
- echo "开始执行 deploy.sh"
- sh ./deploy.sh
- echo "结束执行 deploy.sh"
only:
- master
tags:
- admin-product-vue
dependencies:
- scda-admin-build
3.自动发版-->>项目构建脚本-->>根目录添加build.sh,内容:
#!/bin/bash
if [[ ${CI_COMMIT_MESSAGE} =~ "@install" ]]
then
echo '提交信息包含@install,需要更新安装依赖'
cnpm i
else
echo '不需要更新依赖'
fi
echo '开始构建项目'
cnpm run build
echo '复制构建文件到静态文件目录'
rm -rf /home/scda/project/html/vue
mv dist /home/scda/project/html/vue
4.自动发版-->>项目部署脚本-->>根目录添加deploy.sh,内容:
#!/bin/bash
echo '部署模块服务'
docker restart nginx
五、服务器gitlab-runner 注册到版本库
sudo gitlab-runner register
按步骤输入:
url、token->(gitlab CI/CD Runner)
description、tags->手动输入