文章目录

展开

jenkins的构建风格有几种?

  • freestyle自由风格
  • maven风格(构建Java代码)
  • pipeline流水线风格

Maven介绍

maven是编译构建Java代码的工具

Maven是一个项目管理和综合工具。Maven提供给开发人员构建一个完整的生命周期框架。开发团队可以自动完成该项目的基础设施建设,Maven使用标准的目录结构和默认构建生命周期。Apache的开源项目主要服务于JAVA平台的构建、依赖管理、项目管理。Project Object Model,项目对象模型。通过xml格式保存的pom.xml文件。该文件用于管理:源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等等。该文件是由开发维护,我们运维人员可以不用去关心。

  • Maven风格

    • pre steps 构建之前的操作 // 代码质量检测
    • build 构建 // mvn clean package -Dxxx -Dxxx
    • post steps 构建之后的操作 // 发布代码,邮件通知

    编译JAVA代码的工具

    比如nginx,make

    比如MySQL,cmake、gmake

    Java也有编译的工具

    • Maven
    • Ant

    JAVA代码容器:

    • tomcat
    • resin

java代码上线流程

# 1.java开发将代码传到gitlab

# 2.运维将java代码从gitlab中拉下来(自动化:Jenkins拉取)

# 3.代码质量检测(审核等...)

# 4.代码编译
- maven
- ant

# 5.发布代码
- 灰度
- 蓝绿
- 滚动

gitlab创建Java项目

image-20230828201403258

image-20230828201415498

image-20230828201450482

安装tomcat(web01)

[root@web01 conf.d]# yum install -y tomcat

手动部署,安装maven(gitlab)

[root@gitlab hello-world-war]# yum install -y maven

# 修改maven配置文件
[root@gitlab ~]# vim /etc/maven/settings.xml 

    <mirror>
       <id>aliyunmaven</id>
       <mirrorOf>*</mirrorOf>
       <name>阿里云公共仓库</name>
       <url>https://maven.aliyun.com/repository/public</url>
     </mirror>

 # 编译
 [root@gitlab hello-world-war]# mvn clean package

 ## 编译完成后有个target目录
 [root@gitlab hello-world-war]# ll
total 8
drwxr-xr-x 2 root root  29 May 23  2014 dist
-rw-r--r-- 1 root root 930 May 23  2014 pom.xml
-rw-r--r-- 1 root root 213 May 23  2014 README.md
drwxr-xr-x 3 root root  18 May 23  2014 src
drwxr-xr-x 5 root root 106 Aug 28 20:16 target

 ## 进入target目录会有.war的包
[root@gitlab hello-world-war]# cd target/
[root@gitlab target]# ll
total 4
drwxr-xr-x 4 root root   54 Aug 28 20:16 hello-world-war-1.0.0
-rw-r--r-- 1 root root 2402 Aug 28 20:16 hello-world-war-1.0.0.war
drwxr-xr-x 2 root root   28 Aug 28 20:16 maven-archiver
drwxr-xr-x 2 root root    6 Aug 28 20:16 surefire

## 将war包推到web服务器
[root@gitlab target]# scp hello-world-war-1.0.0.war 172.16.1.7:/root

## 进入tomcat站点目录
[root@web01 ~]# cd /usr/share/tomcat/webapps/

## 将war包移至站点目录
[root@web01 webapps]#  mv /root/hello-world-war-1.0.0.war .

## 启动tomcat
[root@web01 webapps]# systemctl start tomcat

## 查看
[root@web01 webapps]# ll
total 4
drwxr-xr-x 4 tomcat tomcat   54 Aug 28 20:24 hello-world-war-1.0.0
-rw-r--r-- 1 root   root   2402 Aug 28 20:23 hello-world-war-1.0.0.war

## 做软连接
[root@web01 webapps]# ln -s /usr/share/tomcat/webapps/hello-world-war-1.0.0 /usr/share/tomcat/webapps/ROOT

##或者low一点直接在webapps下mkdir ROOT 把解压完的 hello-world-war-1.0.0 移到ROOT目录下

## 访问页面10.0.0.7:8080

image-20230828203019940

优化一下

## 删除maven编译目录
[root@gitlab hello-world-war]# rm -fr /root/hello-world-war/target/

