Jenkins, VUE, Tomcat, Gitee, Linux自动部署

Jenkins, VUE, Tomcat, Gitee, Linux自动部署

相关软件版本号:

  1. Jenkins: 2.190.1
  2. Java : openjdk version "1.8.0_232"
  3. Node: 12.13.0
  4. NPM: 6.12.0
  5. Tomcat: apache-tomcat-8.5.40
  6. VUE-CLI3
  7. Linux: CentOS Linux release 7.7.1908 (Core)
  8. Git: gitee.com (https://gitee.com/help/articles/4193)

作者的Jenkins就安装在测试服务器上,跟tomcat在同一个服务器,所以没有用Add post-build action->Send build artifacts over SSH

 

步骤:

1. 创建一个Job(具体参数都用默认即可)

2. 源码管理及配置

2.1 配置Gitee.com上源码的地址,以及登录信息 ,具体可参考 https://gitee.com/help/articles/4193

2.2 Additional Behaviours: 选择Clean before checkout,每次获取源码前先清理一下,以免有缓存导致有些文件未更新

2.3 Branch,获取源代码时,具体从哪个分支获取,如: origin/master, origin/develop

3. 构建触发机制

默认既可以,作者用来发布开发环境,或者直接用 Push Events 去触发 origin/develop分支


4.构建编译环境

Node JS选择自己配置的Node js,其他参数不变

 

配置Node Js: jenkins->manage jenkins->Global Tool Configuration,没有Node Js选项的话,去安装Node JS插件

 

5. 构建(编辑并发布)

Node JS任务的构建,直接添加 Add Build Step-> Execute shell,作者的发布环境用了4个 Execute Shell

5.1 第一个Execute Shell

5.1.1. 进入到jenkins下VUE项目,注意项目名称plsweb7777是 Jenkins任务的名称,不是Git上项目的名称

5.1.2. 删除build出来的dist文件

5.1.3. 执行 npm install, npm run build

echo "start to build....."

cd /var/lib/jenkins/workspace/plsweb7777

source /etc/profile

rm -rf dist
echo "Deleted dist at /var/lib/jenkins/workspace/plsweb7777/dist....."

echo "Begin to execute npm install....."

# npm config set user 0 
# npm config set unsafe-perm true 
# npm audit fix --force


npm install
echo "Executed npm install....."

echo "Begin to execute npm run build:prod"

npm run build:prod
echo "Executed npm run build:prod"

5.2 第二个Execute Shell

停止原来的Tomcat服务

stop-admin-web7777.sh的文件内容如下,

5.2.1 停止掉tomcat服务(Tomcat程序文件夹的名字:adminweb-tomcat-7777)

5.2.2. 删除Tomcat配置指向的dist程序

5.2.3. 删除tomcat下的临时文件,work文件夹下的Catalina文件夹及其下面的所有文件

#!/bin/sh

echo "Stop pls-admin web tomcat adminweb-tomcat-7777............"
echo ".............stopping.................."

NAME='adminweb-tomcat-7777'
echo $NAME
ID=`ps -ef | grep "$NAME" | grep -v "$0" | grep -v "grep" | awk '{print $2}'`
echo $ID
echo "---------------"
for id in $ID
do
kill -9 $id
echo "killed $id"
done

echo "...........stopped adminweb-tomcat-7777  successfully............"

source /etc/profile

cd  /mnt/diskb/mallplus-runnning/pls-admin-web/
rm -rf dist

echo "......Deleted dist at /mnt/diskb/mallplus-runnning/pls-admin-web/dist ............"

cd /mnt/diskb/installed/adminweb-tomcat-7777/work

rm -rf Catalina

echo "......Deleted Catalina at  /mnt/diskb/installed/adminweb-tomcat-7777/work/Catalina ............"

5.3 第三个Execute Shell

将第一个Exucute Shell编译出的dist程序,拷贝到Tomcat指向执行的文件夹pls-admin-web下,并赋予读写权限

5.4 第四个Execute Shell

启动Tomcat,BUILD_ID=dontKillme,是防止tomcat启动后被Jenkins立刻杀死,如果tomcat能正常启动,这不需要加这个donKillMe.

start-admin-web7777.sh的文件内容如下,

#!/bin/sh

echo "Start adminweb-tomcat-7777 ............"
echo ".............starting.................."
/mnt/diskb/installed/adminweb-tomcat-7777/bin/startup.sh
echo "...........started successfully............"

6. 手动触发Jenkins Job->build now 进行构建或者通过Push Events触发构建,进行测试,在浏览器中访问程序,出现正常登陆页面,进行测试。

7. 备注Tomcat下 server.xml中配置tomcat根项目为dist

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">


		<Context path="/" docBase="/mnt/diskb/mallplus-runnning/pls-admin-web/dist" debug="0" privileged="true" reloadable="true">
       </Context>

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章