jenkins,git私服,maven私服实现自动部署

1、目标

  • 搭建gitlab私服,并迁移项目
  • 搭建maven私服
  • 基于私服,配置jenkins实现自动部署

2、搭建gitlab私服

一、搭建
# 依赖:
sudo yum install -y curl policycoreutils-python openssh-server

#启动ssh服务&设置为开机启动
sudo systemctl enable sshd
sudo systemctl start sshd

curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash

# 下载
sudo yum install -y gitlab-ce

# 出现图标表示成功
#修改 /etc/gitlab/gitlab.rb文件
vi /etc/gitlab/gitlab.rb

# 默认的站点Url配置项
external_url 'http://gitlab.example.com'
# 默认的端口是80,修改默认端口
nginx['listen_port'] = 91

#从新配置并启动
sudo gitlab-ctl reconfigure

复制代码
二、迁移项目,包含分支,提交记录
#从旧址clone下来
git clone --bare gitlab@github.news.com/groups:test.com.git
# push到新址
git push --mirror gitlab@github.xx.com:newgroups/newtest.com.git
# 将本地的url修改成新的url
git remote set-url origin gitlab@github.xx.com:newgroups/newtest.com.git
复制代码
三、参考连接:
https://cloud.tencent.com/developer/article/1333790
http://www.luyixian.cn/news_show_9166.aspx
复制代码

3、搭建maven私服nexus

一、搭建
# 官网下载
https://www.sonatype.com/download-oss-sonatype

nexus-3.13.0-01-unix.tar.gz
# 解压
tar -zxvf apache-maven-3.5.2-bin.tar.gz
mv nexus-3.6.0-02 /usr/local/
cd /usr/local/nexus-3.6.0-0.2/bin

# 启动 
./nexus start
复制代码
二、建立私服
  • 新建私服Ihhb_repository,hosted类型,用于存放依赖的jar,内部项目的发布仓库
  • 新建私服Ihhb_snapshot,hosted类型,version policy 选择SNAPSHOT,Deployment policy选择allow redeploy。用于存放SNAPSHOT快照
  • 修改maven-public 默认,group类型,构建依赖于aliyun,私有仓库的中心仓库
  • 新建aliyun-repository,proxy类型,从阿里云下载

image

三、手动上传快照SNAPSHOT

已-SNAPSHOT结尾的jar,不能经过页面上传,须要在项目pom文件配置,或手动上传,这里由于没有源码,只有jar手动上传。java

# 修改并运行命令,
mvn deploy:deploy-file 
-DgroupId=com.ucf.um 
-DartifactId=visa  
-Dversion=1.0.12-SNAPSHOT  
-Dpackaging=jar 
-Dfile=C:/Users/chenyd/Desktop/visa/visa/1.0.12-SNAPSHOT/visa-1.0.12-SNAPSHOT.jar 
-Durl=http://172.20.0.10:8081/repository/Ihhb_snapshot/ 
-DrepositoryId=maven-snapshot

# 解释
Durl须要是SNAPSHOT仓库,
DrepositoryId是setting文件中配置的帐号ID
复制代码
四、编写setting文件

***注意python

  • servers不正确可能致使上传快照认证错误
  • activeProfiles必定要写,
  • mirrors配置一个仓库,一个快照仓库

***下载不下来快照nginx

  • setting文件配置有问题

***包都有,项目正常启动,可是idea maven project 报错git

  • 从新open,pom 项目
