node { echo 'Hello World' } node 在Jenkins环境中分配一个执行器和工作空间。 echo 在控制台输出中写入简单的字符串 try { timeout(time: 120, unit: 'MINUTES') { def app_name_prefix="trmserverdev" // define maven commands def mvn_cmd = 'mvn -s mvn-settings.xml' //maven配置(nexus/sonar配置) // define gitlab_project url def giturl = "http://10.72.76.54/TRM/TRMServer.git" // define gitlab_project branch def git_branch = "master" // define sonar url def sonarurl = 'http://10.72.76.55:9002' // define jenkins token def credentialsId = "dai" def appName = "${app_name_prefix}" node { stage("Initialize") { project = env.PROJECT_NAME echo "project: ${project}" } } node("maven") { stage("Checkout") { checkout([$class:"GitSCM", branches: [[name: "*/${git_branch}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${credentialsId}", url: "${giturl}"]]]) } stage("Unit Test") { sh "${mvn_cmd} test" } stage("Sonar") { // sh "${mvn_cmd} sonar:sonar -Dsonar.sourceEncoding=UTF-8 -Dsonar.host.url=${sonarurl} -Dsonar.projectName=${appName}-${git_branch} -Dsonar.projectKey=${appName}-${git_branch}" } stage("Build WAR") { sh "${mvn_cmd} clean package" stash name:"war", includes:"target/TRMServerdev.war" //包名 与项目、镜像名保持一致,附加版本号 } stage ('Clear Environment') { sh "rm -rf oc-build && mkdir -p oc-build/deployments" sh "oc delete bc,dc,svc,route,is -l app=${appName} -n ${project}" } stage("Build Image") { //包名 与项目、镜像名保持一致,附加版本号 sh "oc project ${project}" sh "cp target/TRMServerdev.war oc-build/deployments/" sh "oc new-build --binary=true --name=${appName} -l app=${appName} -n ${project} --image-stream=openshift/jboss-webserver31-tomcat8-openshift:1.2" sh "oc start-build ${appName} --from-dir=oc-build --follow=true --wait=true -n ${project}" } stage("Deploy") { //URL附加版本号 sh "oc new-app --name=${appName} --image-stream=${appName} --env TZ=Asia/Shanghai" sh "oc expose svc/${appName} --hostname=${appName}-${project}sit.devapp.cnooc -n ${project}" } stage('Integration test') { //自动化测试 jmeter println 'Starting test' // TBD: Proper test println 'Test completed!' } //增加发邮件 } } } catch (err) { //完善反馈信息 def comment = "编译失败,错误原因如下:${err} " //jiraAddComment idOrKey: "${params.issueKey}", comment: comment, site: 'cnooc jira' //失败状态流转 def transitionInput = [ transition: [id: '81'] ] //jiraTransitionIssue idOrKey: "${params.issueKey}", input: transitionInput,site: 'cnooc jira' echo "in catch block" echo "Caught: ${err}" currentBuild.result = 'FAILURE' throw err }