pipeline配置前端项目

2021-06-04 18:05:46 浏览数 (1)

vue

  1. pipeline {
  2. agent { label 'master'}
  3. options {
  4. timestamps()
  5. disableConcurrentBuilds()
  6. buildDiscarder(
  7. logRotator(
  8. numToKeepStr: '20',
  9. daysToKeepStr: '30',
  10. )
  11. )
  12. }
  13. parameters {
  14. choice(
  15. name: "DEPLOY_FLAG",
  16. choices: ['deploy', 'rollback'],
  17. description: "发布/回滚"
  18. )
  19. }
  20. /*=======================================常修改变量-start=======================================*/
  21. environment {
  22. gitUrl = "git地址"
  23. branchName = "选择分支"
  24. gitlabCredentialsId = "git凭证"
  25. projectBuildDir = "build"
  26. projectBuildPath = "${env.WORKSPACE}/${projectBuildDir}/"
  27. nginxIp = "发布ip"
  28. nginxHtmlRoot = "/tmp/${env.JOB_NAME}"
  29. owner = "font"
  30. group = "font"
  31. backupRootDir = "/opt/backup"
  32. backupJob = "${backupRootDir}/${env.JOB_NAME}"
  33. backupDir = "${backupJob}/${env.BUILD_NUMBER}"
  34. rollbackVersion = ""
  35. }
  36. /*=======================================常修改变量-end=======================================*/
  37. stages {
  38. stage('Deploy') {
  39. when {
  40. expression { return params.DEPLOY_FLAG == 'deploy' }
  41. }
  42. stages {
  43. stage('Pre Env') {
  44. steps {
  45. echo "======================================项目名称 = ${env.JOB_NAME}"
  46. echo "======================================项目 URL = ${gitUrl}"
  47. echo "======================================项目分支 = ${branchName}"
  48. echo "======================================当前编译版本号 = ${env.BUILD_NUMBER}"
  49. echo "======================================项目 Build 文件夹路径 = ${projectBuildPath}"
  50. echo "======================================项目 Nginx 的 ROOT 路径 = ${nginxHtmlRoot}"
  51. }
  52. }
  53. stage('Git Clone') {
  54. steps {
  55. git branch: "${branchName}",
  56. credentialsId: "${gitlabCredentialsId}",
  57. url: "${gitUrl}"
  58. }
  59. }
  60. stage('NPM Install') {
  61. steps {
  62. nodejs('nodejs') {
  63. sh "npm install"
  64. }
  65. }
  66. }
  67. stage('NPM Build') {
  68. steps {
  69. nodejs('nodejs') {
  70. sh "npm run build"
  71. }
  72. }
  73. }
  74. stage('Backup') {
  75. agent { label 'ansible'}
  76. steps {
  77. script {
  78. try {
  79. isItBackupToday = sh (returnStatus: true, script:'ansible ${nginxIp} -m shell -a "ls -l --time-style= %D ${backupJob} | grep $(date %D)"')
  80. if (isItBackupToday !=0){
  81. try {
  82. sh 'ansible ${nginxIp} -m file -a "path=${backupDir} state=directory owner=${owner} group=${group}"'
  83. sh 'ansible ${nginxIp} -m shell -a "cp -a ${nginxHtmlRoot}/* ${backupDir}"'
  84. }
  85. catch (exc) {
  86. echo 'Something failed!'
  87. }
  88. }
  89. }
  90. catch (exc) {
  91. echo 'Something failed!'
  92. }
  93. }
  94. }
  95. }
  96. stage('Nginx Deploy') {
  97. agent { label 'ansible'}
  98. steps {
  99. sh 'ansible ${nginxIp} -m synchronize -a "src=${projectBuildPath} dest=${nginxHtmlRoot} delete=yes"'
  100. sh 'ansible ${nginxIp} -m file -a "path=${nginxHtmlRoot} owner=${owner} group=${group} recurse=yes"'
  101. }
  102. }
  103. stage('Tar Build') {
  104. steps {
  105. sh "tar -zcf ${env.JOB_NAME}.tar.gz ${projectBuildDir}"
  106. }
  107. }
  108. stage('Archive Artifacts') {
  109. steps {
  110. archiveArtifacts "${env.JOB_NAME}.tar.gz"
  111. }
  112. }
  113. }
  114. }
  115. stage('Rollback') {
  116. when {
  117. expression { return params.DEPLOY_FLAG == 'rollback' }
  118. }
  119. agent { label 'ansible'}
  120. steps{
  121. script {
  122. sh 'ansible ${nginxIp} -m shell -a "ls -l ${backupJob}" | grep -v "CHANGED"'
  123. rollbackVersion = input(
  124. message: "请填写要回滚的版本",
  125. parameters: [
  126. string(name:'BUILD_NUMBER')
  127. ]
  128. )
  129. sh 'ansible ${nginxIp} -m file -a "path=${nginxHtmlRoot} state=absent"'
  130. sh 'ansible ${nginxIp} -m file -a "path=${nginxHtmlRoot} state=directory owner=${owner} group=${group}"'
  131. withEnv(["rollbackVersion=${rollbackVersion}"]){
  132. sh 'ansible ${nginxIp} -m shell -a "cp -a ${backupJob}/${rollbackVersion}/* ${nginxHtmlRoot}"'
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }

这个jenkinsfile中有保存 制品,

需要注意的是:制品的保存时间,和jenkins丢弃旧的构建参数,相同

0 人点赞