buildAndPublish.sh 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/bin/bash
  2. # 源码存放根目录
  3. source_dir=/java_projects/source/back
  4. # 父工程目录
  5. parent_dir=$source_dir/mldong
  6. # 需要打包的项目名称
  7. project_name=mldong-admin
  8. # 项目部署目录
  9. project_dir=/java_projects/$project_name
  10. # git仓库地址(使用ssh方式的,需要去配置部署公钥)
  11. git_url=git@gitee.com:mldong/mldong.git
  12. registry_url=registry-vpc.cn-zhangjiakou.aliyuncs.com
  13. registry_ns=mldong/java
  14. registry_username=registry_username
  15. registry_password=password
  16. image_url=$registry_url/$registry_ns/$project_name
  17. host=c.mldong.com
  18. # 环境定义
  19. profiles=test
  20. if [ -d "$source_dir" ]; then
  21. echo "源码存放根目录${source_dir}已存在"
  22. else
  23. echo "源码存放根目录不存在,创建${source_dir}"
  24. cp -p $source_dir
  25. fi
  26. if [ -d "$parent_dir" ]; then
  27. echo "源码已存在,git pull"
  28. cd $parent_dir
  29. git pull
  30. else
  31. echo "源码不存在,git clone"
  32. git clone $git_url $parent_dir
  33. fi
  34. git_version=$(git rev-parse HEAD)
  35. echo "当前版本号:${git_version}"
  36. image_tag=`date +"%Y%m%d%H%M"_``git describe --tags --always`
  37. cd $parent_dir
  38. mvn clean package -B
  39. # 这里需要判断打包是否成功
  40. if [ $? -ne 0 ]; then
  41. echo "打包失败"
  42. else
  43. # 复制jar包
  44. cp -r -f $parent_dir/$project_name/target/$project_name.jar $project_dir/app.jar
  45. # 进入项目目录
  46. cd $project_dir
  47. # 构建镜像
  48. docker build -t $registry_url/$registry_ns/$project_name:$image_tag .
  49. # 登录私服
  50. docker login -u $registry_username -p ${registry_password} $registry_url
  51. # 退送到私服
  52. docker push $registry_url/$registry_ns/$project_name:$image_tag
  53. sed -e "s#{{APP_NAME}}#$project_name#g;s#{{NAMESPACE}}#$project_name-$PROFILES#g;s#{{PROFILES}}#$profiles#g;s#{{IMAGE_URL}}#$image_url#g;s#{{IMAGE_TAG}}#$image_tag#g;s#{{HOST}}#$host#g" k8s.tpl > k8s.yaml
  54. if [ $? -ne 0 ]; then
  55. echo "构建镜像推送到私服失败"
  56. else
  57. cat k8s.yaml
  58. kubectl apply -f k8s.yaml
  59. fi
  60. fi