• 欢迎访问显哥博客,本网站纯属学习技术,绝无商业用途,欢迎小伙伴们共同学习!研究技术!QQ:52249909 加我QQ
  • 世界75亿人,这么小的概率,能认识你,是我一生的幸运,不妨加个QQ接触一下:52249909 加我QQ

代码上线——部署Jensns及Pipeline项目【显哥出品,必为精品】

Jenkins lixian 5年前 (2020-05-16) 22003次浏览 0个评论 扫描二维码
文章目录[隐藏]

一、部署maven

[root@jenkins ~]# ls
apache-maven-3.3.9-bin.tar.gz
[root@jenkins ~]# tar xf apache-maven-3.3.9-bin.tar.gz
[root@jenkins ~]# mv apache-maven-3.3.9 /usr/local/maven-3.3.9
[root@jenkins /usr/local]# ln -s /usr/local/maven-3.3.9 /usr/local/maven
[root@jenkins /usr/local]# ll
lrwxrwxrwx  1 root root 22 May 17 23:16 maven -> /usr/local/maven-3.3.9
drwxr-xr-x  6 root root 99 May 17 23:15 maven-3.3.9
[root@jenkins /usr/local]# echo "export PATH=/usr/local/maven/bin/:$PATH"  >>/etc/profile
[root@jenkins /usr/local]# source /etc/profile
[root@jenkins /usr/local]# mvn -v
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
Maven home: /usr/local/maven
Java version: 1.8.0_181, vendor: Oracle Corporation
Java home: /usr/java/jdk1.8.0_181-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-957.el7.x86_64", arch: "amd64", family: "unix"

二、编译jeesns项目

1.上传解压jeesns项目包

[root@jenkins ~]# ls
apache-maven-3.3.9-bin.tar.gz  jeesns.tar.gz
[root@jenkins ~]# tar xf jeesns.tar.gz 
[root@jenkins ~]# cd jeesns/

2.清除上次编译的结果

[root@jenkins ~/jeesns]# mvn clean
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
...
...省略N行代码
...
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.781 s
[INFO] Finished at: 2020-05-17T23:22:30+08:00
[INFO] Final Memory: 6M/31M
[INFO] ------------------------------------------------------------------------

3.开始编译打包

[root@jenkins ~/jeesns]# ls -l jeesns-web/
total 4
drwxr-xr-x 2 root root  113 Nov 19  2018 database
drwxr-xr-x 2 root root   49 Nov 19  2018 doc
-rwxr-xr-x 1 root root 1443 Nov 19  2018 pom.xml
drwxr-xr-x 3 root root   18 Nov 19  2018 src

