Py学习  »  Git

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

litblank • 4 年前 • 249 次点击  
阅读 52

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

一、目标

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

二、搭建gitlab私服

1、搭建
# 依赖:
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

复制代码
2、迁移项目,包含分支,提交记录
#从旧址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
复制代码
3、参考链接:
https://cloud.tencent.com/developer/article/1333790
http://www.luyixian.cn/news_show_9166.aspx
复制代码

三、搭建maven私服nexus

1、搭建
# 官网下载
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
复制代码
2、创建私服
  • 新建私服Ihhb_repository,hosted类型,用于存放依赖的jar,内部项目的发布仓库
  • 新建私服Ihhb_snapshot,hosted类型,version policy 选择SNAPSHOT,Deployment policy选择allow redeploy。用于存放SNAPSHOT快照
  • 修改maven-public 默认,group类型,构建依赖于aliyun,私有仓库的中心仓库
  • 新建aliyun-repository,proxy类型,从阿里云下载

image

3、手动上传快照SNAPSHOT

已-SNAPSHOT结尾的jar,不能通过页面上传,需要在项目pom文件配置,或手动上传,这里因为没有源码,只有jar手动上传。

# 修改并运行命令,
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
复制代码
4、编写setting文件

***注意

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

***下载不下来快照

  • setting文件配置有问题

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

  • 重新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>

复制代码

四、配置jenkins

1、安装
# 官网下载


    

https://jenkins.io/download/

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

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

安装插件

发布插件 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 
复制代码

系统设置修改

image
SSH Servers,添加需要部署的远程服务器,因为服务器ssh配置了免密登陆,所以不需要配置其他。高级也可以输入密码。
image

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

全局工具配置修改

修改setting

image
修改JDK
image
修改maven
image

3、创建项目

构建maven项目

image

保留2次构建的记录

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

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38317
 
249 次点击