pipeline.txt 4.8 KB

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