jenkins+svn 自动化上线
一.环境:
[root@bimvm01 ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) [root@bimvm01 ~]# uname -a Linux bimvm01 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux Jenkins 版本 jenkins-2.118-1.1 Jdk版本1.8.0 java-1.8.0-openjdk.x86_64
二.安装
1.安装epel源
rpm -ivh https://mirrors.kernel.org/fedora-epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm
2.安装jenkins
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key yum install jenkins -y
3.安装jdk
yum install java-1.8.0-openjdk.x86_64 -y
4.安装svn 客户端
yum install -y subversion
查看svn命令帮助:
svn help
5.启动jenkins
/etc/init.d/jenkins restart
或
java -jar /usr/lib/jenkins/jenkins.war
检查进程是否存在,端口是否开启,不想用8080的,也可以自己换个端口
6.关闭防火墙
或者配置开放端口(我懒得配置直接关闭了)
systemctl stop firewalld.service systemctl disable firewalld.service or(开放端口) firewall-cmd --permanent --add-port=8080/tcp
7.测试jenkins
然后在浏览器上输入
http://192.168.1.170:8080
8.输入密码
[root@bimvm01 ~]# cat /var/lib/jenkins/secrets/initialAdminPassword b6a6666602f34ab4866f9cdf7e5082d9
9.安装插件
我是新手我选择了第一个(安装推荐的插件)
大概需要3-5分钟
10.创建用户
(你可以继续使用admin也可以创建新的用户)
11.jenkins 安装完成
12.安装maven
后边打包后端需要
mkdir /home/maven cd /home/maven #上传maven包 tar -xvf apache-maven-3.6.0-bin.tar.gz mv apache-maven-3.6.0 maven vim /etc/profile ##增加以下内容 export MAVEN_HOME=/home/maven/maven export MAVEN_HOME export PATH=${PATH}:${MAVEN_HOME}/bin ##重新刷新配置文件 source /etc/profile ##测试安装 [root@bimvm01 maven]# mvn -version Apache Maven 3.0.5 (Red Hat 3.0.5-17) Maven home: /usr/share/maven Java version: 1.8.0_201, vendor: Oracle Corporation Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/jre Default locale: zh_CN, platform encoding: UTF-8 OS name: "linux", version: "3.10.0-957.el7.x86_64", arch: "amd64", family: "unix"
13.在jenkins中配置jdk和maven
系统管理--全局配置
14.插件管理中下载Publish over SSH插件
系统管理--系统设置
15创建一个新项目
16.配置该任务
源码管理svn配置
17.构建触发器
18.构建执行shell
#!/bin/bash ########################## ## 建威CMS ########################## now_time=$(date +"%Y%m%d_%H%M%S") cd /home/bim/cms tar -zcvf cms_back_${now_time}.tar.gz * mv cms_back_* /home/bim/back/cmsback systemctl reload nginx
注:1
如果有需要打包的项目的话执行的shell
要先下载npm
yum install npm -y #!/bin/bash -lie #sh /home/bim/back/jianweioa.sh #!/bin/bash ########################## ## 建威OA ########################## now_time=$(date +"%Y%m%d_%H%M%S") cd /home/bim/jianweioa #切换至构建目录 node -v npm -v #检查编译环 cp -r /home/bim/back/jianweioaback/index.js /home/bim/jianweioa/config/ cp -r /home/node_modules /home/bim/jianweioa/ rm -rf /home/bim/jianweioa/src/api/service.js cp /home/bim/back/jianweioaback/api/service.js /home/bim/jianweioa/src/api #npm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/dist/chromedriver #npm install #npm install wangeditor #npm install webpack #npm i #npm install http-server -g npm run build #编译项目 cd dist tar -zcvf jwoa_${now_time}.tar.gz * #打包备份 mv jwoa_* /home/bim/back/jianweioaback rm -rf /home/bim/web/jianweioa/* mv /home/bim/jianweioa/dist/* /home/bim/web/jianweioa rm -rf /home/bim/jianweioa/* systemctl restart nginx
注:2
遇到的坑
刚开始构建打包的时候npm 一直报错
后来查了资料发现开发这个魔鬼没有吧包上传完整config 文件里边缺少index.js文件导致的
这边打包会遇到很多问题 多跟开发交流
遇到的坑
再后来执行shell 发现报错起不来nginx 是因为nginx 1024内的端口只能root 启动
解决的办法是用root 用户启动Jenks
1.修改Jenkins配置文件
# 打开配置文件 vim /etc/sysconfig/jenkins # 修改$JENKINS_USER,并去掉当前行注释 $JENKINS_USER="root"
2.修改Jenkins相关文件夹用户权限
chown -R root:root /var/lib/jenkins chown -R root:root /var/cache/jenkins chown -R root:root /var/log/jenkins
3.重启Jenkins服务并检查运行Jenkins的用户是否已经切换为root
# 重启Jenkins(若是其他方式安装的jenkins则重启方式略不同) systemctl restart jenkins # 查看Jenkins进程所属用户 ps -ef | grep jenkins # 若显示为root用户,则表示修改完成 #!/bin/bash ########################## ## 建威OA ## ########################## now_time=$(date +"%Y%m%d_%H%M%S") cp /home/bim/back/jianweioaback/index.js /home/bim/jianweioa/config/ cd /home/bim/jianweioa #切换至构建目录 node -v npm -v #检查编译环 npm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/dist/chromedriver npm install npm install wangeditor npm install webpack npm i npm run build #编译项目 cd dist tar -zcvf jwoa_${now_time}.tar.gz * #打包备份 mv jwoa_* /home/bim/back/jianweioaback #rm -rf /home/bim/web/jianweioa/* mv /home/bim/jianweioa/dist/* /home/bim/web/jianweioa ls /home/bim/jianweioa | grep -v node_modules | xargs -i rm -rf {} systemctl restart nginx
以上为前端web 发布
点击构建输入
由用户 xuewenlong 启动 Running as SYSTEM 构建中 在工作空间 /home/bim/jianweioa 中 Updating https://192.168.1.200/svn/TestSVN/web/jianweioa at revision '2019-04-30T10:03:53.357 +0800' --quiet Using sole credentials yulinjie/****** (svn) in realm ‘<https://192.168.1.200:443> VisualSVN Server’ At revision 1312 Using sole credentials yulinjie/****** (svn) in realm ‘<https://192.168.1.200:443> VisualSVN Server’ [jianweioa] $ /bin/bash -lie /tmp/jenkins7296205467554102448.sh bash: 此 shell 中无任务控制 v10.15.3 6.4.1 > jianweioa@1.0.0 build /home/bim/jianweioa > node build/build.js Hash: [1m178a8e8725ee622ab067[39m[22m Version: webpack [1m3.12.0[39m[22m Time: [1m72413[39m[22mms [1mAsset[39m[22m [1mSize[39m[22m [1mChunks[39m[22m [1m[39m[22m [1m[39m[22m [1mChunk Names[39m[22m [1m[32mstatic/imgs/.svn/prop-base/background.png.svn-base[39m[22m 131 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/img/asidebg.c3cf0b7.png[39m[22m 48 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/fonts/OAIcons.964a65d.woff[39m[22m 17.4 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/fonts/OAIcons.32db094.eot[39m[22m 17.5 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/fonts/OAIcons.93901bf.ttf[39m[22m 17.3 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/img/OAIcons.7c830dd.svg[39m[22m 61.9 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/fonts/element-icons.6f0a763.ttf[39m[22m 11 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/img/homelogo.3ba8e09.png[39m[22m 12.1 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[33mstatic/js/vendor.71c0e42d72cbb3a0195a.js[39m[22m [1m[33m826 kB[39m[22m [1m0[39m[22m [1m[32m[emitted][39m[22m [1m[33m[big][39m[22m vendor [1m[33mstatic/js/app.cdb168323639ec8ea899.js[39m[22m [1m[33m358 kB[39m[22m [1m1[39m[22m [1m[32m[emitted][39m[22m [1m[33m[big][39m[22m app [1m[32mstatic/js/manifest.2ae2e69a05c33dfc65f8.js[39m[22m 857 bytes [1m2[39m[22m [1m[32m[emitted][39m[22m manifest [1m[33mstatic/css/app.7892718fc7fe44c4f918da18a9c22625.css[39m[22m [1m[33m291 kB[39m[22m [1m1[39m[22m [1m[32m[emitted][39m[22m [1m[33m[big][39m[22m app [1m[32mstatic/css/app.7892718fc7fe44c4f918da18a9c22625.css.map[39m[22m 456 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/js/vendor.71c0e42d72cbb3a0195a.js.map[39m[22m 3.25 MB [1m0[39m[22m [1m[32m[emitted][39m[22m vendor [1m[32mstatic/js/app.cdb168323639ec8ea899.js.map[39m[22m 1.31 MB [1m1[39m[22m [1m[32m[emitted][39m[22m app [1m[32mstatic/js/manifest.2ae2e69a05c33dfc65f8.js.map[39m[22m 4.97 kB [1m2[39m[22m [1m[32m[emitted][39m[22m manifest [1m[32mindex.html[39m[22m 574 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/asidebg.png[39m[22m 48 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/asidelogo.png[39m[22m 4.57 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/background.png[39m[22m 203 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/homelogo.png[39m[22m 12.1 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/logo.png[39m[22m 4.44 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/.svn/all-wcprops[39m[22m 306 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/.svn/entries[39m[22m 517 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/.svn/format[39m[22m 2 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/.svn/prop-base/favicon.ico.svn-base[39m[22m 131 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/all-wcprops[39m[22m 1.62 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/entries[39m[22m 2.02 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/format[39m[22m 2 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/prop-base/asidebg.png.svn-base[39m[22m 131 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/prop-base/asidelogo.png.svn-base[39m[22m 131 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/prop-base/avatar.png.svn-base[39m[22m 131 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/img/background.de3cb54.png[39m[22m 203 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/prop-base/homelogo.png.svn-base[39m[22m 131 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/prop-base/logo.png.svn-base[39m[22m 131 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/prop-base/mail.png.svn-base[39m[22m 131 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/prop-base/newapply.png.svn-base[39m[22m 131 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/prop-base/newimgs.png.svn-base[39m[22m 131 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/prop-base/phone.png.svn-base[39m[22m 131 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/prop-base/share_qq.png.svn-base[39m[22m 131 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/prop-base/share_weixin.png.svn-base[39m[22m 131 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/prop-base/testimg.jpeg.svn-base[39m[22m 131 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/text-base/asidelogo.png.svn-base[39m[22m 4.57 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/text-base/avatar.png.svn-base[39m[22m 2.71 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/text-base/logo.png.svn-base[39m[22m 4.44 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/text-base/mail.png.svn-base[39m[22m 356 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/text-base/newapply.png.svn-base[39m[22m 5.67 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/text-base/phone.png.svn-base[39m[22m 387 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/text-base/share_qq.png.svn-base[39m[22m 591 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/text-base/share_weixin.png.svn-base[39m[22m 605 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/avatar.png[39m[22m 2.71 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/mail.png[39m[22m 356 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/newapply.png[39m[22m 5.67 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/phone.png[39m[22m 387 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/share_qq.png[39m[22m 591 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/share_weixin.png[39m[22m 605 bytes [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/text-base/homelogo.png.svn-base[39m[22m 12.1 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/.svn/text-base/favicon.ico.svn-base[39m[22m 17 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/favicon.ico[39m[22m 17 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/text-base/testimg.jpeg.svn-base[39m[22m 37.2 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/testimg.jpeg[39m[22m 37.2 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/text-base/asidebg.png.svn-base[39m[22m 48 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/text-base/newimgs.png.svn-base[39m[22m 68.6 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/newimgs.png[39m[22m 68.6 kB [1m[39m[22m [1m[32m[emitted][39m[22m [1m[32mstatic/imgs/.svn/text-base/background.png.svn-base[39m[22m 203 kB [1m[39m[22m [1m[32m[emitted][39m[22m Build complete. Tip: built files are meant to be served over an HTTP server. Opening index.html over file:// won't work. index.html static/ static/imgs/ static/imgs/.svn/ static/imgs/.svn/prop-base/ static/imgs/.svn/prop-base/asidebg.png.svn-base static/imgs/.svn/prop-base/asidelogo.png.svn-base static/imgs/.svn/prop-base/avatar.png.svn-base static/imgs/.svn/prop-base/background.png.svn-base static/imgs/.svn/prop-base/homelogo.png.svn-base static/imgs/.svn/prop-base/logo.png.svn-base static/imgs/.svn/prop-base/mail.png.svn-base static/imgs/.svn/prop-base/newapply.png.svn-base static/imgs/.svn/prop-base/newimgs.png.svn-base static/imgs/.svn/prop-base/phone.png.svn-base static/imgs/.svn/prop-base/share_qq.png.svn-base static/imgs/.svn/prop-base/share_weixin.png.svn-base static/imgs/.svn/prop-base/testimg.jpeg.svn-base static/imgs/.svn/text-base/ static/imgs/.svn/text-base/asidelogo.png.svn-base static/imgs/.svn/text-base/avatar.png.svn-base static/imgs/.svn/text-base/logo.png.svn-base static/imgs/.svn/text-base/mail.png.svn-base static/imgs/.svn/text-base/newapply.png.svn-base static/imgs/.svn/text-base/phone.png.svn-base static/imgs/.svn/text-base/share_qq.png.svn-base static/imgs/.svn/text-base/share_weixin.png.svn-base static/imgs/.svn/text-base/homelogo.png.svn-base static/imgs/.svn/text-base/testimg.jpeg.svn-base static/imgs/.svn/text-base/asidebg.png.svn-base static/imgs/.svn/text-base/newimgs.png.svn-base static/imgs/.svn/text-base/background.png.svn-base static/imgs/.svn/all-wcprops static/imgs/.svn/entries static/imgs/.svn/format static/imgs/testimg.jpeg static/imgs/newimgs.png static/imgs/asidebg.png static/imgs/asidelogo.png static/imgs/background.png static/imgs/homelogo.png static/imgs/logo.png static/imgs/avatar.png static/imgs/mail.png static/imgs/newapply.png static/imgs/phone.png static/imgs/share_qq.png static/imgs/share_weixin.png static/.svn/ static/.svn/prop-base/ static/.svn/prop-base/favicon.ico.svn-base static/.svn/text-base/ static/.svn/text-base/favicon.ico.svn-base static/.svn/all-wcprops static/.svn/entries static/.svn/format static/favicon.ico static/img/ static/img/asidebg.c3cf0b7.png static/img/background.de3cb54.png static/img/OAIcons.7c830dd.svg static/img/homelogo.3ba8e09.png static/fonts/ static/fonts/OAIcons.964a65d.woff static/fonts/OAIcons.32db094.eot static/fonts/OAIcons.93901bf.ttf static/fonts/element-icons.6f0a763.ttf static/js/ static/js/vendor.71c0e42d72cbb3a0195a.js static/js/app.cdb168323639ec8ea899.js static/js/manifest.2ae2e69a05c33dfc65f8.js static/js/vendor.71c0e42d72cbb3a0195a.js.map static/js/app.cdb168323639ec8ea899.js.map static/js/manifest.2ae2e69a05c33dfc65f8.js.map static/css/ static/css/app.7892718fc7fe44c4f918da18a9c22625.css static/css/app.7892718fc7fe44c4f918da18a9c22625.css.map Finished: SUCCESS
后台war包自动打包发布
安装插件
jdk tomcat Maven
安装插件maven 插件
安装Deploy to container Plugin插件
jenkins 配置
新建maven项目
脚本只能关闭tomcat 不能启动tomcat
还没能处理
#!/bin/bash -lie #!/bim/bash now_time=$(date +"%Y%m%d_%H%M%S") rm -rf /home/tomcat/apache-tomcat-9.0.10/webapps/oa-0.0.1-SNAPSHOT* cp /var/lib/jenkins/workspace/jwoawar/target/oa-0.0.1-SNAPSHOT.war /home/tomcat/apache-tomcat-9.0.102/webapps/ cp /var/lib/jenkins/workspace/jwoawar/target/oa-0.0.1-SNAPSHOT.war /home/bim/data/jianweioa/oa-0.0.1-SNAPSHOT-${now_time}.war #rm -rf /var/lib/jenkins/workspace/jwoawarwar/* ##!/bin/bash -lie #sh +x /home/tomcat/apache-tomcat-9.0.10/bin/shutdown.sh ##!/bin/bash -lie #sh +x /home/tomcat/apache-tomcat-9.0.10/bin/startup.sh
tomcat监控自启脚本
#!/bin/sh # 获取tomcat进程ID(其中[grep -w 'tomcat']代码中的tomcat需要替换为你的tomcat文件夹名) TomcatID=$(ps -C java u --cols=500|grep /home/tomcat | awk '{print $2}') # tomcat启动程序(这里注意tomcat实际安装的路径) StartTomcat=/home/tomcat/apache-tomcat-9.0.10/bin/startup.sh TomcatCache=/home/apache-tomcat-9.0.10/work #定义要监控的页面地址 WebUrl=http://localhost:8080 #日志输出 GetPageInfo=/tmp/TomcatMonitor.Info TomcatMonitorLog=/tmp/TomcatMonitor.log Monitor() { echo "[info]开始监控tomcat...[$(date +'%F %H:%M:%S')]" if [ $TomcatID ];then #这里判断Tomcat进程是否存在 echo "[info]当前tomcat进程ID为:$TomcatID,继续检测页面..." # 检测是否启动成功(成功的话页面会返回状态"200") TomcatServiceCode=$(curl -s -o $GetPageInfo -m 10 --connect-timeout 10 $WebUrl -w %{http_code}) if [ $TomcatServiceCode -eq 200 ];then echo "[info]页面返回码为$TomcatServiceCode,tomcat启动成功,测试页面正常" else echo "[error]tomcat页面出错,请注意...状态码为$TomcatServiceCode,错误日志已输出到$GetPageInfo" echo "[error]页面访问出错,开始重启tomcat" kill -9 $TomcatID # 杀掉原tomcat进程 sleep 3 rm -rf $TomcatCache # 清理tomcat缓存 $StartTomcat fi else echo "[error]tomcat进程不存在!tomcat开始自动重启..." echo "[info]$StartTomcat,请稍候..." rm -rf $TomcatCache $StartTomcat fi echo "--------------------------" } Monitor>>$TomcatMonitorLog
中间有一些坑 需要自己去学习处理下
git 的话应该也差不多 没用git 也没测试