Jenkins流水线项目发布流程


CI/CD代码发布流程


  1. 开发者开发代码
  2. 提交至git仓库
  3. jenkins从仓库拉取代码
  4. jenkins通过maven(ant,gradle等)构建项目推到docker仓库
  5. 生成一个在tomcat运行的项目的docker容器
  6. 测试人员测试

Jenkins流水线项目发布流程_linux


流水线项目创建

本次环境为:

主机名

IP地址

安装的应用

系统版本

jenkins

192.168.100.1

tomcat jenkins

RedHat 8

client

192.168.100.2

tomcat

RedHat 8

tomcat、jenkins安装配置详细请见:​​Jenkins配置​

准备工作:

#jenkins
//关闭防火墙
[root@jenkins ~]# systemctl disable --now firewalld
[root@jenkins ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
[root@jenkins ~]# setenforce 0

//安装jdk环境
[root@jenkins ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel

//安装git和maven
[root@jenkins ~]# yum -y install git maven

//安装tomcat
[root@jenkins ~]# cd /usr/src/
[root@jenkins src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.46/bin/apache-tomcat-9.0.46.tar.gz
[root@jenkins src]# ls
apache-tomcat-9.0.46.tar.gz debug kernels
[root@jenkins src]# tar xf apache-tomcat-9.0.46.tar.gz -C /usr/local/
[root@jenkins src]# cd /usr/local/
[root@jenkins local]# ln -s apache-tomcat-9.0.46/ tomcat

//部署jenkins
[root@jenkins ~]# cd /usr/src/
[root@jenkins src]# wget http://mirrors.jenkins-ci.org/war/2.293/jenkins.war
[root@jenkins src]# cp jenkins.war /usr/local/tomcat/webapps
[root@jenkins src]# /usr/local/tomcat/bin/catalina.sh start
[root@jenkins src]# ls /usr/local/tomcat/webapps/
docs examples host-manager jenkins jenkins.war manager ROOT

//SSH免密登录
[root@jenkins ~]# ssh-keygen -t rsa
[root@jenkins ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.100.2

#client
//关闭防火墙
[root@client ~]# systemctl disable --now firewalld
[root@client ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
[root@client ~]# setenforce 0

//安装jdk环境
[root@client ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel

//安装tomcat
[root@client ~]# cd /usr/src
[root@client src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.46/bin/apache-tomcat-9.0.46.tar.gz
[root@client src]# tar xf apache-tomcat-9.0.46.tar.gz -C /usr/local/
[root@client src]# cd /usr/local/
[root@client local]# ln -s apache-tomcat-9.0.46/ tomcat

管理Jenkins

登录192.168.100.1:8080/jenkins

安装Pipeline、Git插件

Jenkins流水线项目发布流程_apache_02

创建流水线项目

Jenkins流水线项目发布流程_tomcat_03

定义变量

勾选 参数化构建过程 —— 添加参数

Jenkins流水线项目发布流程_linux_04

配置流水线

Jenkins流水线项目发布流程_linux_05

流水线脚本详情如下

pipeline {
agent any

stages {
stage('pull code') {
steps {
git '${Git_repo}'
}
}

stage('package') {
steps {
sh """
mvn clean
mvn package
"""
}
}

stage('deploy') {
steps {
sh """
scp target/${Project_name}.war root@${Client}:${Tomcat_dir}/webapps/
ssh root@${Client} '${Tomcat_dir}/bin/shutdown.sh && ${Tomcat_dir}/bin/startup.sh'
"""
}
}

}
}

立即构建

本次构建变量参数如下

Jenkins流水线项目发布流程_linux_06

构建成功

Jenkins流水线项目发布流程_linux_07

构建成功后验证

#client
//查看tomcat是否启动
[root@client ~]# 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@client ~]# 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

访问 ​​http://192.168.100.2:8080/ly-simple-tomcat-0.0.1-SNAPSHOT/​

Jenkins流水线项目发布流程_apache_08

访问成功!大功告成!