Devops关键工具及技术(五)—基于Pipeline的Bash脚本部署

在上一篇文章Devops关键工具及技术(四)—基于Pipeline的SonarQube静态代码扫描中,我们在持续集成的基础上加上了静态代码扫描,并利用Pipeline对之前Spring-boot的工程进行了代码扫描。并且在SonarQube中看到了代码中各个指标的一些信息。代码工程Github地址为:https://github.com/zbbkeepgoing/springboot-demo 。

接下来我们将会为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

我们继续沿用上一篇文章Devops关键工具及技术(四)—基于Pipeline的SonarQube静态代码扫描中的Pipeline,并在其后面加上部署的Stage。

Pipeline内容

内容也可以在Github中找到
https://github.com/zbbkeepgoing/pipeline-sample

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 1qaz@WSX scp -o StrictHostKeychecking=no target/sample.jar [email protected]:/opt/ansible'   //把部署的jar传到目标机器上
                   sh 'sshpass -p 1qaz@WSX scp -o StrictHostKeychecking=no deploy.sh [email protected]:/opt/ansible'   //把脚本传到目标机器上
                   sh 'sshpass -p 1qaz@WSX ssh -o StrictHostKeychecking=no [email protected] "bash /opt/ansible/deploy.sh"'      //在目标机器上执行对应的脚本
                   sh 'sleep 8s'     //睡眠8s
                }
            }
        }
    }
}

新建Pipeline

Devops关键工具及技术(五)—基于Pipeline的Bash脚本部署_第1张图片

执行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.springframework.web.context.support.GenericWebApplicationContext@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 1qaz@WSX scp -o StrictHostKeychecking=no target/sample.jar [email protected]:/opt/ansible
[Pipeline] sh
[CI+Sonar+Sh] Running shell script
+ sshpass -p 1qaz@WSX scp -o StrictHostKeychecking=no deploy.sh [email protected]:/opt/ansible
[Pipeline] sh
[CI+Sonar+Sh] Running shell script
+ sshpass -p 1qaz@WSX 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工程。

Devops关键工具及技术(五)—基于Pipeline的Bash脚本部署_第2张图片

你可能感兴趣的:(Devops)