定义

是一组过程、方法与系统统称,用于促进开发(应用程序/软件工程)、技术运营和质量保障(QA)部门之间的沟通写作与整合。

Dev:开发人员;
**Ops:运维人员;

持续集成:将应用程序部署之前需要进行开发、测试、构建的过程成为持续集成
持续部署:在敏捷开发中,短时间内将程序多次部署
**
1594352559500-18a1d8c3-c4f3-4c23-9219-2af0d65cb125.png

1594352559555-40b35a36-9cf8-4627-af42-be16f985d8a3.png

DevOps主要方式

一、 开源自建(基于jenkins)

  1. 开发提交代码 -> GitLab
  2. GitLab 通过 WebHook 触发Jenkins构建
  3. Jenkins 跑构建流程
  4. 更新 Harbor 的镜像
  5. 通知K8s触发更新服务

企业微信截图_16172449364821.png

二 云运营商(阿里云或腾讯云)

阿里云和腾讯云所提供的组件都是基于jenkins

目前使用

企业微信截图_16172452362532.png

  1. pipeline {
  2. agent any
  3. environment {
  4. VERSION = readMavenPom().getVersion()
  5. }
  6. stages {
  7. stage("检出") {
  8. steps {
  9. checkout([
  10. $class: 'GitSCM',
  11. branches: [[name: env.GIT_BUILD_REF]],
  12. userRemoteConfigs: [[
  13. url: env.GIT_REPO_URL,
  14. credentialsId: env.CREDENTIALS_ID
  15. ]]])
  16. }
  17. }
  18. stage("构建") {
  19. steps {
  20. echo "构建中..."
  21. sh 'mvn clean package -Dmaven.test.skip=true'
  22. sh 'ls target/'
  23. echo "构建完成."
  24. // 演示怎样产生构建物
  25. script {
  26. def exists = fileExists "README.md"
  27. if (!exists) {
  28. writeFile(file: "README.md", text: "Helloworld")
  29. }
  30. }
  31. archiveArtifacts artifacts: "README.md", fingerprint: true
  32. // archiveArtifacts artifacts: "**/target/*.jar", fingerprint: true // 收集构建产物
  33. }
  34. }
  35. stage("测试") {
  36. steps {
  37. echo "单元测试中..."
  38. // 请在这里放置您项目代码的单元测试调用过程,例如:
  39. // sh "mvn test" // mvn 示例
  40. // sh "make test" // make 示例
  41. echo "单元测试完成."
  42. // 演示怎么样生成测试报告
  43. writeFile(file: 'TEST-demo.junit4.AppTest.xml', text: '''
  44. <testsuite name="demo.junit4.AppTest" time="0.053" tests="3" errors="0" skipped="0" failures="0">
  45. <properties>
  46. </properties>
  47. <testcase name="testApp" classname="demo.junit4.AppTest" time="0.003"/>
  48. <testcase name="test1" classname="demo.junit4.AppTest" time="0"/>
  49. <testcase name="test2" classname="demo.junit4.AppTest" time="0"/>
  50. </testsuite>
  51. ''')
  52. junit '*.xml'
  53. // junit "target/surefire-reports/*.xml" // 收集单元测试报告的调用过程
  54. }
  55. }
  56. stage("部署") {
  57. steps {
  58. echo "部署中..."
  59. script {
  60. // 您可以在此执行任意的 groovy 脚本
  61. def remote = [:]
  62. remote.name = 'VM-16-8-centos'
  63. remote.allowAnyHosts = true
  64. remote.host = '106.55.34.215'
  65. remote.port = 22
  66. remote.user = 'root'
  67. withCredentials([sshUserPrivateKey(credentialsId: "66172def-3047-49e3-acbe-d14105375159",keyFileVariable: 'id_rsa')]) {
  68. remote.identityFile = "~/.ssh/id_rsa"
  69. // 读取的凭据将以环境的变量的形式获取 SSH_PRIVATE_KEY_PATH (SSH_PRIVATE_KEY_PATH 为私钥路径)
  70. //sshCommand remote: remote, sudo: true, command: "jps -ml | grep $DEPOT_NAME-*.jar"
  71. sshCommand remote: remote, sudo: true, command: "rm -rf /opt/$DEPOT_NAME-*.jar"
  72. sshPut remote: remote, from: "target/$DEPOT_NAME-${env.VERSION}.jar", into: "/opt/"
  73. sshCommand remote: remote, command: "nohup java -jar /opt/$DEPOT_NAME-${env.VERSION}.jar >/dev/null 2>&1 &"
  74. }
  75. }
  76. echo "部署完成"
  77. }
  78. }
  79. }
  80. }