Jenkinsfile
    版本1

    1. def POD_LABEL = "vue-pod-${UUID.randomUUID().toString()}"
    2. podTemplate (
    3. label: POD_LABEL, cloud: 'kubernetes',
    4. containers: [
    5. containerTemplate(name: 'jnlp', image: 'jenkins/jnlp-agent-docker', args: '${computer.jnlpmac} ${computer.name}'),
    6. containerTemplate(name: 'nodejs', image: "node:14-alpine", ttyEnabled: true, command: 'cat')
    7. ],
    8. volumes: [
    9. hostPathVolume(hostPath: '/var/run/docker.sock', mountPath:'/var/run/docker.sock')
    10. ]
    11. ) {
    12. node(POD_LABEL) {
    13. stage('code-checkout') {
    14. container('nodejs') {
    15. checkout scm
    16. }
    17. // script {
    18. // build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
    19. // }
    20. def packageJSON = readJSON file: 'package.json'
    21. build_tag = packageJSON.version
    22. }
    23. stage('code-build') {
    24. container('nodejs') {
    25. // sh 'npm config set registry https://registry.npm.taobao.org'
    26. sh "npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/"
    27. sh 'npm install --unsafe-perm --registry=https://registry.npm.taobao.org'
    28. sh 'npm run build'
    29. }
    30. }
    31. stage('docker-build') {
    32. sh "docker build -t 192.168.1.207:6543/com.newpwr/genoany-web:${build_tag} ."
    33. }
    34. stage('docker-push') {
    35. withCredentials([usernamePassword(credentialsId: 'localDocker', passwordVariable: 'localDockerPassword', usernameVariable: 'localDockerUser')]) {
    36. sh "docker login 192.168.1.207:6543 -u ${localDockerUser} -p ${localDockerPassword}"
    37. sh "docker push 192.168.1.207:6543/test/test-web:${build_tag}"
    38. }
    39. }
    40. stage('deploy') {
    41. // if (env.BRANCH_NAME == 'master') {
    42. // input "是否部署到线上环境?"
    43. // }
    44. input "是否部署到线上环境?"
    45. }
    46. }
    47. }

    版本2

    1. def POD_LABEL = "jnlp-agent-genoany-web-${UUID.randomUUID().toString()}"
    2. def BUILD_VERSION = ''
    3. podTemplate (
    4. label: POD_LABEL, cloud: 'kubernetes',
    5. containers: [
    6. containerTemplate(name: 'jnlp', image: 'jenkins/jnlp-agent-docker', args: '${computer.jnlpmac} ${computer.name}'),
    7. containerTemplate(name: 'nodejs', image: "node:14-alpine", ttyEnabled: true, command: 'cat')
    8. ],
    9. volumes: [
    10. hostPathVolume(hostPath: '/var/run/docker.sock', mountPath:'/var/run/docker.sock')
    11. ]
    12. ) {
    13. node(POD_LABEL) {
    14. stage('code-checkout') {
    15. container('nodejs') {
    16. checkout scm
    17. }
    18. }
    19. stage('build-setting') {
    20. def packageJSON = readJSON file: 'package.json'
    21. userInput = input(
    22. id: 'userInput', message: '构建设置', parameters: [
    23. [$class:'TextParameterDefinition', defaultValue: "${packageJSON.version}", description: '版本号', name: 'version'],
    24. [$class:'ChoiceParameterDefinition',defaultValue:"NO", choices: "NO\nYES", description:"立即部署",name: 'deploy']
    25. ])
    26. BUILD_VERSION = userInput['version']
    27. CAN_DEPLOY = userInput['deploy']
    28. echo "填入的版本号为:${BUILD_VERSION}"
    29. echo "立即部署:${CAN_DEPLOY}"
    30. }
    31. stage('code-build') {
    32. container('nodejs') {
    33. sh "npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/"
    34. sh 'npm install --unsafe-perm --registry=https://registry.npm.taobao.org'
    35. sh 'npm run build'
    36. }
    37. }
    38. stage('docker-build') {
    39. sh "docker build -t 192.168.1.207:6543/com.newpwr/genoany-web:${BUILD_VERSION} ."
    40. }
    41. stage('docker-push') {
    42. withCredentials([usernamePassword(credentialsId: 'localDocker', passwordVariable: 'localDockerPassword', usernameVariable: 'localDockerUser')]) {
    43. sh "docker login 192.168.1.207:6543 -u ${localDockerUser} -p ${localDockerPassword}"
    44. sh "docker push 192.168.1.207:6543/com.newpwr/genoany-web:${BUILD_VERSION}"
    45. }
    46. }
    47. stage('deploy') {
    48. if(CAN_DEPLOY == "YES"){
    49. }
    50. }
    51. }
    52. }

    版本3

    1. def POD_LABEL = "jnlp-agent-genoany-web-${UUID.randomUUID().toString()}"
    2. def BUILD_VERSION = ''
    3. def IS_SUCCESS = true
    4. properties(
    5. [
    6. parameters(
    7. [
    8. booleanParam(defaultValue: true, description: '是否部署到研发环境', name: 'DEPLOY_DEV'),
    9. booleanParam(defaultValue: false, description: '是否部署到产品开发环境', name: 'DEPLOY_PRO_DEV')
    10. ]
    11. )
    12. ]
    13. )
    14. podTemplate (
    15. label: POD_LABEL, cloud: 'kubernetes',
    16. containers: [
    17. containerTemplate(name: 'jnlp', image: 'jenkins/jnlp-agent-docker', args: '${computer.jnlpmac} ${computer.name}'),
    18. // containerTemplate(name: 'sonarscanner', image: 'sonarsource/sonar-scanner-cli:latest', command: 'cat', ttyEnabled: true),
    19. containerTemplate(name: 'nodejs', image: "node:14-alpine", ttyEnabled: true, command: 'cat')
    20. ],
    21. volumes: [
    22. hostPathVolume(hostPath: '/var/run/docker.sock', mountPath:'/var/run/docker.sock')
    23. ]
    24. ) {
    25. node(POD_LABEL) {
    26. try{
    27. stage('code-checkout') {
    28. container('nodejs') {
    29. checkout scm
    30. }
    31. build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
    32. }
    33. // stage('code-sonar') {
    34. // container('sonarscanner') {
    35. // echo "代码质量分析"
    36. // sh """
    37. // sonar-scanner \
    38. // -Dsonar.projectKey=newpwrplatform.cloud \
    39. // -Dsonar.sources=src \
    40. // -Dsonar.language=js \
    41. // -Dsonar.sourceEncoding=UTF-8 \
    42. // -Dsonar.host.url=http://sonarqube:9000 \
    43. // -Dsonar.login=5b09781993c58c204a3c4f26eaf3495ea4bf40e0
    44. // """
    45. // }
    46. // dingtalk (
    47. // robot: '9dd3f2d9-aae0-4145-bd64-fc1701d4a4a8',
    48. // type: 'MARKDOWN',
    49. // title: '你有新的消息,请注意查收',
    50. // text: [
    51. // "# [${env.JOB_NAME}](${env.JOB_URL})",
    52. // '',
    53. // "- 任务: [#${env.BUILD_NUMBER}](${env.BUILD_URL})",
    54. // "- 分支: ${build_tag}",
    55. // "- 状态: 代码质量分析完毕",
    56. // "- 结果: [http://192.168.1.36:30910/dashboard?id=newpwrplatform.cloud](http://192.168.1.36:30910/dashboard?id=newpwrplatform.cloud)"
    57. // ],
    58. // at: []
    59. // )
    60. // }
    61. stage('build-setting') {
    62. // def packageJSON = readJSON file: 'package.json'
    63. BUILD_VERSION="1.5.1."+"${env.BUILD_NUMBER}"
    64. echo "版本号为:${BUILD_VERSION}"
    65. def timeStamp = Calendar.getInstance().getTime().format('yyyy-MM-dd HH:mm:ss',TimeZone.getTimeZone('Asia/Shanghai'))
    66. // def time=new Date().format('yyyy-MM-dd HH:mm:ss')
    67. def versionJSON = readJSON file: 'public/version.json'
    68. versionJSON.version = BUILD_VERSION
    69. versionJSON.publishTime = timeStamp
    70. writeJSON file: 'public/version.json', json: versionJSON
    71. currentBuild.displayName = "v${BUILD_VERSION}"
    72. // userInput = input(
    73. // id: 'userInput', message: '构建设置', parameters: [
    74. // [$class:'ChoiceParameterDefinition', choices: "NO\nYES", description:"立即部署",name: 'deploy']
    75. // ])
    76. CAN_DEPLOY = "YES"
    77. echo "立即部署:${CAN_DEPLOY}"
    78. dingtalk (
    79. robot: '9dd3f2d9-aae0-4145-bd64-fc1701d4a4a8',
    80. type: 'MARKDOWN',
    81. title: '你有新的消息,请注意查收',
    82. text: [
    83. "# [${env.JOB_NAME}](${env.JOB_URL})",
    84. '',
    85. "- 任务: [#${env.BUILD_NUMBER}](${env.BUILD_URL})",
    86. "- 分支: ${build_tag}",
    87. "- 状态: 构建中",
    88. "- 版本: ${BUILD_VERSION}",
    89. "- 是否立即发布: ${CAN_DEPLOY}",
    90. ],
    91. at: []
    92. )
    93. }
    94. stage('code-build') {
    95. container('nodejs') {
    96. sh "npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/"
    97. sh 'npm install genoany-g6-v2'
    98. sh 'npm install --unsafe-perm --registry=https://registry.npm.taobao.org'
    99. sh 'npm run build'
    100. }
    101. }
    102. stage('docker-build') {
    103. sh "docker build -t 192.168.1.207:6543/com.newpwr/genoany-web:${BUILD_VERSION} ."
    104. }
    105. stage('docker-push') {
    106. withCredentials([usernamePassword(credentialsId: 'localDocker', passwordVariable: 'localDockerPassword', usernameVariable: 'localDockerUser')]) {
    107. sh "docker login 192.168.1.207:6543 -u ${localDockerUser} -p ${localDockerPassword}"
    108. sh "docker push 192.168.1.207:6543/com.newpwr/genoany-web:${BUILD_VERSION}"
    109. }
    110. }
    111. stage('deploy') {
    112. if(Boolean.valueOf(DEPLOY_DEV)){
    113. }
    114. if(Boolean.valueOf(DEPLOY_PRO_DEV)){
    115. }
    116. }
    117. }catch(Exception ex){
    118. IS_SUCCESS= false
    119. throw ex
    120. } finally {
    121. if(IS_SUCCESS){
    122. dingtalk (
    123. robot: '9dd3f2d9-aae0-4145-bd64-fc1701d4a4a8',
    124. type: 'MARKDOWN',
    125. title: '你有新的消息,请注意查收',
    126. text: [
    127. "# [${env.JOB_NAME}](${env.JOB_URL})",
    128. '',
    129. "- 任务: [#${env.BUILD_NUMBER}](${env.BUILD_URL})",
    130. "- 分支: ${build_tag}",
    131. "- 状态: <font color=green>成功</font>",
    132. "- 版本: ${BUILD_VERSION}",
    133. "- 是否立即发布: ${CAN_DEPLOY}",
    134. ],
    135. at: []
    136. )
    137. } else {
    138. dingtalk (
    139. robot: '9dd3f2d9-aae0-4145-bd64-fc1701d4a4a8',
    140. type: 'MARKDOWN',
    141. title: '你有新的消息,请注意查收',
    142. text: [
    143. "# [${env.JOB_NAME}](${env.JOB_URL})",
    144. '',
    145. "- 任务: [#${env.BUILD_NUMBER}](${env.BUILD_URL})",
    146. "- 分支: ${build_tag}",
    147. "- 状态: <font color=red>失败</font>",
    148. "- 版本: ${BUILD_VERSION}",
    149. "- 是否立即发布: ${CAN_DEPLOY}",
    150. ],
    151. at: []
    152. )
    153. }
    154. }
    155. }
    156. }

    读取package.json文件里的版本信息

    readJSON需要jenkins插件 Pipeline Utility Steps

    1. def packageJSON = readJSON file: 'package.json'
    2. def packageJSONVersion = packageJSON.version
    3. echo packageJSONVersion
    4. sh 'VERSION=${packageJSONVersion}_${BUILD_NUMBER}_${BRANCH_NAME} npm run build'

    npm-pv-pvc.yaml

    1. apiVersion: v1
    2. kind: PersistentVolume
    3. metadata:
    4. name: npm-pv
    5. spec:
    6. capacity:
    7. storage: 20Gi
    8. accessModes:
    9. - ReadWriteMany
    10. nfs:
    11. server: 192.168.1.36
    12. path: "/nfs/npm"
    13. ---
    14. apiVersion: v1
    15. kind: PersistentVolumeClaim
    16. metadata:
    17. name: npm-pvc
    18. namespace: devops
    19. spec:
    20. accessModes: ["ReadWriteMany"]
    21. resources:
    22. requests:
    23. storage: 20Gi