一.介绍
比起jenkins声明式流水线,脚本式简单很多而且相对用得比较多。
为了理解jenkins的工作原理,需先在本地完全部署运行过一次项目后,才能用在jenkins上。
所以本节示例先用java项目在本地运行后,再用脚本进行CI/CD发版部署。
jenkins和gitlab在之前的文章上已经有安装详情:
jenkins和gitlab安装:https://www.yj-example.cn/?p=1871
二.手动部署微服务(本地)
那么我们以java微服务为案例先在本地经行部署。
项目说明:本项目使用开源的pig微服务开发框架pig4为案例进行部署
项目地址:https://www.yuque.com/pig4cloud/pig/gg3ndm
前端代码:https://gitee.com/log4j/pig-ui
后端代码:https://gitee.com/log4j/pig
项目介绍:前端使用HTML、nodejs编写,后端使用:java语言编写,数据库使用:redis、ysql,项目使用nacos进行统一管理
服务器:jenkins服务器(编译)4C8G、gitlab服务器、mysql+redis+应用服务器(由于示例都放一起了)4c8G
java项目需使用maven进行编译打包,前端项目需要使用node进行打包
1.服务器安装配置maven
配置maven环境apache-maven-3.6.2
所有服务器关防火墙、selinux:
systemctl stop firewalld vi /etc/selinux/config SELINUX=disable reboot
下载安装maven包(需要在jenkins服务器和应用服务器安装):
[root@jenkins ~]# cd /export/soft/ [root@jenkins soft]# wget https://dlcdn.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz --no-check-certificate [root@jenkins soft]# tar xf apache-maven-3.6.3-bin.tar.gz [root@jenkins soft]# mv apache-maven-3.6.3 /usr/local/apache-maven [root@jenkins soft]# vi /etc/profile # 最后一行添加 export MAVEN_HOME=/usr/local/apache-maven export PATH=$MAVEN_HOME/bin:$PATH # 生效环境变量 [root@jenkins soft]# source /etc/profile #验证: [root@jenkins soft]# mvn --version Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
2.后端编译
可以在jenkins服务器上编译
[root@jenkins ~]# mkdir pig [root@jenkins ~]# cd pig/
下载后端代码进行编译—-代码准备
https://github.com/pig-mesh/pig [root@jenkins pig]##将代码上传至本目录 [root@jenkins pig]# ls pig-master.zip [root@jenkins pig]# unzip pig-master.zip [root@jenkins pig]# cd pig-master/
编译成jar包
[root@jenkins pig-master]# mvn clean install -Dmaven.skip.test=true
3.安装数据库mysql
mysql和redis都安装在应用服务器上
二进制安装:
[root@localhost ~]# hostnamectl set-hostname mysql && bash
#版本要求5.7+
[root@k8s-mysql ~]# cd /usr/local/
#上传下载好的mysql5.7.26的安装包到服务器
[root@k8s-mysql local]# tar xf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz [root@k8s-mysql local]# ln -s mysql-5.7.26-linux-glibc2.12-x86_64 mysql57 [root@k8s-mysql local]# vim /etc/profile ...... export PATH=/usr/local/mysql57/bin:$PATH [root@k8s-mysql local]# source /etc/profile #注意:此处配置文件lower_case_table_names = 1为不区分大小写配置(可根据实际情况修改) [root@k8s-mysql local]# vim /etc/my.cnf [mysqld] user=mysql port=3306 basedir=/usr/local/mysql57 datadir=/data/mysql/data_3306 server_id=6 socket=/tmp/mysql.sock lower_case_table_names = 1 [mysql] socket=/tmp/mysql.sock [root@k8s-mysql local]# useradd mysql [root@k8s-mysql local]# mkdir /data [root@k8s-mysql local]# mkdir -p /data/mysql/data_3306 #数据路径 [root@k8s-mysql local]# mkdir -p /data/mysql/binlog_3306 #binlog日志路径 [root@k8s-mysql local]# chown -R mysql.mysql /usr/local/mysql57 [root@k8s-mysql local]# chown -R mysql.mysql /data [root@k8s-mysql local]# yum install libaio-devel -y [root@k8s-mysql local]# cd /data/mysql/data_3306/ #忽略警告。。。。。。 [root@k8s-mysql local]# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql57 --datadir=/data/mysql/data_3306 [root@k8s-mysql local]# cd /usr/local/mysql57/support-files/ [root@k8s-mysql support-files]# ls magic mysqld_multi.server mysql-log-rotate mysql.server [root@k8s-mysql support-files]# cp mysql.server /etc/init.d/mysqld [root@k8s-mysql support-files]# chkconfig --add mysqld [root@k8s-mysql support-files]# systemctl start mysqld [root@k8s-mysql support-files]# systemctl enable mysqld mysqld.service is not a native service, redirecting to /sbin/chkconfig. Executing /sbin/chkconfig mysqld on [root@k8s-node1 support-files]# mysql mysql>
mysql操作(系统数据库中写的默认账号密码均为root)
创建账号密码 root root
mysqladmin -u root password "root"
4.安装数据库redis
[root@mysql ~]# yum install redis -y [root@mysql ~]# vim /etc/redis.conf ...... bind 192.168.1.123 # bind 127.0.0.1 #注释这行 [root@mysql ~]# systemctl restart redis [root@mysql ~]# systemctl enable redis
5.导入微服务系统数据
在mysql机器上创建存放数据的目录
[root@mysql ~]# mkdir pigdb
将代码中SQL数据推送至mysql服务器pigdb目录中
[root@jenkins pig-master]# scp -rp db/* root@192.168.1.123:/root/pigdb/
在mysql服务器导入数据
[root@mysql ~]# mysql -uroot -p123456 mysql> source /root/pigdb/pig_codegen.sql; mysql> source /root/pigdb/pig_config.sql; mysql> source /root/pigdb/pig_job.sql; mysql> source /root/pigdb/pig.sql; mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | pig | | pig_codegen | | pig_config | | pig_job | | sys | +--------------------+ 8 rows in set (0.00 sec)
6.启动nacos
配置host解析
现在在jenkins和应用服务器上加上解析,后面能用到。
[root@jenkins ~]# cat /etc/hosts 192.168.1.123 pig-mysql 192.168.1.123 pig-redis 127.0.0.1 pig-gateway 127.0.0.1 pig-register 127.0.0.1 pig-sentinel 127.0.0.1 pig-monitor 127.0.0.1 pig-job
启动nacos
[root@jenkins ~]# cd /root/pig/pig-master/pig-register/target [root@jenkins target]# nohup java -jar pig-register.jar & [root@jenkins target]# tail -f nohup.out #注意不要报错哦 : | \ | : ,--.--. ,---. / / | / / ' Pid: 23965 | : ' '; | / \ / \. ; ,. :| : /`./ Console: http://192.168.1.122:8848/nacos/index.html ' ' ;. ;.--. .-. | / / '' | |: :| : ;_ | | | \ | \__\/: . .. ' / ' | .; : \ \ `. https://nacos.io ' : | ; .' ," .--.; |' ; :__| : | `----. \ | | '`--' / / ,. |' | '.'|\ \ / / /`--' / ' : | ; : .' \ : : `----' '--'. / ; |.' | , .-./\ \ / `--'---' '---' `--`---' `----' lter@64c2b546, org.springframework.security.web.access.ExceptionTranslationFilter@560cbf1a] 2022-02-25 16:48:14.978 INFO 24105 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 16 endpoint(s) beneath base path '/actuator' 2022-02-25 16:48:15.040 INFO 24105 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8848 (http) with context path '/nacos' 2022-02-25 16:48:15.075 INFO 24105 --- [ main] c.a.n.c.l.StartingApplicationListener : Nacos started successfully in stand alone mode. use external storage #访问nacos IP:8848/nacos 账号密码都是nacos
7.启动pig-upms微服务
注意:一定要按照下面笔记顺序启动
启动pig-upms
[root@jenkins ~]# cd /root/pig/pig-master/pig-upms/pig-upms-biz/target [root@jenkins target]# nohup java -jar pig-upms-biz.jar &
查看日志没问题再进行下一步
[root@jenkins target]# tail -f nohup.out 2022-02-25 16:56:13.872 INFO 24266 --- [ main] io.undertow : starting server: Undertow - 2.2.14.Final 2022-02-25 16:56:13.885 INFO 24266 --- [ main] org.xnio : XNIO version 3.8.4.Final 2022-02-25 16:56:13.898 INFO 24266 --- [ main] org.xnio.nio : XNIO NIO Implementation Version 3.8.4.Final 2022-02-25 16:56:13.949 INFO 24266 --- [ main] org.jboss.threads : JBoss Threads version 3.1.0.Final 2022-02-25 16:56:14.019 INFO 24266 --- [ main] o.s.b.w.e.undertow.UndertowWebServer : Undertow started on port(s) 4000 (http) 2022-02-25 16:56:14.059 INFO 24266 --- [ main] c.a.c.n.registry.NacosServiceRegistry : nacos registry, DEFAULT_GROUP pig-upms-biz 192.168.1.122:4000 register finished 2022-02-25 16:56:14.979 INFO 24266 --- [ main] c.p.pig.admin.PigAdminApplication : Started PigAdminApplication in 24.288 seconds (JVM running for 25.105)
8.启动pig-auth
[root@jenkins ~]# cd pig/pig-master/pig-auth/target/ [root@jenkins target]# nohup java -jar pig-auth.jar & 2022-02-25 17:10:36.793 INFO 24782 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 20 endpoint(s) beneath base path '/actuator' 2022-02-25 17:10:36.926 INFO 24782 --- [ main] io.undertow : starting server: Undertow - 2.2.14.Final 2022-02-25 17:10:36.938 INFO 24782 --- [ main] org.xnio : XNIO version 3.8.4.Final 2022-02-25 17:10:36.949 INFO 24782 --- [ main] org.xnio.nio : XNIO NIO Implementation Version 3.8.4.Final 2022-02-25 17:10:37.014 INFO 24782 --- [ main] org.jboss.threads : JBoss Threads version 3.1.0.Final 2022-02-25 17:10:37.083 INFO 24782 --- [ main] o.s.b.w.e.undertow.UndertowWebServer : Undertow started on port(s) 3000 (http) 2022-02-25 17:10:37.096 INFO 24782 --- [ main] c.a.c.n.registry.NacosServiceRegistry : nacos registry, DEFAULT_GROUP pig-auth 192.168.1.122:3000 register finished 2022-02-25 17:10:37.126 INFO 24782 --- [ main] c.pig4cloud.pig.auth.PigAuthApplication : Started PigAuthApplication in 15.432 seconds (JVM running for 16.165)
9.启动pig-gateway
[root@jenkins ~]# cd pig/pig-master/pig-gateway/target/ [root@jenkins target]# nohup java -jar pig-gateway.jar & [root@jenkins target]# tail -f nohup.out 2022-02-25 17:12:28.427 INFO 24968 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Weight] 2022-02-25 17:12:28.427 INFO 24968 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [CloudFoundryRouteService] 2022-02-25 17:12:28.958 INFO 24968 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 20 endpoint(s) beneath base path '/actuator' 2022-02-25 17:12:29.069 INFO 24968 --- [ main] c.a.c.s.g.s.SentinelSCGAutoConfiguration : [Sentinel SpringCloudGateway] register SentinelGatewayBlockExceptionHandler 2022-02-25 17:12:29.478 INFO 24968 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port 9999 2022-02-25 17:12:29.519 INFO 24968 --- [ main] c.a.c.n.registry.NacosServiceRegistry : nacos registry, DEFAULT_GROUP pig-gateway 192.168.1.122:9999 register finished 2022-02-25 17:12:29.560 INFO 24968 --- [ main] c.p.pig.gateway.PigGatewayApplication : Started PigGatewayApplication in 14.3 seconds (JVM running for 16.373)
三个微服务部署完后可以在nacos上找到服务列表上,查看有没被注册
如图所示,三个服务都是正常的:
10.前端项目部署
所有的前端代码均需使用node进行编译打包
代码获取:https://github.com/pig-mesh/pig-ui
将下载的代码上传到服务器:
[root@jenkins ~]# mkdir pig-web && cd pig-web [root@jenkins pig-web]# ls pig-ui-master.zip [root@jenkins pig-web]# unzip pig-ui-master.zip
配置nodejs环境
[root@jenkins ~]# cd /export/soft/ [root@jenkins soft]# wget https://nodejs.org/dist/v12.18.1/node-v12.18.1-linux-x64.tar.xz [root@jenkins soft]# tar xf node-v12.18.1-linux-x64.tar.xz [root@jenkins soft]# mv node-v12.18.1-linux-x64 /usr/local/node [root@jenkins soft]# vi /etc/profile ......最后加...... export PATH=$PATH:/usr/local/node/bin #使环境变量失效 [root@kjenkins ~]# source /etc/profile [root@jenkins ~]# cd /usr/local/node/bin #查看版本(当前位置生效) [root@jenkins bin]# ./node -v v12.18.1 #配软连接: 相当于全局变量,在任何文件夹都能查看版本信息 ln -s /usr/local/node/bin/node /usr/local/bin/ ln -s /usr/local/node/bin/npm /usr/local/bin/ #测试全局是否生效 [root@jenkins bin]# cd [root@jenkins ~]# node -v v12.18.1
编译打包前端代码
[root@jenkins ~]# cd /root/pig-web/pig-ui-master [root@jenkins pig-ui-master]# npm install -g yarn [root@jenkins pig-ui-master]# yarn install [root@jenkins pig-ui-master]# yarn run build [root@jenkins pig-ui-master]# ls babel.config.js dist docker jsconfig.json LICENSE node_modules package.json public README.md src vue.config.js yarn.lock
11.配置nginx
简易安装一下:
先配置一个yum源
vim /etc/yum.repos.d/nginx.repo [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true yum install -y yum-utils yum-config-manager --enable nginx-mainline yum install -y nginx [root@jenkins pig-ui-master]# yum install nginx -y [root@jenkins pig-ui-master]# cd /etc/nginx/conf.d/ [root@jenkins conf.d]# rm -f default.conf [root@jenkins conf.d]# vim laoyan.com.conf server { listen 80; gzip on; gzip_static on; # 需要http_gzip_static_module 模块 gzip_min_length 1k; gzip_comp_level 4; gzip_proxied any; gzip_types text/plain text/xml text/css; gzip_vary on; gzip_disable "MSIE [1-6]\.(?!.*SV1)"; location / { root /root/pig-web/pig-ui-master/dist; index index.html; } # 注意维护新增微服务,gateway 路由前缀 location ~* ^/(code|act|auth|admin|gen|monitor|tx|job|damon|pay|mp) { proxy_pass http://pig-gateway:9999; proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto $scheme; add_header Cache-Control no-store; proxy_connect_timeout 15s; proxy_send_timeout 15s; proxy_read_timeout 15s; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto http; } }
修改权限,权限问题有可能会403,先改下用户为root
[root@jenkins target]# vim /etc/nginx/nginx.conf user root;
启动nginx:
[root@jenkins conf.d]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@jenkins conf.d]# systemctl start nginx [root@jenkins conf.d]# systemctl enable nginx [root@jenkins conf.d]# nginx -s reload
直接在部署服务器上IP地址访问:如果验证码不显示:
[root@mysql pigdb]# vim /etc/redis.conf #注释掉下面这一行 # bind 127.0.0.1
前端输入IP进行访问
登录正常,整个项目就部署好了
三.jenkins脚本自动部署(本地)
承接上面的类容,发现手动部署比较麻烦。
传统手动上线存在的缺点
1、全程运维进行,占用大量时间。
2、如果节点多,上线速度慢。
3、人为失误多,目录管理混乱。
4、回滚不及时,或者难以回退。
5、满足不了系统可靠性9999的要求
1.Gitlab服务器设置
由于示例,现在gialab服务器上创建2个前后端项目
创建两个项目后端pig-java,前端pig-web
在Gitlab服务器上配置hosts解析
[root@gitlab ~]# vim /etc/hosts ........ 192.168.1.100 gitlab.laoyan.com
配置公钥至Gitlab服务器
[root@gitlab ~]# ssh-keygen #一路回车 [root@gitlab ~]# cat ~/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCcj7SZW2OUiHYrggUE6P5gdPMxikKwva54rnTpWPcRZA4z3CjvnFOpitIVZXqLlpz53BqWYOcdPDMTRSNDDCLX2kGPCqNUcVHjzze8wgw7JC0fgY3lNiV/Lmhug9f7P4yV3q/lbZTFBDwAnEoftSCkZwgW1gQOBE3YbElJ5kYBswQ5rWstkU3mRKFnRrZLLd8aTDYWVhcyIPrXUTF4zcGhuP4kl5bZrz2o1r+FVkqsF5flqMhHtFFpaLKIGFEWYLvoiZDSFfv65hIBrEP76m04PWde3se/2S84j1jKi2ETV6obYuNho09bkvTQCHQhFs7Q0Ics4l5ORZhuf7eFKYb1 root@gitlab
提交后端代码至pig-Java仓库
#1.后端Java开发提交代码到私有仓库Gitlab # 在gitlab新建一个代码库pig-java并clone下来 [root@gitlab ~]# git clone git@gitlab.laoyan.com:root/pig-java.git [root@gitlab ~]# cd pig-java/ [root@gitlab pig-java]# ls README.md #2.将后端代码上传至服务器并解压 [root@gitlab ~]# ls pig-java pig-master.zip [root@gitlab ~]# unzip pig-master.zip && ls pig-master pig-java pig-master.zip #3.将解压好的代码复制到pig-Java目录中 [root@gitlab ~]# mv pig-master/* pig-java/ [root@gitlab ~]# cd pig-java/ [root@gitlab pig-java]# ls db LICENSE pig-common pig-register pig-visual README.md docker-compose.yml pig-auth pig-gateway pig-upms pom.xml #4.将开发好的代码提交到master分支 #添加参数并提交代码 [root@gitlab pig-java]# git config --global user.email "root@163.com" [root@gitlab pig-java]# git config --global user.name "laoyan" [root@gitlab pig-java]# git add . [root@gitlab pig-java]# git commit -m "这是老严的后端微服务项目源代码" #5.在gitlab新建一个master分支,将代码合并到master分支 [root@gitlab pig-java]# git branch master [root@gitlab pig-java]# git checkout master Switched to branch 'master' [root@gitlab pig-java]# git push origin master Counting objects: 1265, done. Compressing objects: 100% (1083/1083), done. Writing objects: 100% (1263/1263), 5.37 MiB | 2.36 MiB/s, done. Total 1263 (delta 261), reused 0 (delta 0) remote: Resolving deltas: 100% (261/261), done. remote: remote: To create a merge request for master, visit: remote: http://gitlab.laoyan.com/root/pig-java/-/merge_requests/new?merge_request%5Bsource_branch%5D=master remote: To git@gitlab.laoyan.com:root/pig-java.git * [new branch] master -> master
提交前端代码至pig-web仓库
#1.前端web开发提交代码到私有仓库Gitlab # 在gitlab新建一个代码库pig-web并clone下来 [root@gitlab ~]# git clone git@gitlab.laoyan.com:root/pig-web.git #2.上传前端代码包并解压 [root@gitlab ~]# unzip pig-ui-master.zip [root@gitlab ~]# mv pig-ui-master/* pig-web/ [root@gitlab ~]# cd pig-web/ [root@gitlab pig-web]# ls babel.config.js docker jsconfig.json LICENSE package.json public README.md src vue.config.js [root@gitlab pig-web]# git add . [root@gitlab pig-web]# git commit -m "这是老王编写的前端代码" #3.在gitlab新建一个master分支,将代码合并到master分支 [root@gitlab pig-web]# git branch master [root@gitlab pig-web]# git checkout master Switched to branch 'master' [root@gitlab pig-web]# git push origin master Counting objects: 171, done. Compressing objects: 100% (146/146), done. Writing objects: 100% (169/169), 127.93 KiB | 0 bytes/s, done. Total 169 (delta 24), reused 0 (delta 0) remote: Resolving deltas: 100% (24/24), done. remote: remote: To create a merge request for master, visit: remote: http://gitlab.laoyan.com/root/pig-web/-/merge_requests/new?merge_request%5Bsource_branch%5D=master remote: To git@gitlab.laoyan.com:root/pig-web.git * [new branch] master -> master
2.Jenkins集成Gitlab
将jenkins服务器的公钥放到gitlub上面
[root@jenkins ~]# ssh-keygen [root@jenkins ~]# cat ~/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDTuEBgv0vLeCRn90KkYvGSI3ss+1ntwbiZpLly86xYGxYcsnNpRp+t6VUbk2PXN2ql/H+ceOha5lu/qadb/eyNxlh6pE1/hmG30sTEWb0QMoc7dq3XStAERZZmyjG43Tk/a2itrVNJB1Jh14c09+NnHxR7tz8Dq49YCdRnu6xxVYH4U+TloEL8Luxto5nFM7dht7Gfrf+Zop4ItF5DJyi7NJ5N3ICqxQM9In2wttcK0H5PuG7gwYopTATNU3SmHeqVP+jmF69GDt8gd1D5geVl3JQHMMtn72uut8ESRTuUzxpzf9hVrx3s2U8q+myy55/y86Oa7SXi6leBCAKe4k8D root@jenkins
jenkins服务器上配置host解析
[root@jenkins ~]# vim /etc/hosts ........ 192.168.1.100 gitlab.laoyan.com 查看Jenkins私钥 [root@jenkins ~]# cat ~/.ssh/id_rsa -----BEGIN RSA PRIVATE KEY----- MIIEowIBAAKCAQEA07hAYL9Ly3gkZ/dCpGLxkiN7LPtZ7cG4maS5cvOsWBsWHLJz aUafrelVG5Nj1zdqpfx/nHjoWuZbv6mnW/3sjcZYeqRNf4Zht9LExFm9EDKHO3at 10rQBEWWZsoxuN05P2tora1TSQdSYdeHNPfjZx8Ue7c/A6uPWAnUZ7uscVWB+FPk 5aBC/C7sbaOZxTO3Ybexn63/maKeCLReQycouzSeTdyAqsUDPSJ9sLbXCtB+T7hu 4MGKKUwEzVN0ph3qlT/o5hevRg7fIHdQ+YHlZdyUBzDLZ+9rrrfBEkU7lM8ac3/Y Va8d7NlPKvpssuef8vOjmu0l4upXgQgCnuJPAwIDAQABAoIBAQCQwqhx/hpK8sih tXSBhY16yHvOE8Y3RH99vh12tZmfXFXQJUu4Lau8c4/XnP1lB+UJvQ1b5Wepz1vo Po3J3mLBxV517KV4jl9+GsSjJGCbFJFWXR0b2XFFmnbtrU42AjzEWiJmrd4GTtVL RNyToPKBbcTJmk+hqMoH/iCXFMEhc4FeXocHuCIdIF2DsCZchO7pA2LBvVyphFMG Vba8pRhlfg42qDtq/HNXT4/T47jpuXpLr4wVN9bHAbAeheDIjHoVV1YIrTHyl3ua FuxZbmB5vpoedxdy+7Vu9VSWeJf0x9QPumj8ZG6mNEuhBSU2UOKqot3YzAWmvs8L ei6qG0NxAoGBAOmWU+9ddch9bNISZwVLcn7q821qk2Ow7Rcf5Tys6ebqErDO4mnj zn6Yab/f76kre9NZEb8hvhnGC4xepxVlgw+jEOnEoFqrdDbgju15SmwBHf0cWEze Y4Y1+BaMXkLwUBumCsDKpybFVfqbtjkGBcJXwBV7I4iG+J9WD9xyfOTlAoGBAOgI yTtdx7lvFDfV0SEgSBi5BkOywVXRGOhNEXbs/35WUrUDWzLDlORwyQRc6Mo6rfZJ 4RA5HMJUjikz/ULGuk63K+daZR0kqwG6sFSCBJJhrRjZFS2p2kbIhVPS066DnPvb cK1s0Ad0pQm8CmKGa/di1ukFJJAxJrEPKa4bbc3HAoGAcGo8ctITwS07lc3tbork y1ffVyUXUb7wG1TqSc0dtFeqLxUhubw8wIgxPHpVKKEYspoHBGkEx/qbzC4xUwbL kFb16MsMMRaak1526qqYumm/OPTPOg887nZCsvdS1fw5UVUTj7z3vJzzx+vwQnk3 hFQeyTe93hK2zFfqwjkHClECgYAL1ubtsItQWuqdLrvNBOio2kgHjEdxKYor0ELj 3pPqzlzJ/7HXlUMzpM2c+2ClcPkPaBWjzkY5ZDccwxpZHerDuXT0CRi/s+9F11DH /xpcnb9HVUxPWCtbEUA8VrWWYgDQmRHWx4aV2s3RAVdGT7WSmeh9W1XSRa/DuRJX X+7qYQKBgBOaCL+0XQlekIgdZbv9zNOvdDMMaTlk98XyLAoJXO4MlExdjp5pmNMY Dt82+qsERy0n49TLl3C41q5mHU1NScoMeSxZoUHWgexXWKcCTl5y6FdUf280hr7j Xyyb3MJpGtXCKal0Pr/JzzH2KH+0GG9UsiAWMqM/EVmVBykOq1XN -----END RSA PRIVATE KEY-----
在Jenkins上面添加凭据
3.创建测试脚本
创建一个test-Java的项目
选择脚本输出测试
echo "123" >> 123.txt sleep 5
点击立即构建—–>查看结果
在Jenkins目录中查看项目所在路径
[root@jenkins ~]# cd .jenkins/workspace/ [root@jenkins workspace]# ls test-java [root@jenkins workspace]# cd test-java/ [root@jenkins test-java]# ls 123.txt [root@jenkins test-java]# cat 123.txt 123 [root@jenkins test-java]# pwd /root/.jenkins/workspace/test-java
4.脚本式自动化发布
jenkins需要安装基本插件Git Parameter、GitLab、Blue Ocean Core JS、Blue Ocean
加速设置:设置——->插件管理———>高级
国内加速源: https://mirror.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
5.配置nacos注册中心的发布
由于我安装Jenkins版本的问题,我需要停掉Jenkins再进行启动。。
[root@jenkins test-java]# ps aux |grep java root 1554 8.1 6.9 3806448 461724 pts/0 Tl 14:00 5:57 java -jar jenkins.war --httpPort=28088 [root@jenkins test-java]# kill -9 1554 [root@jenkins soft]# java -jar jenkins.war
创建一个项目名称为pig-register的项目
添加Gitlab代码仓库地址
脚本启动方式
[root@jenkins ~]# mkdir pig-jenkins-sh && cd pig-jenkins-sh [root@jenkins pig-jenkins-sh]# vim pig-register.sh [root@jenkins pig-jenkins-sh]# cat pig-register.sh [root@jenkins pig-jenkins-sh]# cat pig-register.sh #!/bin/bash BUILD_ID=DONTKILLME qi=`date +'%Y%m%d%H%M'` node=192.168.0.124 #设置应用服务器主机IP project=pig-register #项目名称,为gitlab上项目名称,最好不要改别的,代码冲突 address=/opt/$project #目标端代码仓库位置,演示设置/opt为代码仓库位置,实际按需设置 runningpath=/www/wwwroot/$project #程序运行路径,目标仓库代码会链接到运行项目路径上 #cd /root/.jenkins/workspace/$project #mvn clean install -Dmaven.skip.test=true #编译代码 tar -zcvf /opt/$project-$qi.tar.gz -C /root/.jenkins/workspace $project ssh root@$node "mkdir -p $address" #创建目标仓库目录 scp /opt/$project-$qi.tar.gz root@$node:$address #把编译后压缩包发送到目标应用主机的代码仓库上 rm -rf /opt/$project-$qi.tar.gz #清理压缩包 ssh root@$node "tar -zxf $address/$project-$qi.tar.gz -C $address;mkdir -p $address/$project$qi" #解压代码到目标仓库 ssh root@$node "mv $address/$project $address/$project$qi; ln -snf $address/$project$qi/$project/ $runningpath" #链接代码目录到程序运行目录,设置/www/wwwroot/pig-register为运行目录 ssh root@$node "ps -ef |grep $project.jar |cut -c 9-15 |head -n1 |xargs kill -9" #杀死旧进程 ssh root@$node "ls -t $address |awk 'NR>3' |xargs rm -rf" #为了节省空间,保留最新3个版本的文件(根据需求) ssh root@$node "find $address -type d -maxdepth 1 | grep '$address/' | sort -nr |awk 'NR>3' | xargs rm -rf" #保留最新3个目录 ssh root@$node "cd $runningpath/$project/target;nohup java -jar $project.jar &" #运行新进程 Jenkins脚本配置路径 sh +x /root/pig-jenkins-sh/pig-register.sh
Jenkins点击构建进行项目构建
6.其它微服务构建的发布
和上面基本差不多,改下项目名称和仓库位置就行了
7.前端项目构建
脚本:
cd $WORKSPACE/ && yarn install && yarn run build #编译 cp -rp dist/* /root/pig-web/pig-ui-master/dist/ #复制编译后的文件到nginx的访问目录(根据自己的设置) nginx -s reload
#重启nginx
8.分支构建
安装jenkins插件:List Git branches
如优化之前的pig-gateway
将其它的分支按照上面的方式修改下