第十五章 Jenkins参数及web项目
一、创建本地Git仓库
1.上传java包
1.上传jar包
[root@m01 ~]# rz sholdboyedu-jenkins-jar.zip
2.解压包
[root@m01 ~]# unzip sholdboyedu-jenkins-jar.zip
2.创建gitlab仓库
1.删除原有Git库
[root@jenkins ~]# cd sholdboyedu-jenkins-jar/
[root@jenkins ~/sholdboyedu-jenkins-jar]# rm -rf .git
2.新建Git库
[root@jenkins ~/sholdboyedu-jenkins-jar]# git init
Initialized empty Git repository in /root/sholdboyedu-jenkins-jar/.git/
[root@m01 ~/sholdboyedu-jenkins-jar]# git remote add origin http://10.0.0.61/root/sholdboyedu-jenkins-java-jar.git
[root@m01 ~/sholdboyedu-jenkins-jar]# git config -l
user.name=Administrator
user.email=admin@example.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=http://10.0.0.61/root/sholdboyedu-jenkins-java-jar.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
3.推送到远程Gitlab库
[root@m01 ~/sholdboyedu-jenkins-jar]# git add .
[root@m01 ~/sholdboyedu-jenkins-jar]# git commit -m 'init'
[master (root-commit) 7c0e374] init
11 files changed, 747 insertions(+)
create mode 100644 .gitignore
create mode 100644 .mvn/wrapper/MavenWrapperDownloader.java
create mode 100644 .mvn/wrapper/maven-wrapper.jar
create mode 100644 .mvn/wrapper/maven-wrapper.properties
create mode 100755 mvnw
create mode 100644 mvnw.cmd
create mode 100644 pom.xml
create mode 100644 src/main/java/com/sholdboyedu/com/sholdboyedujenkinsjar/ServletInitializer.java
create mode 100644 src/main/java/com/sholdboyedu/com/sholdboyedujenkinsjar/SholdboyeduJenkinsJarApplication.java
create mode 100644 src/main/resources/application.properties
create mode 100644 src/test/java/com/sholdboyedu/com/sholdboyedujenkinsjar/SholdboyeduJenkinsJarApplicationTests.java
[root@m01 ~/sholdboyedu-jenkins-jar]# git push origin master
Username for 'http://10.0.0.61': root
Password for 'http://root@10.0.0.61':
Counting objects: 29, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (18/18), done.
Writing objects: 100% (29/29), 53.06 KiB | 0 bytes/s, done.
Total 29 (delta 0), reused 0 (delta 0)
To http://10.0.0.61/root/sholdboyedu-jenkins-java-jar.git
* [new branch] master -> master
[root@m01 ~/sholdboyedu-jenkins-jar]#
3.创建jenkins项目
二、jenkins构建maven
在Jenkins集成服务器上,我们需要安装Maven来编译和打包项目。
1.安装maven包
#1.下载安装包
[root@jenkins /opt]# wget https://mirrors.aliyun.com/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
--2020-12-24 10:16:04-- https://mirrors.aliyun.com/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 153.99.248.248, 221.6.157.199, 218.98.9.99, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|153.99.248.248|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9506321 (9.1M) [application/octet-stream]
Saving to: ‘apache-maven-3.6.3-bin.tar.gz’
100%[===============================================================================>] 9,506,321 15.4MB/s in 0.6s
2020-12-24 10:16:05 (15.4 MB/s) - ‘apache-maven-3.6.3-bin.tar.gz’ saved [9506321/9506321]
2.安装jdk
1.安装jdk
[root@jenkins ~]# yum install java-1.8.0-openjdk* -y
2.检查jdk版本
[root@jenkins ~]# java -version
openjdk version "1.8.0_275"
OpenJDK Runtime Environment (build 1.8.0_275-b01)
OpenJDK 64-Bit Server VM (build 25.275-b01, mixed mode)
3.安装maven
[root@jenkins /opt]# tar xf apache-maven-3.6.3-bin.tar.gz
[root@jenkins /opt]# ll
total 9284
drwxr-xr-x 6 root root 99 Dec 24 10:19 apache-maven-3.6.3
-rw-r--r-- 1 root root 9506321 Jul 3 12:01 apache-maven-3.6.3-bin.tar.gz
drwxr-xr-x 4 root root 111 Dec 23 20:25 opt
[root@jenkins /opt]# mv apache-maven-3.6.3 /usr/local/
[root@jenkins /opt]# ll /usr/local/
total 0
drwxr-xr-x 6 root root 99 Dec 24 10:19 apache-maven-3.6.3
4、添加环境变量
1.配置环境变量
[root@jenkins /usr/local/apache-maven-3.6.3]# vim /etc/profile.d/maven.sh
export PATH=/usr/local/apache-maven-3.6.3/bin:$PATH
2.重载环境变量
[root@jenkins /usr/local/apache-maven-3.6.3]# source /etc/profile
3.测试
[root@jenkins ~]# mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/apache-maven-3.6.3
Java version: 1.8.0_275, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.275.b01-0.el7_9.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-957.el7.x86_64", arch: "amd64", family: "unix"
5、配置jenkins适配moven
配置完以上,需要保存。
6、使jenkins调用maven
设置完环境变量,需要点击保存。
7、安装maven插件
8、创建maven本地仓库
1.创建maven仓库
[root@jenkins /usr/local/apache-maven-3.6.3/conf]# mkdir -pv /data/software/repository
mkdir: created directory ‘/data’
mkdir: created directory ‘/data/software’
mkdir: created directory ‘/data/software/repository’
2.授权maven仓库
[root@instance-gvpb80ao ~]# chown jenkins.jenkins /data/software/repository/
9、修改maven配置
[root@jenkins ~]# vim /usr/local/apache-maven-3.6.3/conf/settings.xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!--本地仓库-->
<localRepository>/data/software/repository</localRepository>
<mirrors>
<!--阿里云镜像-->
<mirror>
<id>aliyun-maven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven mirror</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
</mirrors>
...
...
</settings>
测试maven
三、部署Tomcat
1、安装JDK
[root@web01 ~]# yum install java-1.8.0-openjdk* -y
2、下载tomcat安装包
[root@web01 ~]# cd /opt/
[root@web01 /opt]# wget https://ftp.wayne.edu/apache/tomcat/tomcat-9/v9.0.41/bin/apache-tomcat-9.0.41.tar.gz
--2020-12-25 16:56:29-- https://ftp.wayne.edu/apache/tomcat/tomcat-9/v9.0.41/bin/apache-tomcat-9.0.41.tar.gz
Resolving ftp.wayne.edu (ftp.wayne.edu)... 141.217.0.199
Connecting to ftp.wayne.edu (ftp.wayne.edu)|141.217.0.199|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11442169 (11M) [application/x-gzip]
Saving to: ‘apache-tomcat-9.0.41.tar.gz’
100%[===============================================================================>] 11,442,169 476KB/s in 32s
2020-12-25 16:57:03 (348 KB/s) - ‘apache-tomcat-9.0.41.tar.gz’ saved [11442169/11442169]
3、解压安装
[root@web01 /opt]# tar xf apache-tomcat-9.0.41.tar.gz
[root@web01 /opt]# ll
total 11176
drwxr-xr-x 9 root root 220 Dec 25 16:57 apache-tomcat-9.0.41
-rw-r--r-- 1 root root 11442169 Dec 3 20:42 apache-tomcat-9.0.41.tar.gz
[root@web01 /opt]# mv apache-tomcat-9.0.41 /usr/local/tomcat-9.0.41
[root@web01 /opt]# ln -s /usr/local/tomcat-9.0.41 /usr/local/tomcat
[root@web01 /opt]# cd /usr/local/tomcat
4、添加环境变量
[root@web01 /usr/local/tomcat]# vim /etc/profile.d/tomcat.sh
export PATH=/usr/local/tomcat-9.0.41/bin/:$PATH
[root@web01 /usr/local/tomcat]# source /etc/profile
5、添加用户
# 1、添加tomcat用户
[root@web01 /usr/local/tomcat]# vim /usr/local/tomcat/conf/tomcat-users.xml
... ...
<role rolename="tomcat"/>
<role rolename="role1"/>
<role rolename="manager-script"/>
<role rolename="manager-gui"/>
<role rolename="manager-status"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<user username="tomcat" password="tomcat" roles="manager-gui,manager-script,tomcat,admin-gui,admin-script"/>
# 2、注释权限验证
[root@web01 /usr/local/tomcat]# vim /usr/local/tomcat/webapps/manager/META-INF/context.xml
<!--
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
-->
6、启动tomcat
[root@test-web /usr/local/apache-tomcat-9.0.41]# ./bin/startup.sh
Using CATALINA_BASE: /usr/local/apache-tomcat-9.0.41
Using CATALINA_HOME: /usr/local/apache-tomcat-9.0.41
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-9.0.41/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/apache-tomcat-9.0.41/bin/bootstrap.jar:/usr/local/apache-tomcat-9.0.41/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
7、验证
四、添加ssh免密认证
[root@jenkins ~]# ssh-keygen
[root@jenkins ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.7
/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.7's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@10.0.0.7'"
and check to make sure that only the key(s) you wanted were added.
五、将war包推送至tomcat容器
1、下载jenkins链接tomcat容器的插件
Deploy to container