1. 程式人生 > >Devops關鍵工具及技術(五)—基於Pipeline的Bash指令碼部署

Devops關鍵工具及技術(五)—基於Pipeline的Bash指令碼部署

接下來我們將會為Pipeline加上部署的Stage,部署採用sh指令碼進行部署。後續我們將會採用Ansible的自動化部署。

Sh指令碼

沿用之前的Spring-boot工程,經過持續整合後,我們將會得到maven構建後的一個jar包,這個jar即是工程的啟動jar包。而我們需要把這個jar部署到另外一臺機器上,實現從持續整合、程式碼靜態掃描、以及部署的整個過程 。 首先我們看一下sh指令碼的內容:

#!/bin/bash
id=$(ps -ef|grep sample.jar|grep -v grep|awk '{print $2}')   //查找出名稱中sample.jar的程序
if [ ! $id ]; then echo "process not started"; else kill -9 $id;fi    //如果存在,則kill掉
sleep 2s;   //sleep 2秒
nohup java -jar /tmp/sample.jar  > nohup.out &   //java -jar 啟動jar

Pipeline

Pipeline內容

pipeline {
    agent none 
    stages {
       stage('Preparation') { 
            agent { node { label 'master' } }
            steps {
               checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'binbin', url: 'https://github.com/zbbkeepgoing/springboot-demo.git']]])
            }
        }
         
        stage('Build') { 
            agent { node { label 'master' } }
            steps {
                dir(env.WORKSPACE){
                  sh "mvn clean install"
                  junit allowEmptyResults: true, keepLongStdio: true, testResults: 'target/surefire-reports/*.xml'
                  sh "mv target/sample-0.0.1-SNAPSHOT.jar target/sample.jar"
                }
            }
        }
        stage('Sonarqube') { 
            agent { node { label 'master' } }
            steps {
                dir(env.WORKSPACE){
                  sh "mvn sonar:sonar -Dsonar.host.url=http://192.168.88.130:9000 -Dsonar.login=2382ac098363521b98731e286e52e1ad22adef2b"    //指定sonar的ip和token
                }
            }
        }
        stage('Deploy') { 
            agent { node { label 'master' } }
            steps {
                dir(env.WORKSPACE){
                   sh 'sshpass -p 
[email protected]
scp -o StrictHostKeychecking=no target/sample.jar [email protected]:/opt/ansible' //把部署的jar傳到目標機器上 sh 'sshpass -p [email protected] scp -o StrictHostKeychecking=no deploy.sh [email protected]:/opt/ansible' //把指令碼傳到目標機器上 sh 'sshpass -p
[email protected]
ssh -o StrictHostKeychecking=no [email protected] "bash /opt/ansible/deploy.sh"' //在目標機器上執行對應的指令碼 sh 'sleep 8s' //睡眠8s } } } } }

新建Pipeline

在這裡插入圖片描述

執行Pipeline

Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] stage
[Pipeline] { (Preparation)
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/CI+Sonar+Sh
[Pipeline] {
[Pipeline] checkout
Cloning the remote Git repository
Cloning repository https://github.com/zbbkeepgoing/springboot-demo.git
 > git init /var/jenkins_home/workspace/CI+Sonar+Sh # timeout=10
Fetching upstream changes from https://github.com/zbbkeepgoing/springboot-demo.git
......
 > git checkout -f 76c01188ae3f7497796e2238bd91e28b7629cd12
Commit message: "Rename mian.yml to main.yml"
First time build. Skipping changelog.
......
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/CI+Sonar+Sh
[Pipeline] {
[Pipeline] dir
Running in /var/jenkins_home/workspace/CI+Sonar+Sh
[Pipeline] {
[Pipeline] sh
[CI+Sonar+Sh] Running shell script
+ mvn clean install
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
......
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ sample ---
[INFO] Surefire report directory: /var/jenkins_home/workspace/CI+Sonar+Sh/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
......
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 21.324 sec - in com.dxc.ddccloud.demo.DemoControllerTests
2018-10-10 23:48:48.874  INFO 1104 --- [       Thread-2] o.s.w.c.s.GenericWebApplicationContext   : Closing org.s[email protected]68e965f5: startup date [Wed Oct 10 23:48:32 UTC 2018]; root of context hierarchy

Results :

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ sample ---
[INFO] Building jar: /var/jenkins_home/workspace/CI+Sonar+Sh/target/sample-0.0.1-SNAPSHOT.jar
......
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:03 min
[INFO] Finished at: 2018-10-10T23:48:54+00:00
[INFO] Final Memory: 29M/70M
[INFO] ------------------------------------------------------------------------
[Pipeline] junit
Recording test results
[Pipeline] sh
[CI+Sonar+Sh] Running shell script
+ mv target/sample-0.0.1-SNAPSHOT.jar target/sample.jar
......
[Pipeline] stage
[Pipeline] { (Sonarqube)
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/CI+Sonar+Sh
[Pipeline] {
[Pipeline] dir
Running in /var/jenkins_home/workspace/CI+Sonar+Sh
[Pipeline] {
[Pipeline] sh
[CI+Sonar+Sh] Running shell script
+ mvn sonar:sonar -Dsonar.host.url=http://192.168.88.130:9000 -Dsonar.login=2382ac098363521b98731e286e52e1ad22adef2b
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- sonar-maven-plugin:3.5.0.1254:sonar (default-cli) @ sample ---
......
[INFO] Analysis report generated in 752ms, dir size=51 KB
[INFO] Analysis reports compressed in 60ms, zip size=16 KB
[INFO] Analysis report uploaded in 259ms
[INFO] ANALYSIS SUCCESSFUL, you can browse http://192.168.88.130:9000/dashboard/index/com.dxc.ddccloud:sample
[INFO] Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
[INFO] More about the report processing at http://192.168.88.130:9000/api/ce/task?id=AWZgYsHZG8W2SE-PR30C
[INFO] Task total time: 24.968 s
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 36.735 s
[INFO] Finished at: 2018-10-10T23:49:37+00:00
[INFO] Final Memory: 32M/153M
[INFO] ------------------------------------------------------------------------
......
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/CI+Sonar+Sh
[Pipeline] {
[Pipeline] dir
Running in /var/jenkins_home/workspace/CI+Sonar+Sh
[Pipeline] {
[Pipeline] sh
[CI+Sonar+Sh] Running shell script
+ sshpass -p [email protected] scp -o StrictHostKeychecking=no target/sample.jar [email protected]:/opt/ansible
[Pipeline] sh
[CI+Sonar+Sh] Running shell script
+ sshpass -p [email protected] scp -o StrictHostKeychecking=no deploy.sh [email protected]:/opt/ansible
[Pipeline] sh
[CI+Sonar+Sh] Running shell script
+ sshpass -p [email protected] ssh -o StrictHostKeychecking=no [email protected] bash /opt/ansible/deploy.sh
process not started
[

Pipeline] sh
[CI+Sonar+Sh] Running shell script
+ sleep 8s
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
Finished: SUCCESS

部署結果

訪問部署的節點的8080埠。我們即可以看到Springboot的demo工程。

在這裡插入圖片描述