## 修改hello world代码
[root@gitlab webapp]# vim /root/hello-world-war/src/main/webapp/index.jsp 
<html>
<head>
<title>Hello World!</title>
</head>
<body>
        <h1>Hello World! v6.0</h1>
        <p>
                It is now
                <%= new java.util.Date() %></p>
        <p>
                You are coming from
                <%= request.getRemoteAddr()  %></p>
</body>

## 编译是根据 pom.xml来编译的
[root@gitlab webapp]# cd /root/hello-world-war
[root@gitlab hello-world-war]# ll
total 8
drwxr-xr-x 2 root root  29 May 23  2014 dist
-rw-r--r-- 1 root root 930 May 23  2014 pom.xml
-rw-r--r-- 1 root root 213 May 23  2014 README.md
drwxr-xr-x 3 root root  18 May 23  2014 src

## 修改pom.xml文件
   <version>1.0.0</version>  ->    <version>6.0.0</version>

## 编译
[root@gitlab hello-world-war]# mvn clean package

## 查看编译文件
[root@gitlab hello-world-war]# cd target/
[root@gitlab target]# ll
total 4
drwxr-xr-x 4 root root   54 Aug 28 19:57 hello-world-war-6.0.0
-rw-r--r-- 1 root root 2405 Aug 28 19:57 hello-world-war-6.0.0.war
drwxr-xr-x 2 root root   28 Aug 28 19:57 maven-archiver
drwxr-xr-x 2 root root    6 Aug 28 19:57 surefire

## 在web端创建一个历史版本的目录(web01)
[root@web01 webapps]# mkdir /hello_version

## 将war包推到历史版本目录
[root@gitlab target]#  scp hello-world-war-6.0.0.war 172.16.1.7:/hello_version

## 解压war包(web01)
[root@web01 webapps]# cd /hello_version/
[root@web01 hello_version]# unzip hello-world-war-6.0.0.war -d hello-world-war-6.0.0
[root@web01 hello_version]# ll
total 4
drwxr-xr-x 4 root root   54 Aug 28 20:46 hello-world-war-6.0.0
-rw-r--r-- 1 root root 2403 Aug 28 20:41 hello-world-war-6.0.0.war

## 删除之前的1.0版本
[root@web01 hello_version]# rm -fr /usr/share/tomcat/webapps/ROOT

## 做软连接
[root@web01 hello_version]# ln -s /hello_version/hello-world-war-6.0.0 /usr/share/tomcat/webapps/ROOT

## 重启tomcat
[root@web01 hello_version]# systemctl restart tomcat

## 访问页面

image-20230828205138931

使用jenkins创建mvn风格项目

image-20230828210521607

image-20230828210640335

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

# 修改maven配置文件
[root@gitlab ~]# vim /etc/maven/settings.xml
159 <mirror>
160 <id>aliyunmaven</id>
161 <mirrorOf>*</mirrorOf>
162 <name>阿里云公共仓库</name>
163 <url>https://maven.aliyun.com/repository/public</url>
164 </mirror>

image-20230828210917684

image-20230828210943804

image-20230828211028521

image-20230828211148635

## 查看java和maven家目录
[root@jenkins ~]# mvn -version
Apache Maven 3.0.5 (Red Hat 3.0.5-17)
Maven home: /usr/share/maven
Java version: 1.8.0_382, vendor: Red Hat, Inc.
Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.382.b05-1.el7_9.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.el7.x86_64", arch: "amd64", family: "unix"
## java 家目录/jer不需要填

image-20230828211540244

image-20230828211554174

image-20230828211514229

image-20230828211721337

image-20230828211824396

image-20230828211929396

image-20230828212003924

## 版本
version=`awk -F '[><]' '/<version>/{print $3; exit}' ${WORKSPACE}/pom.xml`
history='/hello_version'
for ip in 7;do
scp ${WORKSPACE}/target/hello-world-war-${version}.war root@172.16.1.${ip}:${history}
ssh -o StrictHostKeyChecking=no -o BatchMode=yes root@172.16.1.${ip} "unzip -n ${history}/hello-world-war-${version}.war -d ${history}/hello-world-war-${version}"
ssh root@172.16.1.${ip} "rm -rf /usr/share/tomcat/webapps/ROOT"
ssh root@172.16.1.${ip} "ln -s ${history}/hello-world-war-${version} /usr/share/tomcat/webapps/ROOT"
ssh root@172.16.1.${ip} "systemctl restart tomcat"
done

