天天看點

Jenkins自動釋出(5)-建立任務自動化部署程式(javaweb+maven+gitlab+tomcat)

自動化部署流程

Jenkins自動釋出(5)-建立任務自動化部署程式(javaweb+maven+gitlab+tomcat)

程式項目結構

Jenkins自動釋出(5)-建立任務自動化部署程式(javaweb+maven+gitlab+tomcat)

deploy.sh 自動化部署指令腳本

jenkins将會遠端執行deploy.sh腳本自動化部署程式到tomcat

#!/bin/bash

# $PROJ_PATH:jenkins遠端工作目錄,執行腳本時傳入
# $TOMCAT_PATH:tomcat目錄,執行腳本時傳入

# 關閉tomcat
killTomcat()
{
  pid=`ps -ef|grep tomcat|grep java|awk '{print $2}'`
  echo "tomcat id list :$pid"
  if [ "$pid" = "" ]
  then 
      echo "no tomcat pid alive"
  else
       kill -9 $pid
  fi
}

# 進入遠端工作目錄
cd $PROJ_PATH/jenkinsWeb
# maven打包程式
mvn clean install
# 結束tomcat程序
killTomcat

# 删除舊檔案
rm -rf $TOMCAT_PATH/webapps/ROOT
rm -f $TOMCAT_PATH/webapps/ROOT.war
rm -f $TOMCAT_PATH/webapps/jenkinsWeb.war

# 拷貝檔案
cp $PROJ_PATH/jenkinsWeb/target/jenkinsWeb.war $TOMCAT_PATH/webapps/

# 進入tomcat/webapps目錄
cd $TOMCAT_PATH/webapps/

# 重新命名
mv jenkinsWeb.war ROOT.war

# 進入tomcat目錄
cd $TOMCAT_PATH/

# 重新啟動tomcat
sh bin/startup.sh
           

Git位址

[email protected].199.140:chy2z/jenkinsWeb.git
           

Tomcat伺服器環境

ip:192.168.199.106
jdk:1.8
maven:3.3.9
tomcat:8.5.32
           

Jenkins建立任務

任務名稱:DeployJenkinsWeb

Jenkins自動釋出(5)-建立任務自動化部署程式(javaweb+maven+gitlab+tomcat)

配置General

Jenkins自動釋出(5)-建立任務自動化部署程式(javaweb+maven+gitlab+tomcat)
節點jenkinsWeb的遠端工作目錄:/root/.jenkins

tomcat運作在jenkinsWeb節點上
           

配置Git

Local subdirectory for repo: 是配置jenkins下載下傳git代碼的子目錄

本示例代碼下載下傳目錄:/root/.jenkins/workspace/DeployJenkinsWeb/jenkinsWeb
           
Jenkins自動釋出(5)-建立任務自動化部署程式(javaweb+maven+gitlab+tomcat)

配置建構

選擇執行shell(自動部署時在遠端伺服器執行shell指令)

Jenkins自動釋出(5)-建立任務自動化部署程式(javaweb+maven+gitlab+tomcat)
# 防止關閉jenkins程序
BUILD_ID=DONTKILLME

# 配置環境變量
. /etc/profile

# jenkins遠端工作目錄
export PROJ_PATH=`pwd`

# 遠端tomcat目錄
export TOMCAT_PATH=/usr/local/apache-tomcat-8.5.32

# 執行遠端腳本
sh $PROJ_PATH/jenkinsWeb/deploy.sh
           

 儲存即可!

立即建構

Jenkins自動釋出(5)-建立任務自動化部署程式(javaweb+maven+gitlab+tomcat)

檢視建構結果

點選 #1,然後點選  控制台輸出,檢視結果

由使用者 admin 啟動
在遠端節點 jenkinsWeb 上建構 在工作空間 /root/.jenkins/workspace/DeployJenkinsWeb 中
Cloning the remote Git repository
Cloning repository [email protected]:chy2z/jenkinsWeb.git
 > /usr/bin/git init /root/.jenkins/workspace/DeployJenkinsWeb/jenkinsWeb # timeout=10
