pipeline.txt 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. pipeline { //pipeline 顶层
  2. //any 在任何可用的agent上执行Pipeline或stage
  3. agent any
  4. //environment 自定义的环境变量
  5. environment {
  6. //本项目此三个变量值如下,修改jenkins打包即可,其余不动
  7. //PROGRAM_ID=integrated-eureka
  8. //APPLICATION_NAME=integrated-eureka
  9. //PROJECT_JAR_PATH=integrated-eureka
  10. PROGRAM_ID = "$PROGRAM_ID" //项目名称
  11. APPLICATION_NAME = "$PROGRAM_ID" //项目应用名-镜像名称
  12. WORKSPACE = "/root/jenkins" //jenkins home 宿主机路径
  13. PROJECT_JAR_PATH = "$PROJECT_JAR_PATH" //项目打包后所在相对路径
  14. PROJECT_ROOT_PATH = "zkt-alarm" //项目根路径名称
  15. CONTAINER_HOME = "$CONTAINER_HOME" //容器内的基本路径
  16. WORKER_HOME = "$CONTAINER_HOME/$APPLICATION_NAME" //项目运行所在路径-容器内的路径
  17. DEPLOY_FILE_PATH = "docker" //部署文件所在路径
  18. MAVEN_PACKAGE_PATH = "$WORKSPACE/workspace/$PROGRAM_ID" //maven打包根路径
  19. IMAGE_REPOSITORY_PREFIX="labisenlin.persagy.com"
  20. DOCKER_IMAGE = "$IMAGE_REPOSITORY_PREFIX/$IMAGE_REPOSITORY_PROJECT/$APPLICATION_NAME:$VERSION" //镜像全路径
  21. BACKUP_PATH = "$JENKINS_HOME/program-backup/$PROGRAM_ID/$VERSION" //备份路径
  22. }
  23. //不允许并行执行Pipeline,可用于防止同时访问共享资源等
  24. options {
  25. disableConcurrentBuilds()
  26. }
  27. stages {
  28. //部署流程一:从SVN或者Git上拉取项目程序代码
  29. stage('git checkout') {
  30. steps {
  31. checkout([$class: 'GitSCM', branches: [[name: '*/$GIT_BRANCH']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'zkt-git-server', url: 'http://39.106.8.246:3003/persagy/persagy-zkt-project-alarm.git']]])
  32. }
  33. }
  34. //部署流程二:Java程序进行maven打包
  35. stage('maven build') {
  36. steps {
  37. sh 'cd $WORKSPACE'
  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 ./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. //-H tcp://192.168.64.16:9999
  53. sh 'docker login $IMAGE_REPOSITORY_PREFIX -u $DOCKER_USERNAME -p $DOCKER_PASSWORD'
  54. sh 'docker push $DOCKER_IMAGE'
  55. //只打镜像的话,将其打开
  56. sh 'docker logout $IMAGE_REPOSITORY_PREFIX'
  57. sh 'docker rmi $DOCKER_IMAGE'
  58. }
  59. }
  60. //部署流程五:项目版本本地备份
  61. stage('backup') {
  62. steps {
  63. sh 'rm -rf $BACKUP_PATH/'
  64. sh 'mkdir -p $BACKUP_PATH/'
  65. sh 'cp ./target/*.jar $BACKUP_PATH/'
  66. }
  67. }
  68. //部署流程六:项目docker部署运行
  69. stage('deploy') {
  70. steps {
  71. sh 'rm -rf /root/jenkins/workspace/zkt-project-alarm/docker/.env'
  72. sh 'echo "tag = $VERSION" >> /root/jenkins/workspace/zkt-project-alarm/docker/.env'
  73. sh 'ssh -tt root@192.168.64.16 "rm -rf /mnt/zkt/zkt-project-alarm/ && mkdir -p /mnt/zkt/zkt-project-alarm/ && scp -r root@192.168.64.18:/root/jenkins/workspace/zkt-project-alarm/docker/ /mnt/zkt/zkt-project-alarm/docker/ && cd /mnt/zkt/zkt-project-alarm/ && sh ./docker/deploy.sh " '
  74. }
  75. }
  76. }
  77. }