image-20230828215138346

[root@gitlab hello-world-war]# vim pom.xml 
[root@gitlab hello-world-war]# vim src/main/webapp/index.jsp 
[root@gitlab hello-world-war]# git add .
[root@gitlab hello-world-war]# git commit -m '7.0'
[master 7a4ce49] 7.0
 2 files changed, 3 insertions(+), 3 deletions(-)
[root@gitlab hello-world-war]# git tag -a '7,0' -m '7.0'
[root@gitlab hello-world-war]# git push --all
Counting objects: 44, done.
Compressing objects: 100% (29/29), done.
Writing objects: 100% (44/44), 4.90 KiB | 0 bytes/s, done.
Total 44 (delta 11), reused 0 (delta 0)
remote: 
remote: To create a merge request for master, visit:
remote:   http://gitlab.wodeyumengouwo.com/root/hello-word/-/merge_requests/new?merge_request%5Bsource_branch%5D=master
remote: 
To git@gitlab.wodeyumengouwo.com:root/hello-word.git
 * [new branch]      master -> master
[root@gitlab hello-world-war]# git push --tag
Counting objects: 5, done.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 646 bytes | 0 bytes/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To git@gitlab.wodeyumengouwo.com:root/hello-word.git
 * [new tag]         7,0 -> 7,0
 * [new tag]         v1.0 -> v1.0
 * [new tag]         v2.0 -> v2.0
 * [new tag]         v3.0 -> v3.0
 * [new tag]         v4.0 -> v4.0

image-20230828212710218

image-20230828215054889

  • 访问网页

image-20230828215004217

使用钩子创建mvn风格项目

image-20230828215347322

image-20230828215408789

image-20230828215747573

image-20230828221347451

image-20230828215706559

image-20230828215929562

image-20230828220000202

image-20230828220049479

http://10.0.0.92:8080/project/hello-word-maven-webhook
5d96999f7cc0466606795a6ebf6cc592

image-20230828220137534

version=`awk -F '[><]' '/<version>/{print $3; exit}' ${WORKSPACE}/pom.xml`
history='/hello_version'
for ip in 7;do
scp ${WORKSPACE}/target/hello-world-war-${version}.war root@172.16.1.${ip}:${history}
ssh -o StrictHostKeyChecking=no -o BatchMode=yes root@172.16.1.${ip} "unzip -n ${history}/hello-world-war-${version}.war -d ${history}/hello-world-war-${version}"
ssh root@172.16.1.${ip} "rm -rf /usr/share/tomcat/webapps/ROOT"
ssh root@172.16.1.${ip} "ln -s ${history}/hello-world-war-${version} /usr/share/tomcat/webapps/ROOT"
ssh root@172.16.1.${ip} "systemctl restart tomcat"
done

Gitlab上进行钩子配置

image-20230828220354139

image-20230828220427412

image-20230828220504005

image-20230828220526189

image-20230828220609066

image-20230828220617244

image-20230828220635715

image-20230828220644089

[root@gitlab ~]# cd hello-world-war/
[root@gitlab hello-world-war]# vim src/main/webapp/index.jsp 
[root@gitlab hello-world-war]# vim pom.xml 
[root@gitlab hello-world-war]#  mvn clean package
[root@gitlab hello-world-war]# git add .
[root@gitlab hello-world-war]# git commit -m '8.0'
[root@gitlab hello-world-war]# git tag -a '8.0' -m '8.0'
[root@gitlab hello-world-war]# git push --all
[root@gitlab hello-world-war]# git push --tag

image-20230828223238865

image-20230828222953122

jenkins角色关联项目关联视图

创建视图

image-20230829153919485

image-20230829153941403

image-20230829154145403

image-20230829154215052

创建用户

image-20230829154618025

image-20230829154636297

image-20230829154739634

关联项目关联视图

  • 安装插件:Role-based Authorization Strategy

image-20230829152710749

  • 在全局安全配置授权策略中选择role-based strategy

image-20230829152751311

  • 再返回系统管理中会有manage and assign roles的选项

image-20230829152823366

image-20230829160137237

image-20230829155238283

image-20230829155424575

  • 切换dnf用户查看

image-20230829155541976

image-20230829155948990