pipeline.txt 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 = "$APPLICATION_NAME" //项目应用名-镜像名称
  12. WORKSPACE = "/root/jenkins" //jenkins home 宿主机路径
  13. PROJECT_JAR_PATH = "$PROJECT_JAR_PATH" //项目打包后所在相对路径
  14. PROJECT_ROOT_PATH = "persagy-sync-data" //项目根路径名称
  15. WORKER_HOME = "/root/$APPLICATION_NAME" //项目运行所在路径-容器内的路径
  16. DEPLOY_FILE_PATH = "docker" //部署文件所在路径
  17. MAVEN_PACKAGE_PATH = "$WORKSPACE/workspace/$PROGRAM_ID" //maven打包根路径
  18. IMAGE_REPOSITORY_PREFIX="labisenlin.persagy.com"
  19. DOCKER_IMAGE = "$IMAGE_REPOSITORY_PREFIX/$IMAGE_REPOSITORY_PROJECT/$APPLICATION_NAME:$VERSION" //镜像全路径
  20. BACKUP_PATH = "$JENKINS_HOME/program-backup/$PROGRAM_ID/$VERSION" //备份路径
  21. }
  22. //不允许并行执行Pipeline,可用于防止同时访问共享资源等
  23. options {
  24. disableConcurrentBuilds()
  25. }
  26. stages {
  27. //部署流程一:从SVN或者Git上拉取项目程序代码
  28. stage('git checkout') {
  29. steps {
  30. checkout([$class: 'GitSCM', branches: [[name: '*/$GIT_BRANCH']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'weiyizhong', url: 'http://39.106.8.246:3003/weiyizhong/persagy-sync-data.git']]])
  31. }
  32. }
  33. //部署流程二:Java程序进行maven打包
  34. stage('maven build') {
  35. steps {
  36. sh 'cd $WORKSPACE'
  37. sh 'mvn clean install -Dmaven.test.skip=true -f $MAVEN_PACKAGE_PATH/pom.xml'
  38. }
  39. }
  40. //部署流程三:根据项目dockerfile创建项目的docker镜像
  41. stage('image buid') {
  42. steps {
  43. sh 'chmod -R +x ./$DEPLOY_FILE_PATH/'
  44. sh 'rm -rf ./$DEPLOY_FILE_PATH/*.jar && cp ./target/*.jar ./$DEPLOY_FILE_PATH'
  45. sh 'docker build -f ./$DEPLOY_FILE_PATH/Dockerfile --build-arg WORKER_HOME=$WORKER_HOME -t $DOCKER_IMAGE ./$DEPLOY_FILE_PATH'
  46. }
  47. }
  48. //部署流程四:项目推送到本地私有仓库
  49. stage('repository push') {
  50. steps {
  51. //-H tcp://192.168.64.16:9999
  52. sh 'docker login $IMAGE_REPOSITORY_PREFIX -u $DOCKER_USERNAME -p $DOCKER_PASSWORD'
  53. sh 'docker push $DOCKER_IMAGE'
  54. //只打镜像的话,将其打开
  55. sh 'docker logout $IMAGE_REPOSITORY_PREFIX'
  56. sh 'docker rmi $DOCKER_IMAGE'
  57. }
  58. }
  59. }
  60. }