可持续化集成和可持续化部署笔记

原文链接:https://www.jianshu.com/p/d4bd2e34181d

注:以下只是参加TW顾问关于CI/CD演示的时候做的一些笔记

  • 相关资料:https://trello.com/b/PFrWELDr/ci-cd-workshop
  • 需要工具: Git, Maven, JDK

1、克隆项目

$ git clone https://github.com/franktang/make-it-cry.git

2、Build项目(本地)

首先我们先执行maven 测试(mvn clean test),成功以后会得到以下output

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.983 s
[INFO] Finished at: 2018-03-02T15:28:20+08:00
[INFO] Final Memory: 27M/218M
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0

然后我们还是用maven来给项目打包(‘mvn package’), 成功以后会得到以下Output

[INFO] 
[INFO] --- maven-war-plugin:2.6:war (default-war) @ make-it-cry ---
[INFO] Packaging webapp
[INFO] Assembling webapp [make-it-cry] in [C:\Users\hztxw\Documents\GitHub\make-it-cry\target\make-it-cry-1.0]
[INFO] Processing war project
[INFO] Copying webapp resources [C:\Users\hztxw\Documents\GitHub\make-it-cry\src\main\webapp]
[INFO] Webapp assembled in [527 msecs]
[INFO] Building war: C:\Users\hztxw\Documents\GitHub\make-it-cry\target\make-it-cry-1.0.war
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.5.9.RELEASE:repackage (default) @ make-it-cry ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.330 s
[INFO] Finished at: 2018-03-02T15:35:59+08:00
[INFO] Final Memory: 25M/276M
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0

/make-it-cry/target 目录下也能找到build好的war包。

image.png

3、本地部署
执行以下cmd在本地部署war包

cd target
java -jar make-it-cry-1.0.war

成功会出现以下output

2018-03-02 15:43:30.193  INFO 4280 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-03-02 15:43:30.258  INFO 4280 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-03-02 15:43:30.487  INFO 4280 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-03-02 15:43:30.700  INFO 4280 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-03-02 15:43:30.715  INFO 4280 --- [           main] com.cry.it.make.Application              : Started Application in 9.036 seconds (JVM running for 10.032)

并且相应的端口号为8080。 在浏览器输入http://localhost:8080/face 测试一下。

成功!!!


image.png

4、配置Jenkins

接下来就进入了CI&CD的重点了!首先我们要配置Jenkins
(Ref website:https://jenkins.io/).
这里我们通过写Pipeline来实现CI&CD。

4.1 新建Pipeline

输入名字,选中“Pipeline”


image.png
4.2 配置Pipeline

Build Trigger 里面选中 "Poll SCM" 并在 "Schedule" 一栏输入 “* * * * *”, 这样jenkins 每隔1分钟就会去检查源码是否有改动,并会自动trigger build.


image.png

接下来写Pipeline 脚本,

//从git代码仓库拉取代码
stage('pull code'){
  node {
    git url:'https://github.com/franktang/make-it-cry.git'
  }
}

//编译 + 执行测试
stage('compile & test') {
  node{
    withMaven(maven: 'mvn', mavenLocalRepo: '.repository') {
      sh 'mvn clean compile test'
    }
  }
}

//发布到artifactory
stage('publish'){
  node{
    withMaven(maven: 'mvn', mavenLocalRepo: '.repository') { 
       sh "mvn deploy" 
    }
  }
}

//部署到服务器上
stage('deployment'){
  value = input message: 'please provide coachId and versionId', ok: 'deploy', parameters: [string(defaultValue: '', description: '', name: 'coachId'), string(defaultValue: '', description: '', name: 'versionId')]
  node {
    sshagent(['coachx']) {
      app = "great-company/${value['coachId']}/make-it-cry/${value['versionId']}/make-it-cry-${value['versionId']}.war"
      sh "ssh -o StrictHostKeyChecking=no ${value['coachId']}@internal-coach-web.eastasia.cloudapp.azure.com "+
      '"source /cd/install-java.sh http://internal-coach-devops.eastasia.cloudapp.azure.com:8081/server-jre-8u161-linux-x64.tar.gz ; ' + 
      "/cd/run-app.sh ${app}\""
    }
  }
}

最后保存配置,选择“Build Now” Jenkins就会帮你做CI和CD啦!!!

image.png

成功!!!

打开JFrog Artifactory, 可以看到我们的war包和pom.xml已经发布上去了!

image.png

我们再打开服务器地址去验证一下,耶!!!


image.png

你可能感兴趣的:(可持续化集成和可持续化部署笔记)