jenkins流水线项目发布流程
jenkins流水线项目发布流程
环境说明
主机名称 | IP地址 | 需要的应用服务 | 工具包 | 系统版本 |
---|---|---|---|---|
node1 | 192.168.110.14 | tomcat、jenkins | git、maven | redhat 8 |
node2 | 192.168.110.13 | tomcat | 无 | redhat |
准备工作
关闭防火墙和selinux
//node1和node2
systemctl disable --now firewalld
sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
setenforce 0
在node1主机部署服务(Tomcat、jenkins)
1. 把Tomcat和jenkins包使用xftp传到本机node1
//node1
#在本机查看一下
[root@node1 ~]# ls
anaconda-ks.cfg apache-tomcat-9.0.46.tar.gz jenkins.war
2. 部署Tomcat容器和jenkins项目
//node1
#安装java环境
[root@node1 ~]# yum -y install java-11-openjdk
#安装工具包
[root@node1 ~]# yum -y install maven git
#解压安装包,创建jenkins项目
[root@node1 ~]# tar xf apache-tomcat-9.0.46.tar.gz
[root@node1 ~]# ls
anaconda-ks.cfg apache-tomcat-9.0.46 apache-tomcat-9.0.46.tar.gz jenkins.war
[root@node1 ~]# mkdir -p /usr/local/tomcat/jenkins
[root@node1 ~]# cp -r apache-tomcat-9.0.46/* /usr/local/tomcat/jenkins/
[root@node1 ~]# cp jenkins.war /usr/local/tomcat/jenkins/webapps/
#启动jenkins项目
[root@node1 ~]# /usr/local/tomcat/jenkins/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat/jenkins
Using CATALINA_HOME: /usr/local/tomcat/jenkins
Using CATALINA_TMPDIR: /usr/local/tomcat/jenkins/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/jenkins/bin/bootstrap.jar:/usr/local/tomcat/jenkins/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
[root@node1 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
LISTEN 0 100 *:8080 *:*
在node2主机部署服务
1. 在node1主机上生成一个密钥,传给node2主机
//node1
#生成密钥,直接回车4下
[root@node1 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:gns7tHucexs/lfoIp5e07uMYi9HO1ozGOZeiPenOtkg root@node1
The key's randomart image is:
+---[RSA 3072]----+
| |
| |
| |
| . |
| . . S . |
| ... . . o |
| ...oEo=B.* |
| .oo+O&X%. |
| o+=X&%*+. |
+----[SHA256]-----+
#密钥传给node2
[root@node1 ~]# ssh-copy-id root@192.168.110.13
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.110.13 (192.168.110.13)' can't be established.
ECDSA key fingerprint is SHA256:+UR5bwjFNEKZsJ+X/qSSsMGCvtq5ByludUEL47JqwoU.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/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@192.168.110.13's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.110.13'"
and check to make sure that only the key(s) you wanted were added.
2. 部署Tomcat
//node1
#使用scp命令把Tomcat包传给node2
[root@node1 ~]# scp apache-tomcat-9.0.46.tar.gz root@192.168.110.13:/root/
apache-tomcat-9.0.46.tar.gz 100% 11MB 42.6MB/s 00:00
//node2
#安装java环境
[root@node2 ~]# yum -y install java-11-openjdk
#查看
[root@node2 ~]# ls
anaconda-ks.cfg apache-tomcat-9.0.46.tar.gz
#解压安装包
[root@node2 ~]# mkdir /usr/local/tomcat
[root@node2 ~]# tar xf apache-tomcat-9.0.46.tar.gz
[root@nide2 ~]# cp -r apache-tomcat-9.0.46/* /usr/local/tomcat/
[root@node2 ~]# ls /usr/local/tomcat/
bin conf lib logs README.md RUNNING.txt webapps
BUILDING.txt CONTRIBUTING.md LICENSE NOTICE RELEASE-NOTES temp work
开始部署
第一步:安装jenkins
安装和配置jenkins详情可以参考:jenkins安装和界面说明
选择推荐插件安装(这一步需要注意,其它步骤一致)
第二步:配置jenkins
1、访问网页
访问192.168.110.14:8080/jenkins
2、创建流水线项目
创建任务
输入任务名称test,选择流水线
3、配置test流水线任务
使用构建化参数定义变量
定义文本参数变量
- 仓库地址 Git_URL :http://github.com/lizhenliang/tomcat-java-demo.git
- 项目的名称 Project_name :ly-simple-tomcat-0.0.1-SNAPSHOT
- Tomcat容器解压的目录 Tomcat_dir :/usr/local/tomcat/
- 客户机的IP地址 Client_IP :192.168.110.13
编写流水线代码
流水线脚本内容
pipeline {
agent any
stages {
stage('pull code') {
steps {
git '${Git_URL}'
}
}
stage('package') {
steps {
sh """
mvn clean
mvn package
"""
}
}
stage('deploy') {
steps {
sh """
scp target/${Project_name}.war root@${Client_IP}:${Tomcat_dir}webapps/
ssh root@${Client_IP} '${Tomcat_dir}bin/shutdown.sh && ${Tomcat_dir}bin/startup.sh'
"""
}
}
}
}
4、构建任务
使用参数构建
构建过程
构建成功
第三步:验证
在node2主机上查看运行情况
//node2
[root@node2 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
LISTEN 0 100 *:8080 *:*
LISTEN 0 128 [::]:22 [::]:*
//项目文件
[root@node2 ~]# ls /usr/local/tomcat/webapps/
docs ly-simple-tomcat-0.0.1-SNAPSHOT ROOT
examples ly-simple-tomcat-0.0.1-SNAPSHOT.war
host-manager manager