为了账号安全,请及时绑定邮箱和手机立即绑定

『互联网架构』软件架构-jenkins搭建和 maven gitlab自动化部署流程(下)(六)

上次基本把环境都搭建好了,下一步就是开始完成整个流程的自动化部署。源码:https://github.com/limingios/netFuture/tree/master/jenkins/

环境回顾

应用 IP地址 服务 端口 安装应用 安装方式
gitlab 192.168.66.100 gitlab 10080 gitlab docker
jenkins 192.168.66.101 jenkins 8888 jdk8 maven3.2 git2.8 shell
nexus 192.168.66.102 nexus 8081 nexus docker
tomcat 192.168.66.103 tomcat 8080 tomcat docker
IP地址 端口 用户名 密码
192.168.66.100 10080 root 123456789qwe
192.168.66.101 8888 root 8d31833e277c4b579a3be35fe2bdc7d4
192.168.66.102 8081 admin admin123
192.168.66.103 8080

本地项目部署到100上gitlab

  • 本地代码

代码比较简单为的是流程


  • 本地提交到gitlab上

修改配置,可以非本地化提交代码

可以新建用户建立仓库,或者用root建立仓库。

提示没有上传ssh 秘钥

查看本地的ssh秘钥,window环境直接使用git bash

ssh-keygen -t rsa -C "394498036@qq.com"
cat /c/Users/Administrator/.ssh/id_rsa.pub

查看jenkins的ssh秘钥,101环境直接使

ssh-keygen -t rsa -C "394498036@qq.com"
cat /root/.ssh/id_rsa.pub

本地windows 和 101的jenkins添加完毕

本地windows 代码提交

git init
git add .
git commit -m "注释语句"
git remote add origin ssh://git@192.168.66.100:10022/root/idig8.git
git push -u origin master

本地项目部署到101上 jenkins

  • 全局安全性配置

  • 创建任务

口令随意写,等下要在gitlab做触发设置
Use the following URL to trigger build remotely: JENKINS_URL/job/idig8/build?token=TOKEN_NAME 或者 /buildWithParameters?token=TOKEN_NAME

100 gitlab触发器的设置
http://192.168.66.101:8888/job/idig8/build?token=123456
token的值就是jenkins上设置的。

等下有专门的对pipeline的编写

保存

101的配置私服nexus

cd .m2
pwd
vi settings.xml

settings.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">

  <pluginGroups></pluginGroups>
  <proxies></proxies>

  <servers>
      <server>
      <id>nexus-releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
    <server>
      <id>nexus-snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
  </servers>

  <mirrors> 
    <mirror> 
      <id>nexus-releases</id> 
      <mirrorOf>*</mirrorOf> 
      <url>http://192.168.66.102:8081/nexus/content/groups/public</url> 
    </mirror>
    <mirror> 
      <id>nexus-snapshots</id> 
      <mirrorOf>*</mirrorOf> 
      <url>http://192.168.66.102:8081/nexus/content/repositories/snapshots</url> 
    </mirror> 
  </mirrors> 
 
  <profiles>
   <profile>
      <id>nexus</id>
      <repositories>
        <repository>
          <id>nexus-releases</id>
          <url>http://nexus-releases</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
          <id>nexus-snapshots</id>
          <url>http://nexus-snapshots</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
         <pluginRepository>
                <id>nexus-releases</id>
                 <url>http://nexus-releases</url>
                 <releases><enabled>true</enabled></releases>
                 <snapshots><enabled>true</enabled></snapshots>
               </pluginRepository>
               <pluginRepository>
                 <id>nexus-snapshots</id>
                  <url>http://nexus-snapshots</url>
                <releases><enabled>true</enabled></releases>
                 <snapshots><enabled>true</enabled></snapshots>
             </pluginRepository>
         </pluginRepositories>
    </profile>
  </profiles>

  <activeProfiles>
      <activeProfile>nexus</activeProfile>
  </activeProfiles>
 
</settings>

安装sshpass,ssh登陆不能在命令行中指定密码。sshpass的出现,解决了这一问题。sshpass用于非交互SSH的密码验证,一般用在sh脚本中,无须再次输入密码。

 yum -y install sshpass

测试gitlab和jenkins

正式编写

  • pipeline
#!groovy
pipeline {
	agent any
	//环境变量,
	environment {
		REPOSITORY="ssh://git@192.168.66.100:10022/root/idig8.git"
		MODULE="idig8"
		SCRIPT_PATH="/root/"
		remoteHost_tomcat= '192.168.66.103'
	
	}
	//流水线是如何提前,都是通过很多个stages下面的stage
	stages {
		stage('获取代码'){
			steps{
				echo " start fetch code from git ssh://git@192.168.66.100:10022/root/idig8.git"
				deleteDir()
				git "${REPOSITORY}"
			}
		}
		stage('代码静态检查') {
			steps{
				echo " start code check"
			}
		}
		stage('编译+单元测试') {
			steps{
				echo " start compile"
				 sh"""
         cd $workspace/idig8/common-parent/
         mvn  -U clean install -Dmvn.test.skip=true -Ptest
         """
			}
		}
		
		stage('jar包上传到nexus上') {
			steps{
				echo " start maven update jar"
				 sh"""
         cd $workspace/idig8/common-parent/common-utils
         mvn clean deploy
         cd $workspace/idig8/common-parent/common-dao
         mvn clean deploy
         cd $workspace/idig8/common-parent/common-service
         mvn clean deploy
         """
			}
		}
		
		stage('移动目录到用户目录下') {
			steps{
				echo "mv idig8.war"
				 sh"""
         mv -f $workspace/idig8/common-parent/common-web/target/common-web.war ${SCRIPT_PATH}${MODULE}.war
         """
			}
		}
		stage('拷贝文件到tomcat指定目录下') {
		
			steps{
				echo "scp 目标文件"
				 sh"""
         sshpass -p 'vagrant' ssh -p 22 -o stricthostkeychecking=no root@${remoteHost_tomcat} 'rm -rf /root/tomcat/tomcat-persistence/tomcat/data/${MODULE}*';
         sshpass -p 'vagrant' scp -P 22 -o stricthostkeychecking=no ${SCRIPT_PATH}${MODULE}.war root@${remoteHost_tomcat}:/root/tomcat/tomcat-persistence/tomcat/data/${MODULE}.war;
         """
         echo "打包完毕美滋滋 刷新tomcat查看吧"
			}
		}
		
	}
}

只要push代码流水线自动开启工作,真是美滋滋啊

结果20轮的pipeline的编写终于成功了
删除docker挂载的idig所有的目录,然后替换新的jar包放进去

PS:完成自动化部署,终于完成了 ,在自己的虚拟机环境下搞了3天,感觉好有成就感,首选shell脚本部署学习,pipeline并不是那么麻烦。里面多半是用shell的方式搞定了,多亏自己搞了2年shell的开发。没忘记,必须前2年了解的东西确定对自己的以后的路起关键作用。ssh的公钥也在里面起到了很重要的作用,希望老铁按照我的思路可以轻松的完成部署。感谢老铁的支持,本来想2篇写在一起的,后来又拆开了,因为内容太充实了。为了搞这个每天怼到晚上2点,下班就搞不容易啊。那就拜了个拜~来不及握手!

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
全栈工程师
手记
粉丝
1.7万
获赞与收藏
1318

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消