Pipeline内容
properties ( [ parameters ( [ [ $ class : 'CascadeChoiceParameter' , choiceType: 'PT_CHECKBOX' , description: '选择 受管服务器' , filterLength: 1 , filterable: false , name: 'WLS_Targets' , randomName: 'choice-parameter-173347495550024' , referencedParameters: 'Action' , script: [ $ class : 'GroovyScript' , fallbackScript: [ classpath: [ ] , oldScript: '' , sandbox: false , script: 'return ["Envrionment Undefined"]' ] , script: [ classpath: [ ] , oldScript: '' , sandbox: false , script: '''if (Action.equals("Deploy")) {return ["AdminServer:selected","serverA","serverB","serverC"]
} ''' ] ] ] ] ) ] ) pipeline { agent anytools { maven 'MAVEN3' jdk 'JDK1.8' } environment { svn_path = "svn://172.16.20.70/ccms-project" svn_auth_id = "cdb6584d-9f9b-4ecb-890e-a15081c0abca" appName = "ccms-test" warName = "water-test-1.0-SNAPSHOT.war" } parameters { choice choices: [ 'Deploy' , 'Delete' ] , description: '选择 发布项目|删除项目' , name: 'Action' extendedChoice name: 'MavenBuild' , type: 'PT_RADIO' , description: '发版选项: 是否进行全新的Maven构建; 删除项目不需要勾选' , quoteValue: false , saveJSONParameterToFile: false , value: 'Yes,Clean' , descriptionPropertyValue: '进行Maven构建并发版,清除Maven缓存全新构' , visibleItemCount: 5 , multiSelectDelimiter: ',' , defaultValue: 'Yes' extendedChoice name: 'WLS_Servers' , type: 'PT_CHECKBOX' , description: '请选择weblogic服务器' , quoteValue: false , saveJSONParameterToFile: false , value: 'weblogic1,weblogic2,weblogic3,weblogic4' , descriptionPropertyValue: '172.16.20.70,172.16.20.71,172.16.20.72,172.16.20.73' , visibleItemCount: 10 , multiSelectDelimiter: ',' , defaultValue: 'weblogic1' } stages { stage ( '项目代码拉取' ) { when { anyOf { allOf { environment name: 'Action' , value: 'Deploy' environment name: 'MavenBuild' , value: 'Yes' } allOf { environment name: 'Action' , value: 'Deploy' environment name: 'MavenBuild' , value: 'Clean' } } } steps{ script { if ( env. Action == "Deploy" && env. MavenBuild == "Clean" ) { deleteDir ( ) checkout ( [ $ class : 'SubversionSCM' , filterChangelog: false , ignoreDirPropChanges: false , includedRegions: '' , locations: [ [ cancelProcessOnExternalsFail: true , credentialsId: " ${ svn_auth_id } " , depthOption: 'infinity' , ignoreExternalsOption: true , local: '.' , remote: " ${ svn_path } " ] ] , quietOperation: true , workspaceUpdater: [ $ class : 'UpdateUpdater' ] ] ) } else { checkout ( [ $ class : 'SubversionSCM' , filterChangelog: false , ignoreDirPropChanges: false , includedRegions: '' , locations: [ [ cancelProcessOnExternalsFail: true , credentialsId: " ${ svn_auth_id } " , depthOption: 'infinity' , ignoreExternalsOption: true , local: '.' , remote: " ${ svn_path } " ] ] , quietOperation: true , workspaceUpdater: [ $ class : 'UpdateUpdater' ] ] ) } } } } stage ( '项目构建' ) { when { anyOf { allOf { environment name: 'Action' , value: 'Deploy' environment name: 'MavenBuild' , value: 'Yes' } allOf { environment name: 'Action' , value: 'Deploy' environment name: 'MavenBuild' , value: 'Clean' } } } steps { script { if ( env. MavenBuild == "Clean" ) { sh "rm -rf /root/.m2" withMaven ( jdk: 'JDK1.8' , maven: 'MAVEN3' ) { sh '''java -versionmvn -versionmvn clean install -Dmaven.test.skip=true''' } } else { withMaven ( jdk: 'JDK1.8' , maven: 'MAVEN3' ) { sh '''java -versionmvn -versionmvn clean install -Dmaven.test.skip=true''' } } } } } stage ( '项目发布' ) { when { environment name: 'Action' , value: 'Deploy' } steps { script{ for ( wls_server in WLS_Servers. tokenize ( ',' ) ) { def target_dir = "target" def target_file = " ${ warName } " def source_file = " ${ target_dir } / ${ target_file } " def remove_prefix = "target" def remote_dir = "version" def remote_cmd = "cd /home/weblogic/; /bin/sh ccms-deploy.sh deploy ${ appName } ${ warName } ${ WLS_Targets } >> version/version-deploy.log" sshPublisher ( publishers: [ sshPublisherDesc ( configName: " ${ wls_server } " , transfers: [ sshTransfer ( execCommand: " ${ remote_cmd } " , remoteDirectory: " ${ remote_dir } " , removePrefix: " ${ remove_prefix } " , sourceFiles: " ${ source_file } " ) ] , ) ] ) } } } } stage ( '项目删除' ) { when { environment name: 'Action' , value: 'Delete' } steps { script{ for ( wls_server in WLS_Servers. tokenize ( ',' ) ) { def target_dir = "" def target_file = "" def source_file = "" def remove_prefix = "" def remote_dir = "" def remote_cmd = "cd /home/weblogic/; /bin/sh ccms-deploy.sh delete ${ appName } >> version/version-delete.log" sshPublisher ( publishers: [ sshPublisherDesc ( configName: " ${ wls_server } " , transfers: [ sshTransfer ( execCommand: " ${ remote_cmd } " , remoteDirectory: " ${ remote_dir } " , removePrefix: " ${ remove_prefix } " , sourceFiles: " ${ source_file } " ) ] , ) ] ) } } } } }
}
脚本内容
#!/bin/bash
wls_classpath = /home/weblogic/Oracle/Middleware/wlserver/server/lib/action = $1
appName = $2
warName = $3
target = $4 wlsaddr = "t3://172.16.20.71:7001"
wlsuser = weblogic
wlspasswd = weblogic1save_files = 5 deploy ( ) {
java -cp ${wls_classpath} /weblogic.jar weblogic.Deployer -adminurl $wlsaddr -user $wlsuser -password $wlspasswd -name $appName -deploy -targets $target -source /home/weblogic/version/$warName
} delete ( ) {
java -cp ${wls_classpath} /weblogic.jar weblogic.Deployer -adminurl $wlsaddr -user $wlsuser -password $wlspasswd -name $appName -undeploy
} cleanWar ( ) {
cd /home/weblogic/version
while true ;
do file_count = ` ls -1t ${ warName} .*| wc -l ` if [ ${file_count} -gt ${save_files} ] ; then ls -1t ${warName} .*| tail -1| xargs rm -f else break ; fi
done
}
if [ "$action " == "deploy" ] ; then deployif [ "$? " -eq "0" ] ; then cd /home/weblogic/version; mv $warName $warName .bak_$( date '+%Y%m%d%H%M' ) fi cleanWar
elif [ "$action " == "delete" ] ; then delete
fi