注意:下列操作都是在jenkins主机部署
tar zxf apache-maven-3.3.9-bin.tar.gz -C /opt/
cd /opt/
ln -s apache-maven-3.3.9 maven
在mirror标签下添加mirror区块
vim /opt/maven/conf/settings.xml
<mirror>
<id>nexus-aliyunid>
<mirrorOf>*mirrorOf>
<name>Nexus aliyunname>
<url>http://maven.aliyun.com/nexus/content/groups/publicurl>
mirror>
echo 'export PATH=$PATH:/opt/maven/bin' >> /etc/profile
source /etc/profile
mvn -v
tar zxf hello-world-war.tar.gz
cd hello-world-war/
mvn package
ll target
以下操作在gitlab服务器进行:
tar zxf hello-world-war.tar.gz
cd hello-world-war/
git remote remove origin
git init
git remote add origin git@10.0.0.200:dev/java-helloworld.git
git add .
git commit -m "Install commit"
git push -u origin master
进入Jenkins的系统管理-->全局工具配置页面-->Maven安装
点击立即构建,然后查看jenkins的工作目录下是否成功生成了war包
[root@jenkins-201 ~]# ll /var/lib/jenkins/workspace/java-helloworld/target/
总用量 4
drwxr-xr-x 4 root root 54 8月 7 08:13 hello-world-war-1.0.0
-rw-r--r-- 1 root root 2403 8月 7 08:13 hello-world-war-1.0.0.war
drwxr-xr-x 2 root root 28 8月 7 08:13 maven-archiver
[root@jenkins-201 ~]#
cd /var/lib/jenkins/workspace/java-helloworld/target/
scp hello-world-war-1.0.0.war 10.0.0.7:/tmp/
注意:以下操作在web服务器进行
yum install java -y
tar xf apache-tomcat-8.0.27.tar.gzls -C /opt/
cd /opt
ln -s apache-tomcat-8.0.27 tomcat
rm -rf /opt/tomcat/webapps/*
cp /tmp/hello-world-war-1.0.0.war /opt/tomcat/webapps/ROOT.war
/opt/tomcat/bin/startup.sh
测试完成后我们将war包删掉,后续我们使用jenkins自动部署
rm -rf /opt/tomcat/webapps/ROOT.war
cat > /scripts/jenkins/java_deploy.sh <<'EOF'
#!/bin/bash
PATH_CODE=/var/lib/jenkins/workspace
PATH_WEB=/opt/tomcat/webapps
IP=10.0.0.7
#拷贝war包发送到web服务器代码目录
code_scp(){
ssh ${IP} "mkdir ${PATH_WEB}/java-${git_version} -p"
scp ${PATH_CODE}/java-helloworld/target/*.war ${IP}:${PATH_WEB}/java-${git_version}
}
#web服务器解压代码
code_unzip(){
ssh ${IP} "cd ${PATH_WEB}/java-${git_version} && unzip *.war && rm -rf *.war"
}
#创建代码软链接
code_ln(){
ssh ${IP} "cd ${PATH_WEB} && rm -rf ROOT && ln -s java-${git_version} ROOT"
}
#重启tomcat
restart_tomcat(){
ssh ${IP} "cd /opt/tomcat/bin && ./shutdown.sh && ./startup.sh"
}
main(){
code_scp
code_unzip
code_ln
}
#选择发布还是回滚
if [ "${deploy_env}" == "deploy" ]
then
ssh ${IP} "ls ${PATH_WEB}/java-${git_version}" >/dev/null 2>&1
if [ $? == 0 -a ${GIT_COMMIT} == ${GIT_PREVIOUS_SUCCESSFUL_COMMIT} ]
then
echo "java-${git_version} 已部署,不允许重复构建"
exit
else
main
restart_tomcat
fi
elif [ "${deploy_env}" == "rollback" ]
then
code_ln
restart_tomcat
fi
EOF
最后还需要配置一下git仓库地址,注意需要修改拉取的版本的变量为 $git_version
jenkins构建查看会发现已经有了选项,但是因为还没提交代码,所以版本号还没有:
删除以前的标签:
git tag
git tag -d v1.0 v2.0 v3.0 v4.0
发布v2.0版本
cd hello-world-war/src/main/webapp/
git checkout master
git pull
git checkout -b v2.0
vim src/main/webapp/index.jsp
git status
git add .
git commit -m "v2.0 稳定版"-
git push -u origin v2.0
发布v3.0版本
cd hello-world-war/src/main/webapp/
git checkout master
git pull
git checkout -b v3.0
vim src/main/webapp/index.jsp
git status
git add .
git commit -m "v3.0 稳定版"
git push -u origin v3.0
发布v2.0版本:
web服务器查看:
目录查看:
[root@web-7 ~]# ll /opt/tomcat/webapps/
总用量 0
drwxr-xr-x 4 root root 54 8月 7 09:26 java-v2.0
lrwxrwxrwx 1 root root 9 8月 7 09:27 ROOT -> java-v2.0
测试发布v3.0版本:
浏览器查看:
代码目录查看
[root@web-7 ~]# ll /opt/tomcat/webapps/
总用量 0
drwxr-xr-x 4 root root 54 8月 7 09:26 java-v2.0
drwxr-xr-x 4 root root 54 8月 7 09:39 java-v3.0
lrwxrwxrwx 1 root root 9 8月 7 09:39 ROOT -> java-v3.0
当前版本为v3.0,我们选择回滚到v2.0
web浏览器查看发现已经回滚成功了:
查看代码目录:
[root@web-7 ~]# ll /opt/tomcat/webapps/
总用量 0
drwxr-xr-x 4 root root 54 8月 7 09:26 java-v2.0
drwxr-xr-x 4 root root 54 8月 7 09:39 java-v3.0
lrwxrwxrwx 1 root root 9 8月 7 09:41 ROOT -> java-v2.0
cat > /opt/sonar-scanner/conf/sonar-scanner.properties <<'EOF'
sonar.host.url=http://10.0.0.203:9000
sonar.login=a82e776ea9fe8efa91c53d041d969ee23d2a27d1
sonar.sourceEncoding=UTF-8
EOF
参数命令:
sonar.projectName=${JOB_NAME}
sonar.projectKey=java
sonar.sources=.
cd hello-world-war/src/main/webapp/
git checkout master
git pull
git checkout -b v4.0
vim src/main/webapp/index.jsp
git status
git add .
git commit -m "v4.0 稳定版"-
git push -u origin v4.0
web服务状态:
sonar状态: