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登錄服務器的路徑之上。沒有關注,打包好的文件路徑,是工做空間的相對路徑。修改文件的路徑和前綴後自動傳包成功。

相關文章
相關標籤/搜索