<!--
maven配置文件
-->
<settings>
	<!-- 发布到私服 -->

	<mirrors>
          <mirror>
            <!--This sends everything else to /public -->
            <id>maven-public</id>
            <mirrorOf>*</mirrorOf>
            <url>http://172.20.0.10:8081/repository/maven-public/</url>
        </mirror>  
	    <mirror>
            <!--This sends everything else to /public -->
            <id>maven-snapshot</id>
            <mirrorOf>Ihhb_snapshot</mirrorOf>
            <url>http://172.20.0.10:8081/repository/Ihhb_snapshot/</url>
        </mirror>  
    </mirrors>


	<profiles>
		<profile>
			<id>development</id>
			<repositories>
				<repository>
					<id>central</id>
					<url>http://central</url>
					<releases>
						<enabled>true</enabled>
					
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</repository>
			</repositories>
			<pluginRepositories>
				<pluginRepository>
					<id>central</id>
					<url>http://central</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</pluginRepository>
			</pluginRepositories>
		</profile>
		<profile>
			<!--this profile will allow snapshots to be searched when activated-->
			<id>Ihhb_snapshot</id>
			<repositories>
				<repository>
					<id>Ihhb_snapshot</id>
					<url>http://Ihhb_snapshot</url>
					<releases>
						<enabled>false</enabled>
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</repository>
			</repositories>
			<pluginRepositories>
				<pluginRepository>
					<id>Ihhb_snapshot</id>
					<url>http://Ihhb_snapshot</url>
					<releases>
						<enabled>false</enabled>
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</pluginRepository>
			</pluginRepositories>
		</profile>
	</profiles>
	<activeProfiles>
		<activeProfile>Ihhb_snapshot</activeProfile>
	</activeProfiles>
	<localRepository>/data/apache-maven-3.6.1/maven_repository</localRepository>

	<!-- 发布的服务器和密码,暂时未限制权限,请不要提交不稳定版本 -->
	<servers>
		<server>
			<id>maven-public</id>
			<username>admin</username>
			<password>admin123</password>
		</server>
		<server>
			<id>maven-snapshot</id>
			<username>admin</username>
			<password>admin123</password>
		</server>
	</servers>

</settings>

复制代码

4、配置jenkins

一、安装
# 官网下载
https://jenkins.io/download/

# 能够tomcat启动,也可直接启动,修改端口
java -jar jenkins.war --ajp13Port=-1 --httpPort=8090

# 访问采用新手安装
复制代码
二、配置

安装插件github

发布插件 Deploy to container Plugin 必须
Maven插件 Maven Integration plugin必须
git插件 Git plugin
如须要git根据target部署,须要Dynamic Parameter Plug-in 插件,这个插件下载不了,CSDN收费,这里提供免费网盘

连接:https://pan.baidu.com/s/1hvl_URW8s1mvLIRv1jFhUg 
提取码:rnw7 
复制代码

系统设置修改apache

image
SSH Servers,添加须要部署的远程服务器,由于服务器ssh配置了免密登录,因此不须要配置其余。高级也能够输入密码。
image

SSH登录配置
# 登录服务器生成密钥
ssh-keygen
# 发送公钥到须要目标服务器上
ssh-copy-id -i /root/.ssh/id_rsa.pub root@目标服务器主机ip
# 登录
ssh root@目标服务器主机ip
复制代码

全局工具配置修改tomcat

修改setting bash

image
修改JDK
image
修改maven
image

三、建立项目

构建maven项目服务器

image

保留2次构建的记录app

image

获取git源码

image

服务器生成的密钥对,私钥用于gitlab文件建立

image

公钥用于添加凭证

image

maven打包跳过测试,选择编译的环境

image

编译后执行的脚本

image

目标服务器上的脚本

不知道什么缘由,Jenkins构建好的包没有上传到远端服务器,因此写脚本传输,启动。

echo "切换到项目地址"
cd /apps/product/exch

echo "关闭exch-platform.jar进程"
ps -ef|grep java|grep exch-platform.jar|awk '{print $2}'|xargs kill -9

echo "删除包"
rm -rf exch-platform.jar

echo "传包"
scp  root@172.20.0.10:/root/.jenkins/workspace/exch-test-0.13/exch_platform/target/exch-platform.jar /apps/product/exch

echo "启动"
/apps/product/jdk1.8.0_181-amd64/bin/java -jar exch-platform.jar > nohup.out &


echo "完成"
复制代码

***注意

  • 由于启动的是jar包,脚本中nohup命令会致使,前台打印日志,最终同time out。因此采用nohup sh /apps/jenkins/exch.sh启动,命令采用> nohup.out & ,这样保证了日志的数据,而且服务不会被Jenkins杀掉。
  • nohup 不挂断,& 后台运行

最终成果

image

更新

没有自动传包到远端服务器的缘由,是由于我只关注了,构建后登录的地址是在配置SSH登录服务器的路径之上。没有关注,打包好的文件路径,是工做空间的相对路径。修改文件的路径和前缀后自动传包成功。

相关文章
相关标签/搜索