Jenkinsfile 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. pipeline {
  2. agent {
  3. node {
  4. label '112.74.105.17'
  5. }
  6. }
  7. // tools { node '' }
  8. // parameters {}
  9. environment {
  10. _productFileName = buildProductFileName() // 产物文件名
  11. _remote = "root@47.97.230.53"
  12. _productBackupPath = '/app/csair/backup' // 产物备份目录
  13. }
  14. triggers {
  15. // cron('0 0 * * *') // 周期任务
  16. // pollSCM('H/1 * * * *') // 轮询代码仓库(每分钟判断一次代码是否有变化)
  17. gitlab(
  18. triggerOnPush: true,
  19. triggerOnMergeRequest: true,
  20. triggerOnNoteRequest: true,
  21. branchFilterType: 'All',
  22. secretToken: 'asdfghjkl'
  23. )
  24. }
  25. options {
  26. buildDiscarder(logRotator(numToKeepStr: '10')) // 保存最近历史构建记录的数量
  27. disableConcurrentBuilds() // 同一个pipeline,Jenkins默认是可以同时执行多次的,此选项为了禁止pipeline同时执行
  28. // checkoutToSubdirectory('sub') // Jenkins默认拉取源码至工作空间的根目录中,此选项可以指定检出到工作空间的子目录中
  29. retry(1) // 当发生失败时进行重试(包括第1次失败)
  30. timestamps() // 添加日志打印时间
  31. timeout(time: 15, unit: 'MINUTES') // 如果pipeline执行时间过长,超出了设置的timeout时间,Jenkins将中止pipeline(SECONDS、MINUTES、HOURS)
  32. gitLabConnection('gitlab') // 连接gitlab服务(需要在Jenkins中设置Jenkins -> Configure System)
  33. }
  34. post {
  35. always { // 不论当前完成状态是什么,都执行
  36. cleanWs() // 清理工作空间插件[Workspace Cleanup Plugin](https://plugins.jenkins.io/ws-cleanup)
  37. }
  38. failure {
  39. updateGitlabCommitStatus name: 'build', state: 'failed'
  40. }
  41. success {
  42. updateGitlabCommitStatus name: 'build', state: 'success'
  43. }
  44. }
  45. stages {
  46. stage('Env & Param') {
  47. parallel {
  48. stage('Env') {
  49. steps {
  50. sh 'printenv'
  51. echo "系统当前用户 [${env.USER}]"
  52. echo "WORKSPACE [${env.WORKSPACE}]"
  53. echo "JENKINS_URL [${env.JENKINS_URL}]"
  54. echo "${_productFileName}"
  55. }
  56. }
  57. stage('Job') {
  58. steps {
  59. echo "Running [${env.BUILD_NUMBER}] on [${env.BUILD_URL}]"
  60. echo "BRANCH_NAME [${env.BRANCH_NAME}] GIT_BRANCH [${env.GIT_BRANCH}]"
  61. }
  62. }
  63. }
  64. }
  65. stage('NPM Install') {
  66. steps {
  67. sh 'npm install'
  68. }
  69. }
  70. stage('Static check') {
  71. parallel {
  72. stage('eslint') {
  73. steps {
  74. echo 'eslint'
  75. }
  76. }
  77. }
  78. }
  79. stage('NPM build') {
  80. steps {
  81. sh 'npm run build'
  82. }
  83. }
  84. stage('Product') {
  85. steps {
  86. dir("${env.WORKSPACE}/dist") {
  87. sh "tar -zcvf ${env.WORKSPACE}/${_productFileName} ./"
  88. }
  89. archiveArtifacts(artifacts: '*.tar.gz', caseSensitive: true, fingerprint: true)
  90. }
  91. }
  92. stage('Release') {
  93. parallel {
  94. stage('Master') {
  95. when {
  96. branch 'master'
  97. }
  98. steps {
  99. script {
  100. sh "scp -r ${WORKSPACE}/${_productFileName} ${_remote}:${_productBackupPath}"
  101. sh """
  102. ssh ${_remote} "
  103. source /etc/profile
  104. rm -rf /app/csair/admin/*
  105. tar -zxvf ${_productBackupPath}/${_productFileName} -C /app/csair/admin
  106. "
  107. """
  108. }
  109. }
  110. }
  111. }
  112. }
  113. }
  114. }
  115. // 生成产物的文件名
  116. def buildProductFileName() {
  117. String projectName = "${env.JOB_NAME}".tokenize('//')[0]
  118. String branchName = "${env.JOB_NAME}".tokenize('//')[1]
  119. String date = new Date().format('yyyyMMddHHmmss')
  120. return "${projectName}-${branchName}-${date}.tar.gz"
  121. }