[root@jenkins ~/jeesns]# mvn package
...
...
省略N行代码
...
[INFO] jeesns-web ......................................... SUCCESS [  4.604 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.136 s
[INFO] Finished at: 2020-05-17T23:24:37+08:00
[INFO] Final Memory: 24M/63M
[INFO] ------------------------------------------------------------------------
[root@jenkins ~/jeesns]# ls -l jeesns-web/
total 4
drwxr-xr-x 2 root root  113 Nov 19  2018 database
drwxr-xr-x 2 root root   49 Nov 19  2018 doc
-rwxr-xr-x 1 root root 1443 Nov 19  2018 pom.xml
drwxr-xr-x 3 root root   18 Nov 19  2018 src
drwxr-xr-x 7 root root  128 May 17 23:24 target

4.将sql文件传输到Tomcat节点

[root@jenkins ~/jeesns]# scp -rp jeesns-web/database/jeesns.sql 10.0.0.5:
root@10.0.0.5's password: 
jeesns.sql                                                                     100%   28KB   2.0MB/s   00:00

三、部署Tomcat节点环境

1.部署jdk及Tomcat

[root@tomcat ~]# ls -l
total 174984
-rw-r--r-- 1 root root   9128610 Mar 27  2019 apache-tomcat-8.0.27.tar.gz
-rw-r--r-- 1 root root 170023183 Aug 14  2018 jdk-8u181-linux-x64.rpm
-rwxr-xr-x 1 root root     28667 Nov 19  2018 jeesns.sql
[root@tomcat ~]# rpm -ivh jdk-8u181-linux-x64.rpm 
warning: jdk-8u181-linux-x64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:jdk1.8-2000:1.8.0_181-fcs        ################################# [100%]
Unpacking JAR files...
	tools.jar...
	plugin.jar...
	javaws.jar...
	deploy.jar...
	rt.jar...
	jsse.jar...
	charsets.jar...
	localedata.jar...
[root@tomcat ~]# mkdir /application
[root@tomcat ~]# tar xf apache-tomcat-8.0.27.tar.gz -C /application
[root@tomcat ~]# ln -s /application/apache-tomcat-8.0.27 /application/tomcat

2.启动Tomcat

#加速启动Tomcat(大概在117行改成下面代码)
[root@tomcat ~]# vim /usr/java/jdk1.8.0_181-amd64/jre/lib/security/java.security
117 securerandom.source=file:/dev/urandom
#启动Tomcat
[root@tomcat ~]# /application/tomcat/bin/startup.sh 
Using CATALINA_BASE:   /application/tomcat
Using CATALINA_HOME:   /application/tomcat
Using CATALINA_TMPDIR: /application/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar
Tomcat started.

3.部署mariadb数据库

#安装mariadb-server服务
[root@tomcat ~]# yum install -y mariadb-server
#启动mariadb-server服务
[root@tomcat ~]# systemctl start mariadb
#设置密码为root
[root@tomcat ~]# mysqladmin password 'root'
[root@tomcat ~]# mysql -uroot -proot
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>
#创建jeesns数据库
MariaDB [(none)]> create database jeesns;
[root@tomcat ~]# mysql -uroot -proot jeesns < jeesns.sql 
[root@tomcat ~]# mysql -uroot -proot -e "use jeesns;show tables;"
+---------------------------+
| Tables_in_jeesns          |
+---------------------------+
| tbl_action                |
| tbl_action_log            |
| tbl_ads                   |
| tbl_archive               |
| tbl_archive_favor         |
| tbl_article               |
| tbl_article_cate          |
...
省略多行表
...
| tbl_tag                   |
| tbl_validate_code         |
| tbl_weibo                 |
| tbl_weibo_comment         |
| tbl_weibo_favor           |
| tbl_weibo_topic           |
+---------------------------+

4.将Jenkins节点编译的war包传输到Tomcat节点

[root@jenkins ~/jeesns]# scp -rp jeesns-web/target/jeesns-web.war 10.0.0.5:/application/tomcat/webapps/ROOT.war

5.浏览器访问IP+8080

代码上线——部署Jensns及Pipeline项目【显哥出品,必为精品】

四、开发上线jeesns项目

1.Gitlab创建项目

代码上线——部署Jensns及Pipeline项目【显哥出品,必为精品】

2.复制项目地址

代码上线——部署Jensns及Pipeline项目【显哥出品,必为精品】

3.开发人员上传项目到gitlab

[root@dev01 ~]# ls
jeesns.tar.gz
[root@dev01 ~]# tar xf jeesns.tar.gz 
[root@dev01 ~]# ls
jeesns  jeesns.tar.gz
[root@dev01 ~]# cd jeesns/
[root@dev01 ~/jeesns]# git remote add origin git@10.0.0.100:Dev/jeesns.git
[root@dev01 ~/jeesns]# git remote
origin
[root@dev01 ~/jeesns]# git push -u origin master 
Counting objects: 1966, done.
Compressing objects: 100% (1880/1880), done.
Writing objects: 100% (1966/1966), 7.09 MiB | 10.51 MiB/s, done.
Total 1966 (delta 297), reused 0 (delta 0)
remote: Resolving deltas: 100% (297/297), done.
To git@10.0.0.100:Dev/jeesns.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

代码上线——部署Jensns及Pipeline项目【显哥出品,必为精品】

4.Jenkins创建任务

代码上线——部署Jensns及Pipeline项目【显哥出品,必为精品】

5.设置丢弃的构建天数及个数

代码上线——部署Jensns及Pipeline项目【显哥出品,必为精品】

6.设置git管理

代码上线——部署Jensns及Pipeline项目【显哥出品,必为精品】

7.设置打包命令并保存

代码上线——部署Jensns及Pipeline项目【显哥出品,必为精品】

8.设置maven路径

代码上线——部署Jensns及Pipeline项目【显哥出品,必为精品】

9.立即构建

代码上线——部署Jensns及Pipeline项目【显哥出品,必为精品】

Jenkins查看结果成功

[root@jenkins ~/jeesns]# ls -l /var/lib/jenkins/workspace/maven-jeesns/jeesns-web/target/
total 25496
drwxr-xr-x 4 root root      181 May 18 00:18 classes
drwxr-xr-x 3 root root       25 May 18 00:18 generated-sources
drwxr-xr-x 5 root root      104 May 18 00:18 jeesns-web
-rw-r--r-- 1 root root 26106049 May 18 00:18 jeesns-web.war
drwxr-xr-x 2 root root       28 May 18 00:18 maven-archiver
drwxr-xr-x 3 root root       35 May 18 00:18 maven-status

五、修改代码自动发布编译

1.编写脚本

[root@jenkins ~/jeesns]# ls /server/scripts/
deploy_rollback.sh  jeesns.sh
[root@jenkins ~/jeesns]# cat /server/scripts/jeesns.sh 
#!/bin/bash
Wep_Ip=10.0.0.5
Code_Dir=/application/tomcat/webapps
Old_Code_Dir=/code/${JOB_NAME}
#判断旧的代码目录是否存在
ssh root@$Wep_Ip "[ -d $Old_Code_Dir ] || mkdir -p $Old_Code_Dir"
#将旧的war包移动到旧的代码目录中
ssh root@$Wep_Ip "mv $Code_Dir/ROOT.war $Old_Code_Dir/ROOT-${BUILD_NUMBER}.war"
#将新的war包推送到站点目录中
scp -rp ${WORKSPACE}/jeesns-web/target/jeesns-web.war root@${Wep_Ip}:${Code_Dir}/ROOT.war

2.发送免密公钥

[root@jenkins ~/jeesns]# ssh-copy-id -i /root/.ssh/id_rsa.pub 10.0.0.5
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@10.0.0.5's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '10.0.0.5'"
and check to make sure that only the key(s) you wanted were added.

3.设置执行脚本命令

代码上线——部署Jensns及Pipeline项目【显哥出品,必为精品】

4.开发人员修改代码

[root@dev01 ~/jeesns]# vim jeesns-web/src/main/webapp/WEB-INF/templates/front/index.ftl
[root@dev01 ~/jeesns]# git commit -am "update jeensns 1"
[master 2d64954] update jeensns 1
 1 file changed, 5 insertions(+), 5 deletions(-)
[root@dev01 ~/jeesns]# git push -u origin master 
Counting objects: 19, done.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (10/10), 877 bytes | 0 bytes/s, done.
Total 10 (delta 6), reused 0 (delta 0)
To git@10.0.0.100:Dev/jeesns.git
   9de3360..2d64954  master -> master
Branch master set up to track remote branch master from origin.

5.立即构建

代码上线——部署Jensns及Pipeline项目【显哥出品,必为精品】

6.访问web测试

代码上线——部署Jensns及Pipeline项目【显哥出品,必为精品】

六、部署Pipeline项目

流水线

CI/CD

CI 持续集成:

CD 持续部署

CD 持续交付

什么是pipeline

工作流程的架构

将独立运行的任务进行结合在一起 形成一套流水线模式的一套架构

1.创建Pipeline任务

代码上线——部署Jensns及Pipeline项目【显哥出品,必为精品】

2.设置仓库地址

代码上线——部署Jensns及Pipeline项目【显哥出品,必为精品】

3.编写Jenkinsfile文件

代码上线——部署Jensns及Pipeline项目【显哥出品,必为精品】

4.开发修改代码

[root@dev01 ~/web_zabbix]# vim index.html 
[root@dev01 ~/web_zabbix]# git commit -am "update 1"
[root@dev01 ~/web_zabbix]# git push -u origin master

5.立即构建,成功

代码上线——部署Jensns及Pipeline项目【显哥出品,必为精品】

6.测试脚本

pipeline{
    agent any
stages{
    stage("get code"){
        steps{
            echo "get code from scm"
        }
    }
    stage("package"){
        steps{
        echo "packge code"
        }
    }
    stage("deploy"){
        steps{
            echo "deploy packge to node1"
        }
    }
    }
}

7.发布脚本

pipeline{

agent any

stages{

stage("get code"){

steps{

echo "get code"

}

}

stage("unit test"){

steps{

echo "unit test"

}

}

stage("package"){

steps{

echo "packge code"

}

}

stage("deploy"){

steps{

sh '/bin/bash /server/scripts/depoly.sh'

			}
		}
	}
}

本站博主 , 版权所有丨如未注明 , 均为原创
转载请注明原文链接:代码上线——部署Jensns及Pipeline项目【显哥出品,必为精品】
喜欢 (0)

您必须 登录 才能发表评论!