[root@Tomcat ~]# ls
anaconda-ks.cfg jdk-8u181-linux-x64.tar.gz
[root@Tomcat ~]# tar zxf jdk-8u181-linux-x64.tar.gz
[root@Tomcat ~]# ls
anaconda-ks.cfg jdk-8u181-linux-x64.tar.gz jdk1.8.0_181
[root@Tomcat ~]# mv jdk1.8.0_181/ /usr/local/java
[root@Tomcat ~]# cat <<END >> /etc/profile
export JAVA_HOME=/usr/local/java
export PATH=$PATH:"$JAVA_HOME/bin"
END
[root@Tomcat ~]# source /etc/profile
[root@Tomcat ~]# java -version
[root@Tomcat ~]# wget https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.32/bin/apache-tomcat-8.5.32.tar.gz
[root@Tomcat ~]# ls
anaconda-ks.cfg apache-tomcat-8.5.32.tar.gz jdk-8u181-linux-x64.tar.gz
[root@Tomcat ~]# tar zxf apache-tomcat-8.5.32.tar.gz
[root@Tomcat ~]# ls
anaconda-ks.cfg apache-tomcat-8.5.32.tar.gz apache-tomcat-8.5.32 jdk-8u181-linux-x64.tar.gz
[root@Tomcat ~]# mv apache-tomcat-8.5.32 /usr/local/tomcat
在 Tomcat 服务器上配置(优化执行路径)
[root@Tomcat ~]# sed -i '/#!\/bin\/sh/aJAVA_HOME="/usr/local/java"' /usr/local/tomcat/bin/catalina.sh
[root@Tomcat ~]# sed -i '/#!\/bin\/sh/aJRE_HOME="/usr/local/java/jre"' /usr/local/tomcat/bin/catalina.sh
启动 Tomcat
[root@Tomcat ~]# /usr/local/tomcat/bin/startup.sh # 启动 Tomcat 服务
[root@Tomcat ~]# netstat -nlpt | grep 8080 # 查看端口是否开启
我们这里主要就是使用 Maven 来将开发写的 Java 代码,打包成 war
包后,来使用 Jenkins 将打包好的 war
包,部署到 Tomcat 上,来完成简单的版本迭代;像一些在云上的项目,基本上都在用云效(云版 Jenkins)
我们可以将 Maven 安装在 Jenkins 的服务器上:
[root@Jenkins ~]# wget https://archive.apache.org/dist/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz
[root@Jenkins ~]# tar xf apache-maven-3.5.4-bin.tar.gz
[root@Jenkins ~]# mv apache-maven-3.5.4 /usr/local/maven
[root@Jenkins ~]# ln -s /usr/local/maven/bin/mvn /usr/bin/
1)配置 Maven 的环境变量
[root@Jenkins ~]# echo "export MAVEN_HOME=/usr/local/maven" >> /etc/profile
[root@Jenkins ~]# echo "export PATH=$PATH:$MAVEN_HOME/bin" >> /etc/profile
[root@Jenkins ~]# source /etc/profile
2)在 Maven 上配置阿里云加速
[root@Jenkins ~]# vim /usr/local/maven/conf/settings.xml
157 </mirror>
158 -->
159 <mirror>
160 <id>aliyun maven</id>
161 <name>aliyun</name>
162 <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
163 <mirrorOf>central</mirrorOf>
164 </mirror>
165 </mirrors>
1)创建 Jenkins 任务前,需要安装以下插件:
Git plugin
Maven Release Plug-in
3)开始新建一个 Jenkins 任务
General
源码管理
Build
[root@Jenkins ~]# vim tomcat_update.sh
#!/bin/bash
scp /var/lib/jenkins/workspace/tomcat/gameoflife-web/target/*.war root@192.168.1.3:/usr/local/tomcat/webapps/
ssh root@192.168.1.3 "/usr/local/tomcat/bin/shutdown.sh"
sleep 3
ssh root@192.168.1.3 "/usr/local/tomcat/bin/startup.sh"
5)将代码上传到 GitLab 远程仓库中
[root@Jenkins ~]# git clone git@192.168.1.1:ZhangSan/test.git
[root@Jenkins ~]# git clone git@github.com:wakaleo/game-of-life.git # 克隆 GitHub 上的代码
[root@Jenkins ~]# cd test/
[root@Jenkins test]# git rm -rf * # 删除原有仓库上的代码 (生产环境需慎重考虑)
[root@Jenkins test]# mv ../game-of-life/* . # 将克隆过来的代码移动到当前目录
[root@Jenkins test]# git add . # 上传到暂存区
[root@Jenkins test]# git commit -m 'This is Tomcat of Test' # 将暂存区代码上传到本地仓库
[root@Jenkins test]# git push origin master # 将本地仓库上传到远程仓库
Permission denied (publickey).
时,我们只需要将公钥上传到 GitHub 上即可。6)在 Jenkins 构建 Tomcat 项目
问题(1):如果在构建时出现如下错误,原因是因为没有配置密钥对。
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
配置 SSH 密钥对
[root@Jenkins ~]# ssh-keygen -t rsa # 创建密钥对 (三连回车即可)
[root@Jenkins ~]# ssh-copy-id root@192.168.1.3
问题(2):如果在构建时出现如下错误,原因是因为没有优化执行路径。
Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
At least one of these environment variable is needed to run this program
Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
At least one of these environment variable is needed to run this program
在 Tomcat 服务器上配置
[root@Tomcat ~]# sed -i '/#!\/bin\/sh/aJAVA_HOME="/usr/local/java"' /usr/local/tomcat/bin/catalina.sh
[root@Tomcat ~]# sed -i '/#!\/bin\/sh/aJRE_HOME="/usr/local/java/jre"' /usr/local/tomcat/bin/catalina.sh