• 持续集成方案
  • 操作步骤

    持续集成方案

    由于Tars的web管理界面暂时未支持集成git/svn,每次发布需要上传zip包极为不方便,这里给出一个基于jenkins的持续集成方案方便大家参考。具体业务中可能需要根据实际情况进行调整。

    操作步骤

    这里使用 jenkins 的pipeline 构建持续集成环境,通过调用tars web管理界面的http接口实现持续集成。下面将以 github examples中的 QD.ActHttpServer为例。

    1. 新建一个构建节点如phpenv,节点可以使docker,包含:
      • php 可能会需要多php版本,看具体业务
      • composer
      • jq Linux下json的命令行工具,方便解析http接口返回的json
      • phpunit 非必须
      • valgrind 非必须
    2. 安装以下plugin
      • Valgrind Plug-in
      • Pipeline
      • Workspace Cleanup Plugin
    3. 新建一个pipeline job: QD.ActHttpServer
    4. Pipeline 中定义如下 script:
      1. pipeline {
      2. agent {
      3. node {
      4. label 'phpenv'
      5. }
      6. }
      7. parameters {
      8. string(defaultValue: 'upload_from_jenkins', name: 'TAG_DESC', description: '发布版本描述' )
      9. string(defaultValue: 'master', name: 'BRANCH_NAME', description: 'git分支,如:develop,master 默认: master')
      10. }
      11. environment {
      12. def JENKINS_HOME = "/home/jenkins"
      13. def PROJECT_ROOT = "$JENKINS_HOME/workspace/QD.ActHttpServer"
      14. def APP_NAME = "QD"
      15. def SERVER_NAME = "ActHttpServer"
      16. }
      17. stages {
      18. stage('代码拉取'){
      19. steps {
      20. echo "checkout from git"
      21. git credentialsId:'2', url: 'https://github.com/TarsPHP/TarsPHP', branch: "${env.BRANCH_NAME}"
      22. }
      23. }
      24. stage('单元测试') {
      25. steps {
      26. echo "phpunit 测试"
      27. echo "valgrind 测试"
      28. }
      29. }
      30. stage('覆盖率测试') {
      31. steps {
      32. echo "LCOV 覆盖率测试"
      33. }
      34. }
      35. stage('编译与发布') {
      36. steps {
      37. script {
      38. dir("$PROJECT_ROOT/examples/TarsActDemo/QD.ActHttpServer/src") {
      39. echo "QD.ActHttpServer 编译与发布"
      40. sh "composer install -vvv"
      41. sh "composer run-script deploy"
      42. sh "ls *.tar.gz > tmp.log"
      43. def packageDeploy = sh(script: "head -n 1 tmp.log", returnStdout: true).trim()
      44. sh "curl -H 'Host:tars.qidian.local' -F 'suse=@./${packageDeploy}' -F 'application=${APP_NAME}' -F 'module_name=${SERVER_NAME}' -F 'comment=${env.TAG_DESC}' http://tars.qidian.local/pages/server/api/upload_patch_package > curl.log"
      45. def packageVer = sh(script: "jq '.data.id' curl.log", returnStdout: true).trim()
      46. def postJson = '{"serial":true,"items":[{"server_id":28,"command":"patch_tars","parameters":{"patch_id":' + packageVer + ',"bak_flag":false,"update_text":"${env.TAG_DESC}"}}]}'
      47. echo postJson
      48. sh "curl -H 'Host:tars.qidian.local' -H 'Content-Type:application/json' -X POST --data '${postJson}' http://tars.qidian.local/pages/server/api/add_task"
      49. }
      50. }
      51. }
      52. }
      53. }
      54. post {
      55. success {
      56. emailext (
      57. subject: "[jenkins]构建通知:${env.JOB_NAME} 分支: ${env.BRANCH_NAME} - Build# ${env.BUILD_NUMBER} 成功 !",
      58. body: '${SCRIPT, template="groovy-html.template"}',
      59. mimeType: 'text/html',
      60. to: "liujingfeng.a@yuewen.com",
      61. )
      62. cleanWs()
      63. }
      64. failure {
      65. emailext (
      66. subject: "[jenkins]构建通知:${env.JOB_NAME} 分支: ${env.BRANCH_NAME} - Build# ${env.BUILD_NUMBER} 失败 !",
      67. body: '${SCRIPT, template="groovy-html.template"}',
      68. mimeType: 'text/html',
      69. to: "liujingfeng.a@yuewen.com",
      70. )
      71. cleanWs()
      72. }
      73. }
      74. }
    5. 执行构建 PNG
    6. 查看构建结果(邮件通知) PNG
    7. 查看tars web管理界面 PNG