环境准备:
安装centos7.2系统,最小化安装
设置好网络和防火墙 网络需要能访问外网,
ip 192.168.0.88
下面关闭防火墙
setenforce 0
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/enforcing/disabled/g' /etc/sysconfig/selinux
设置yum源
cd /etc/yum.repos.d/
wget http://mirrors.aliyun.com/repo/Centos-7.repo
wget http://mirrors.aliyun.com/repo/epel-7.repo
yum -y install epel-release
yum -y install vim wget lrzsz net-tools
安装JDK
yum install java-1.8.0-openjdk.x86_64 -y
yum install java-1.8.0-openjdk*-y
安装jenkins
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo
rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
yum install jenkins -y
service jenkins start
安装git
yum install -y git
安装maven
wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz
tar zxf apache-maven-3.3.9-bin.tar.gz -C /usr/local/
vim /etc/profile
增加配置
export MAVEN_HOME=/usr/local/apache-maven-3.3.9
export PATH=$PATH:$MAVEN_HOME/bin
source /etc/profile
查看版本
[root@localhost ~]# mvn -v
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
Maven home: /usr/local/apache-maven-3.3.9
Java version: 1.8.0_201, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-327.el7.x86_64", arch: "amd64", family: "unix"
配置git仓库
设置git全局用户和邮件
git config --global user.name root
git config --global user.email [email protected]
git config --list
创建代码库
cd /opt/
git init --bare mybank-demo-maven.git
cd /tmp/
git clone [email protected]:/opt/mybank-demo-maven.git
cd mybank-demo-maven/
git clone https://github.com/nichochen/mybank-demo-maven
cd mybank-demo-maven/
rm -rf .git
cd ../
mv mybank-demo-maven/* .
mv mybank-demo-maven/.* .
rm -rf mybank-demo-maven/
提交到代码库
git add .
git commit -m 'first commit'
git push origin master
测试
cd /home/
git clone [email protected]:/opt/mybank-demo-maven.git
git ls-remote -h [email protected]:/opt/mybank-demo-maven.git HEAD
git ls-remote -h [email protected]:/opt/mybank-demo-maven.git
创建秘钥
(jenkins的ssh方式连接git,必须使用Credentials秘钥id_rsa形式,http连接方式可以用Credentials的用户名密码形式)
ssh-keygen -t rsa (一路回车即可)
cd /root/.ssh/
cat id_rsa.pub > authorized_keys
chmod 600 authorized_keys
界面配置使用jenkins
打开http://192.168.0.88:8080
cat /var/lib/jenkins/secrets/initialAdminPassword 将密码填入
系统管理--全局安全配置
系统管理---》插件管理
在可选插件中,maven ssh 相关插件安装
添加Credentials凭据
添加秘钥凭据 (ssh方式连接git用这种)
将该密钥放到下图key中 创建对应的凭据
系统管理--全局工具配置
查看java 家目录
[root@localhost ~]# ll /etc/alternatives/javac
lrwxrwxrwx. 1 root root 70 4月 10 10:04 /etc/alternatives/javac -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/bin/javac
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/ 就是家目录
配置完maven就可以了
回主页 新任务 创建maven 工程
然后保存 --立即构建
将应用自动部署到tomcat上
安装tomcat
yum install tomcat* -y
修改端口为8081
vim /etc/tomcat/server.xml 改端口为8081
systemctl start tomcat
回到jenkins界面
系统管理 -- 系统配置--增加要部署机器的ssh server配置--应用--保存
然后 调整原maven项目的配置,增加下面配置--构建后操作
其中的命令行如下
systemctl stop tomcat
rm -rf /usr/share/tomcat/webapps/ROOT.war
rm -rf /usr/share/tomcat/webapps/ROOT
cp /mnt/ROOT.war /usr/share/tomcat/webapps/ROOT.war
systemctl start tomcat
然后构建 成功后就可以访问啦