Jenkinsfile 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. pipeline {
  2. agent any
  3. // 环境变量
  4. environment {
  5. // docker私有仓库凭证
  6. dockerhub_credentials = credentials('ali-dockerhub')
  7. // 镜像版本
  8. image_tag = sh(returnStdout: true,script: 'echo `date +"%Y%m%d%H%M"_``git describe --tags --always`').trim()
  9. }
  10. // 入参定义
  11. parameters {
  12. string(name: 'project_name', defaultValue: 'mldong-admin', description: '项目名称')
  13. string(name: 'deploy_type', defaultValue: 'deploy', description: '发布类型')
  14. string(name: 'git_url', defaultValue: 'git@gitee.com:mldong/mldong.git', description: '仓库地址')
  15. string(name: 'branch_name', defaultValue: 'master', description: 'git分支')
  16. string(name: 'profiles', defaultValue: 'test', description: '环境')
  17. string(name: 'registry_url', defaultValue: 'registry-vpc.cn-zhangjiakou.aliyuncs.com', description: '镜像仓库地址')
  18. string(name: 'registry_ns', defaultValue: 'mldong/java', description: '镜像命名空间')
  19. string(name: 'hostname', defaultValue: 'c.mldong.com', description: '绑定的域名')
  20. string(name: 'k8sCredentialsId', defaultValue: 'ali-k8s-config', description: 'k8s集群配置id')
  21. string(name: 'k8sServerUrl', defaultValue: 'https://172.26.22.121:6443', description: 'k8s集群服务地址')
  22. }
  23. stages{
  24. stage('检出代码') {
  25. steps{
  26. // 检出代码
  27. checkout([$class: 'GitSCM', branches: [[name: "*/${params.branch_name}"]],
  28. doGenerateSubmoduleConfigurations: false,
  29. extensions: [],
  30. submoduleCfg: [],
  31. userRemoteConfigs: [[
  32. credentialsId: 'mldong-gitbash',
  33. url: "${params.git_url}"]]])
  34. }
  35. }
  36. stage("编译打包"){
  37. agent {
  38. docker {
  39. image 'maven:3-alpine'
  40. args "-v /root/.m2:/root/.m2 -v /root/${params.project_name}-config:/root/${params.project_name}-config"
  41. }
  42. }
  43. steps{
  44. // 编译打包
  45. sh "mvn -B -DskipTests clean package"
  46. // 删除当前目录下的config
  47. sh "rm -rf config"
  48. // 复制配置文件到当前工作空间
  49. sh "cp -rf /root/${params.project_name}-config config"
  50. sh "pwd"
  51. }
  52. }
  53. stage('构建镜像及推送到docker仓库') {
  54. steps {
  55. sh "pwd"
  56. // 将前一步的配置文件复制到当前目录下
  57. sh "cp -rf ${env.WORKSPACE}@2/config config"
  58. // 将前一步生成的jar包复制到当前工作空间下
  59. sh "cp -rf ${env.WORKSPACE}@2/${params.project_name}/target/${params.project_name}.jar app.jar"
  60. // 登录镜像仓库
  61. sh "docker login -u ${dockerhub_credentials_USR} -p ${dockerhub_credentials_PSW} ${params.registry_url}"
  62. // 构建镜像
  63. sh "docker build -t ${params.registry_url}/${params.registry_ns}/${params.project_name}:${image_tag} ."
  64. // 推送镜像到私服
  65. sh "docker push ${params.registry_url}/${params.registry_ns}/${params.project_name}:${image_tag}"
  66. // 删除当前目录下的config
  67. sh "rm -rf config"
  68. // 删除当前目录下的app.jar
  69. sh "rm -rf app.jar"
  70. }
  71. }
  72. stage('生成k8s发布模板') {
  73. steps {
  74. // 生成k8s发布模板
  75. sh "sed -e 's#{{APP_NAME}}#${params.project_name}#g;s#{{NAMESPACE}}#${params.project_name}-${params.profiles}#g;s#{{PROFILES}}#${params.profiles}#g;s#{{IMAGE_URL}}#${params.registry_url}/${params.registry_ns}/${params.project_name}#g;s#{{IMAGE_TAG}}#${image_tag}#g;s#{{HOST}}#${params.hostname}#g' k8s.tpl > k8s.yaml"
  76. // 暂存文件
  77. stash name: "k8s.yaml", includes: "k8s.yaml"
  78. // 查看文件
  79. sh "cat k8s.yaml"
  80. }
  81. }
  82. stage("kubectl apply") {
  83. agent {
  84. docker {
  85. image 'lwolf/helm-kubectl-docker'
  86. }
  87. }
  88. steps {
  89. withKubeConfig([credentialsId: "${params.k8sCredentialsId}",serverUrl: "${params.k8sServerUrl}"]) {
  90. // 取出文件
  91. unstash("k8s.yaml")
  92. // 发布到k8s集群
  93. sh 'kubectl apply -f k8s.yaml'
  94. }
  95. }
  96. }
  97. }
  98. }