1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| pipeline { agent any environment { gitlab_address = "http://192.168.64.17/microservice/demo.git" gitlab_auth = "96b60e82-a827-48ba-ba52-f5cab7e9aa49" } parameters { gitParameter branch: '', branchFilter: '.*', defaultValue: 'master', description: '请选择要发布的分支名称', name: 'selectBranch', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'PT_BRANCH' } triggers { pollSCM('*/1 * * * *') } stages { stage('拉取代码') { steps { checkout([$class: 'GitSCM', branches: [[name: "${params.selectBranch}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${gitlab_auth}", url: "${gitlab_address}"]]]) } } stage('编译构建') { steps { sh "/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true" } } stage('拷贝构建文件到远程主机并部署') { steps { sshPublisher(publishers: [sshPublisherDesc(configName: '192.168.31.73', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''# 备份已部署的程序 tomcat=/usr/local/tomcat cd $tomcat/webapps mv ROOT.war /data/backup/ROOT-$(date +%F-%T).war # 部署新程序并重启Tomcat mv *.war ROOT.war pid=$(ps -ef |grep $tomcat |egrep -v \'grep\' |awk \'{print $2}\') export JAVA_HOME=/usr/local/jdk [ -n "$pid" ] && kill -9 $pid /usr/local/tomcat/bin/startup.sh ''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '$JOB_NAME', remoteDirectorySDF: false, removePrefix: 'target', sourceFiles: 'target/*.war')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) } } } }
|