pipeline.txt 5.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. pipeline { //pipeline 顶层
  2. //any 在任何可用的agent上执行Pipeline或stage
  3. agent any
  4. //environment 自定义的环境变量
  5. environment {
  6. PROGRAM_ID = "integrated-calendar-center" //项目名称
  7. APPLICATION_NAME = "calendar-center" //项目应用名-镜像名称
  8. PROJECT_JAR_PATH = "integrated-business/integrated-calendar-center" //项目打包后所在相对路径
  9. WORKSPACE = "/root/jenkins" //jenkins home 宿主机路径
  10. PROJECT_ROOT_PATH = "integrated-calendar-center" //项目根路径名称,jenkins制作pipeline时,自定义的
  11. WORKER_ROOT_HOME = "/mnt/integrated" //工作目录根目录-容器内的路径
  12. WORKER_HOME = "$WORKER_ROOT_HOME/$APPLICATION_NAME" //项目运行所在路径-容器内的路径
  13. DEPLOY_FILE_PATH = "integrated-doc/docker/$PROGRAM_ID" //部署文件所在路径
  14. MAVEN_PACKAGE_PATH = "$WORKSPACE/workspace/$PROJECT_ROOT_PATH" //maven打包根路径
  15. IMAGE_REPOSITORY_PREFIX="labisenlin.persagy.com" //私有镜像仓库的前缀域名
  16. DOCKER_IMAGE = "$IMAGE_REPOSITORY_PREFIX/$IMAGE_REPOSITORY_PROJECT/$APPLICATION_NAME:$VERSION" //镜像全路径
  17. BACKUP_PATH = "$JENKINS_HOME/program-backup/$PROJECT_ROOT_PATH/$PROGRAM_ID/$VERSION" //备份路径
  18. SERVER_PORT = "8815" // yml部署文件中服务端口号
  19. EUREKA_CLIENT_DEFAULT_ZONE = "http://frame:123456@192.168.64.18:9931/eureka/" // yml部署文件中eureka注册地址
  20. EUREKA_INSTANCE_IP_ADDRESS = "192.168.64.17" // yml部署文件中eureka登录自身IP地址
  21. SERVER_LOGS_LOCAL_PATH = "/mnt/integrated/logs/$APPLICATION_NAME" // yml部署文件中,服务日志,宿主机存放位置
  22. REMOTE_DEPLOY = "true" // false本机部署,true-指定部署服务器
  23. REMOTE_PORT = "22" // 远程服务器ssh登录端口
  24. REMOTE_USER_IP = "root@192.168.64.17" // 远程服务器ssh登录用户及IP
  25. REMOTE_DEPLOY_PATH = "/mnt/integrated/docker" // 远程服务器部署所需的文件存放位置
  26. }
  27. //不允许并行执行Pipeline,可用于防止同时访问共享资源等
  28. options {
  29. disableConcurrentBuilds()
  30. }
  31. stages {
  32. //部署流程一:从SVN或者Git上拉取项目程序代码
  33. stage('git checkout') {
  34. steps {
  35. checkout([$class: 'GitSCM', branches: [[name: '*/$GIT_BRANCH']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitlab-integrated-server-deploy', url: 'http://39.106.8.246:3003/persagy/integrated-platform.git']]])
  36. }
  37. }
  38. //部署流程二:Java程序进行maven打包
  39. stage('maven build') {
  40. steps {
  41. sh 'mvn clean install -Dmaven.test.skip=true -f $MAVEN_PACKAGE_PATH/pom.xml'
  42. }
  43. }
  44. //部署流程三:根据项目dockerfile创建项目的docker镜像
  45. stage('image buid') {
  46. steps {
  47. sh 'chmod -R +x ./$DEPLOY_FILE_PATH/'
  48. sh 'rm -rf ./$DEPLOY_FILE_PATH/*.jar && cp ./$PROJECT_JAR_PATH/target/*.jar ./$DEPLOY_FILE_PATH'
  49. sh 'docker build -f ./$DEPLOY_FILE_PATH/Dockerfile --build-arg WORKER_HOME=$WORKER_HOME -t $DOCKER_IMAGE ./$DEPLOY_FILE_PATH'
  50. }
  51. }
  52. //部署流程四:项目推送到本地私有仓库
  53. stage('repository push') {
  54. steps {
  55. sh 'docker login $IMAGE_REPOSITORY_PREFIX -u $DOCKER_USERNAME -p $DOCKER_PASSWORD'
  56. sh 'docker push $DOCKER_IMAGE'
  57. sh 'docker logout $IMAGE_REPOSITORY_PREFIX'
  58. }
  59. }
  60. //部署流程五:项目版本本地备份
  61. stage('backup') {
  62. steps {
  63. sh 'rm -rf $BACKUP_PATH/'
  64. sh 'mkdir -p $BACKUP_PATH/'
  65. sh 'cp ./$PROJECT_JAR_PATH/target/*.jar $BACKUP_PATH/'
  66. }
  67. }
  68. //创建环境变量文件,分发文件
  69. stage('deploy') {
  70. steps {
  71. sh '''
  72. cat >$MAVEN_PACKAGE_PATH/$DEPLOY_FILE_PATH/.env <<-EOF
  73. SERVER_PORT=$SERVER_PORT
  74. DOCKER_IMAGE=$DOCKER_IMAGE
  75. WORKER_ROOT_HOME=$WORKER_ROOT_HOME
  76. APPLICATION_NAME=$APPLICATION_NAME
  77. SERVER_LOGS_LOCAL_PATH=$SERVER_LOGS_LOCAL_PATH
  78. EUREKA_CLIENT_DEFAULT_ZONE=$EUREKA_CLIENT_DEFAULT_ZONE
  79. EUREKA_INSTANCE_IP_ADDRESS=$EUREKA_INSTANCE_IP_ADDRESS
  80. EOF
  81. '''
  82. script {
  83. if (env.REMOTE_DEPLOY == 'true') {
  84. sh 'scp -q -r -P $REMOTE_PORT $MAVEN_PACKAGE_PATH/$DEPLOY_FILE_PATH/ $REMOTE_USER_IP:$REMOTE_DEPLOY_PATH'
  85. sh 'ssh -tt -p $REMOTE_PORT $REMOTE_USER_IP "$REMOTE_DEPLOY_PATH/$PROGRAM_ID/deploy.sh $APPLICATION_NAME $REMOTE_DEPLOY_PATH/$PROGRAM_ID && exit"'
  86. } else {
  87. sh '$MAVEN_PACKAGE_PATH/$DEPLOY_FILE_PATH/deploy.sh $APPLICATION_NAME $MAVEN_PACKAGE_PATH/$DEPLOY_FILE_PATH'
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }