10

Jenkins-pipeline学习笔记–使用Jenkinsfile进行PHP,前端等类似项目的部署与回滚(五)...

 3 years ago
source link: http://www.eryajf.net/3352.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Jenkins-pipeline学习笔记–使用Jenkinsfile进行PHP,前端等类似项目的部署与回滚(五) |坐而言不如起而行! 二丫讲梵
> 术业专攻 > 自动化运维 > Jenkins > <三十五>Jenkins-pipeline学习笔记–使用Jenkinsfile进行PHP,前端等类似项目的部署与回滚(五)
本文预计阅读时间 9 分钟

PHP项目的发布实际上是超级简单的,无非就是将项目代码同步到远程主机的应用目录即可,实际生产中,有不少与此类似的同类项目,也都可以借鉴如下的思路。

因为配置比较简单了,这里就不再赘述,不清楚的同学可以参考这里:http://www.eryajf.net/3304.html

直奔主题,列出核心构建文件内容,前边文章看过,一些内容就比较熟悉了,这里就不重复介绍了,只说明一下值得注意的点。

  1. pipeline {
  2. agent any
  3. environment {
  4. // 定义项目名称方便全局引用
  5. project = "test-php"
  6. // 远程主机地址,这里只演示了一台,如果是多台,可以空格继续写,下边的部署嵌套个for循环即可
  7. remote_ip = "192.168.0.1"
  8. remote_dir = "/data/www/test-php"
  9. }
  10. options {
  11. timestamps()
  12. disableConcurrentBuilds()
  13. timeout(time: 10, unit: 'MINUTES')
  14. buildDiscarder(logRotator(numToKeepStr: '10'))
  15. }
  16. triggers{
  17. gitlab( triggerOnPush: true,
  18. triggerOnMergeRequest: true,
  19. branchFilterType: 'All',
  20. secretToken: "${env.git_token}")
  21. }
  22. // 这里使用参数化构建的方式,而没有使用input参数,下边会说明一下原因。
  23. parameters {
  24. choice(name: 'MODE', choices: ['deploy','rollback'], description: '请选择发布或者回滚?')
  25. }
  26. stages {
  27. stage('部署') {
  28. when {
  29. environment name: 'MODE',value: 'deploy'
  30. }
  31. steps {
  32. script {
  33. try {
  34. sh '''
  35. ssh -p 10022 root@$remote_ip "mkdir -p /media/${project}"
  36. rsync -avz -e 'ssh -p 34222' --exclude='Jenkinsfile' --delete ${WORKSPACE}/ root@$remote_ip:/media/${project}/${BUILD_ID}
  37. ssh -p 10022 root@$remote_ip "rm -rf $remote_dir && ln -s /media/${project}/${BUILD_ID} $remote_dir"
  38. '''
  39. } catch(err) {
  40. echo "${err}"
  41. }
  42. }
  43. }
  44. }
  45. stage('回滚') {
  46. when {
  47. environment name: 'MODE',value: 'rollback'
  48. }
  49. steps {
  50. script {
  51. try {
  52. sh '''
  53. last_success=$(ssh -p 10022 root@$remote_ip "ls -lt /media/${project} | sed -n '3p'" | awk '{print \$9}')
  54. ssh -p 10022 root@$remote_ip "rm -rf $remote_dir && ln -s /media/${project}/${last_success} $remote_dir"
  55. '''
  56. } catch(err) {
  57. echo "${err}"
  58. }
  59. }
  60. }
  61. }
  62. stage('清理工作空间') {
  63. steps {
  64. echo '清理工作空间'
  65. cleanWs()
  66. }
  67. }
  68. }
  69. post {
  70. success {
  71. dingTalk accessToken:'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxx',
  72. imageUrl:'https://ae01.alicdn.com/kf/Hdfe28d2621434a1eb821ac6327b768e79.png',
  73. jenkinsUrl: "${env.JENKINS_URL}",
  74. message:'构建成功 ✅',
  75. notifyPeople:'李启龙'
  76. }
  77. failure {
  78. dingTalk accessToken:'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxx',
  79. imageUrl:'https://ae01.alicdn.com/kf/Hdfe28d2621434a1eb821ac6327b768e79.png',
  80. jenkinsUrl: "${env.JENKINS_URL}",
  81. message:'构建失败 ❌',
  82. notifyPeople:'李启龙'
  83. }
  84. }
  85. }

原本打算通过input来定义部署与回滚的参数,又想结合when这种判断来进行,结果发现目前when支持的参数项还非常有限,因此这条路恐怕是不太好走了,而我又不太想用if的那种判断,于是只得将input参数化构建改成了常规的参数化构建。

配置中添加了push代码自动构建,比较方便,但是慎重直接在线上环境中引用自动构建功能,因为经我测试,尽管开启了参数化构建,那么默认触发之后,是自动选择部署的。

部署与回滚的方案参考了瓦力的软链的思路,这样回滚起来,似乎也变得相对简单了。

最后的通知功能,当然是交给熟悉的钉钉啦!


weinxin


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK