最近,也是抽空整理了一些在工做中積累的經驗,經過博客記錄下來分享給你們,但願能對你們有所幫助;java
關於自動化部署的優勢,我就不在這裏贅述了;只要想一想手工打包、上傳、部署、重啓的種種,就會有不少場景歷歷在目,相信經歷過的朋友都能體會其中的酸甜苦辣;node
而一旦到了大型項目,好比所微服務化以後的項目,不只僅功能模塊多,並且都再也不是單機部署;而且一搞大型活動就是動不動幾十個節點的大集羣部署,想要靠手工再來完成這些操做,那就等着玩死本身吧;git
那麼,若是把這一切都交給Jenkins來管理,你要作的就是在頁面輕點鼠標,接下來就是刷刷手機、喝喝茶。。。哈哈哈哈web
--centos 7shell
--Jenkins v.2.121.3apache
--JDK 1.8json
--SpringBoot+Maven+Gitcentos
這裏,咱們藉助了maven-assembly-plugin來完成打包,操做以下;服務器
<build> <finalName>bm-demo-admin</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <!--指定main入口--> <mainClass>cn.com.bluemoon.admin.web.WebAdminApplication</mainClass> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> </manifest> </archive> <excludes> <include>*.xml</include> <include>*.yml</include> <include>*.json</include> </excludes> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <configuration> <!-- not append assembly id in release file name --> <appendAssemblyId>false</appendAssemblyId> <descriptors> <!--打包的詳細描述,須要配置額外文件--> <descriptor>src/assembly/assembly-descriptor.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
<?xml version="1.0" encoding="UTF-8"?> <assembly> <id>bin</id> <!-- 最終打包成一個用於發佈的zip文件 --> <formats> <format>tar.gz</format> </formats> <fileSets> <!-- 打包jar文件 --> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory></outputDirectory> <includes> <include>*.jar</include> </includes> <fileMode>0755</fileMode> <lineEnding>unix</lineEnding> </fileSet> <!-- 把項目相關的啓動腳本,打包進zip文件的bin目錄 --> <fileSet> <directory>${project.basedir}/src/main/scripts</directory> <outputDirectory>/</outputDirectory> <includes> <include>*</include> </includes> <fileMode>0755</fileMode> <lineEnding>unix</lineEnding> </fileSet> <!-- 把項目的配置文件,打包進zip文件的config目錄 --> <fileSet> <directory>${project.build.directory}/classes</directory> <outputDirectory>conf</outputDirectory> <includes> <include>*.xml</include> <include>*.yml</include> <include>*.json</include> </includes> </fileSet> </fileSets> <!-- 把項目的依賴的jar打包到lib目錄下 --> <dependencySets> <dependencySet> <outputDirectory>lib</outputDirectory> <scope>runtime</scope> <excludes> <exclude>${groupId}:${artifactId}</exclude> </excludes> </dependencySet> </dependencySets> </assembly>
restart.sh併發
#!/bin/sh ./stop.sh ./start.sh
start.sh
#!/bin/sh export JAVA_HOME=$JAVA_HOME export PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar PIDFILE=service.pid ROOT_DIR="$(cd $(dirname $0) && pwd)" CLASSPATH=./*:$ROOT_DIR/lib/*:$ROOT_DIR/conf/ JAVA_OPTS="-Xms256m -Xmx512m -XX:+UseParallelGC" MAIN_CLASS=cn.com.bluemoon.admin.web.WebAdminApplication if [ ! -d "logs" ]; then mkdir logs fi if [ -f "$PIDFILE" ]; then echo "Service is already start ..." else echo "Service start ..." nohup java $JAVA_OPTS -cp $CLASSPATH $MAIN_CLASS 1> logs/bm-demo-admin.out 2>&1 & printf '%d' $! > $PIDFILE echo "Service start SUCCESS " fi
stop.sh
#!/bin/sh PIDFILE=service.pid if [ -f "$PIDFILE" ]; then kill -9 `cat $PIDFILE` rm -rf $PIDFILE echo "Service is stop SUCCESS!" else echo "Service is already stop ..." fi
這裏就不在介紹如何安裝Jenkins,以及Jenkins的環境配置,包括git,mave,node.js等,網上有不少的博客講這一塊,能夠本身找一下;
這裏有必要解釋一下添加的參數
brunch git的分支,做爲部署時的可修改參數
target_host 部署的目標機器,能夠是ip,也能夠在hosts裏面添加代理(後面補充)
war_path 項目打包完成以後的包所在路徑
deploy_path 部署在目標服務器上面的路徑
app_name 部署的應用的名稱
tar_name 打包完成以後的壓縮包的名字(這裏不是達成jar,而是壓縮包)
cd /data/ansible/jenkins-ansible-supervisor-deploy ansible-playbook -i hosts om-platform.yml --verbose --extra-vars "target_host=$target_host
deploy_path=$deploy_path deploy_war_path=$WORKSPACE/$war_path tar_name=$tar_name app_name=$app_name"
如上是執行的shell命令,這裏解釋兩個文件,一個是om-platform.yml,內容貼出來看一下:
--- # This playbook deploys a simple standalone Tomcat 7 server. - hosts: "{{ target_host }}" user: appadm roles: - om-platform-deploy
一個是hosts 也就是host文件,在上面target_host中配置了別名的話,就須要在此處的hosts文件中定義:
好比你要部署的節點服務是兩個節點的,那你就能夠針對240.62/63添加一個叫bm_mana_11_11_test的別名,那麼部署的時候在target_host參數中添加別名代替就能夠一次部署完畢了;
最後保存,任務就新建完了,固然,若是有其餘的須要你能夠本身行選擇;
九、在目標服務器上的部署目錄/home/appadm 下添加init.sh文件,內容以下:
# init.sh 初始化項目 #!/bin/sh serverName=$1 if [ ! $serverName ]; then echo "請輸入正確的啓動服務包名。。" else #echo "$serverName 正準備啓動,請稍候。。。" cd "/home/appadm/$serverName" echo "/home/appadm$serverName" #./start.sh #nohup java -jar $serverName & #echo "the ${serverName} 啓動完成。。。" fi
通常,咱們在部署時,就會操做這個界面來修改參數部署
這裏要補充說明的是,jenkins的機器和目標服務器之間是須要作SSH KEY的,關於怎麼完成這一步其實也很簡單,就是生成信任的key,具體操做就由度娘來介紹吧;
那麼,到這裏關於怎麼完成項目的自動化部署,基本上就介紹完了,開始構建,並構建成功以後,就會在自動去git拉去代碼,而且打包完成併發送到對應的服務器目錄,完成重啓操做;
若是有什麼疑問,歡迎你們提問和討論;若有不對之處,懇請批評指正;若是以爲對您有所幫忙,那麼也請您賞個贊;