環境準備:java
安裝centos7.2系統,最小化安裝linux
設置好網絡和防火牆 網絡須要能訪問外網,git
ip 192.168.0.88github
下面關閉防火牆web
setenforce 0apache
systemctl stop firewalldvim
systemctl disable firewalldcentos
sed -i 's/enforcing/disabled/g' /etc/sysconfig/selinuxtomcat
設置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
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 914409585@qq.com
git config --list
建立代碼庫
cd /opt/
git init --bare mybank-demo-maven.git
cd /tmp/
git clone root@192.168.0.88:/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 root@192.168.0.88:/opt/mybank-demo-maven.git
git ls-remote -h root@192.168.0.88:/opt/mybank-demo-maven.git HEAD
git ls-remote -h root@192.168.0.88:/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
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
而後構建 成功後就能夠訪問啦