Fetching upstream changes from [email protected]:chy2z/jenkinsWeb.git
 > /usr/bin/git --version # timeout=10
using GIT_SSH to set credentials gitlab-ssh-key
 > /usr/bin/git fetch --tags --progress [email protected]:chy2z/jenkinsWeb.git +refs/heads/*:refs/remotes/origin/*
 > /usr/bin/git config remote.origin.url [email protected]:chy2z/jenkinsWeb.git # timeout=10
 > /usr/bin/git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > /usr/bin/git config remote.origin.url [email protected]:chy2z/jenkinsWeb.git # timeout=10
Fetching upstream changes from [email protected]:chy2z/jenkinsWeb.git
using GIT_SSH to set credentials gitlab-ssh-key
 > /usr/bin/git fetch --tags --progress [email protected]:chy2z/jenkinsWeb.git +refs/heads/*:refs/remotes/origin/*
 > /usr/bin/git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > /usr/bin/git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 3bdaa3b3784ec67f68e91d1b016693bfa36afe04 (refs/remotes/origin/master)
 > /usr/bin/git config core.sparsecheckout # timeout=10
 > /usr/bin/git checkout -f 3bdaa3b3784ec67f68e91d1b016693bfa36afe04
Commit message: "workspace"
First time build. Skipping changelog.
[DeployJenkinsWeb] $ /bin/sh -xe /tmp/jenkins4953122394921675090.sh
+ BUILD_ID=DONTKILLME
+ . /etc/profile
...................省略部分内容......................
++ pwd
+ export PROJ_PATH=/root/.jenkins/workspace/DeployJenkinsWeb
+ PROJ_PATH=/root/.jenkins/workspace/DeployJenkinsWeb
+ export TOMCAT_PATH=/usr/local/apache-tomcat-8.5.32
+ TOMCAT_PATH=/usr/local/apache-tomcat-8.5.32
+ sh /root/.jenkins/workspace/DeployJenkinsWeb/jenkinsWeb/deploy.sh
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building jenkinsWeb Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://maven.aliyun.com/nexus/content/repositories/central/org/apache/maven/plugins/maven-clean-plugin/3.0.0/maven-clean-plugin-3.0.0.pom
4/5 KB   
5/5 KB   
         
...................省略部分内容......................           

Downloaded: http://maven.aliyun.com/nexus/content/repositories/central/commons-codec/commons-codec/1.6/commons-codec-1.6.jar (228 KB at 516.6 KB/sec)
[INFO] Installing /root/.jenkins/workspace/DeployJenkinsWeb/jenkinsWeb/target/jenkinsWeb.war to /root/.m2/repository/com/chy/jenkinsWeb/1.0-SNAPSHOT/jenkinsWeb-1.0-SNAPSHOT.war
[INFO] Installing /root/.jenkins/workspace/DeployJenkinsWeb/jenkinsWeb/pom.xml to /root/.m2/repository/com/chy/jenkinsWeb/1.0-SNAPSHOT/jenkinsWeb-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 59.846 s
[INFO] Finished at: 2018-09-02T23:24:59+08:00
[INFO] Final Memory: 14M/180M
[INFO] ------------------------------------------------------------------------
tomcat id list :5536
Using CATALINA_BASE:   /usr/local/apache-tomcat-8.5.32
Using CATALINA_HOME:   /usr/local/apache-tomcat-8.5.32
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-8.5.32/temp
Using JRE_HOME:        /usr/local/jdk1.8.0_181/jre
Using CLASSPATH:       /usr/local/apache-tomcat-8.5.32/bin/bootstrap.jar:/usr/local/apache-tomcat-8.5.32/bin/tomcat-juli.jar
Tomcat started.
Finished: SUCCESS
           

  看到 finished:success說明建構成功

#1建構運作結果

Jenkins自動釋出(5)-建立任務自動化部署程式(javaweb+maven+gitlab+tomcat)

編輯代碼重新送出git,#2建構運作結果

Jenkins自動釋出(5)-建立任務自動化部署程式(javaweb+maven+gitlab+tomcat)