第九章 Jenkins引數及web專案
阿新 • • 發佈:2022-01-25
一、建立本地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 [email protected] 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://[email protected]': 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 [email protected]
/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
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
五、將war包推送至tomcat容器
1、下載jenkins連結tomcat容器的外掛
Deploy to container