|
| 1 | +import groovy.json.JsonSlurper |
| 2 | + |
| 3 | +pipeline { |
| 4 | + agent { |
| 5 | + node { |
| 6 | + label 'ubuntu' |
| 7 | + } |
| 8 | + } |
| 9 | + |
| 10 | + options { |
| 11 | + buildDiscarder(logRotator(daysToKeepStr: '14', artifactNumToKeepStr: '10')) |
| 12 | + } |
| 13 | + |
| 14 | + environment { |
| 15 | + JAVA_HOME = "${tool 'JDK 1.8 (latest)'}" |
| 16 | + } |
| 17 | + |
| 18 | + tools { |
| 19 | + maven 'Maven 3 (latest)' |
| 20 | + jdk 'JDK 1.8 (latest)' |
| 21 | + } |
| 22 | + |
| 23 | + triggers { |
| 24 | + cron '''TZ=Asia/Shanghai |
| 25 | + H 2,14 * * *''' |
| 26 | + pollSCM '''TZ=Asia/Shanghai |
| 27 | + H H/2 * * *''' |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + stages { |
| 32 | + stage('Clone') { |
| 33 | + steps { |
| 34 | + checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CloneOption', noTags: true, reference: '', shallow: true]], gitTool: 'Default', submoduleCfg: [], userRemoteConfigs: [[url: 'https://github.com/apache/dubbo.git']]]) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + stage('Duplicate deploy check') { |
| 39 | + steps { |
| 40 | + script { |
| 41 | + def deployedCommitId = sh(returnStdout: true, script: "curl --silent https://builds.apache.org/job/Apache%20Dubbo/job/${env.JOB_BASE_NAME}/lastSuccessfulBuild/artifact/DEPLOY_COMMIT_ID || true").trim() |
| 42 | + env.DEPLOYED_COMMIT_ID = deployedCommitId |
| 43 | + def commitId = sh(returnStdout: true, script: 'git rev-parse HEAD').trim() |
| 44 | + env.COMMIT_ID = commitId |
| 45 | + |
| 46 | + if (commitId == deployedCommitId) { |
| 47 | + env.STATUS_CHECK = "false" |
| 48 | + println "Latest deployed commit id is $deployedCommitId, Skip deployment this time" |
| 49 | + } else { |
| 50 | + env.STATUS_CHECK = "true" |
| 51 | + println "Current commit id hasn't been deployed, continue" |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + stage('Commit status check') { |
| 58 | + when { |
| 59 | + expression { |
| 60 | + return env.STATUS_CHECK == "true"; |
| 61 | + } |
| 62 | + } |
| 63 | + steps { |
| 64 | + script { |
| 65 | + def commitId = env.COMMIT_ID |
| 66 | + println "Current commit id: $commitId" |
| 67 | + |
| 68 | + def commitStatusJson = sh(script: "curl --silent https://api.github.com/repos/apache/dubbo/commits/$commitId/status", returnStdout: true).trim() |
| 69 | + println "Commit status: \r\n$commitStatusJson" |
| 70 | + |
| 71 | + def jsonSlurper = new JsonSlurper() |
| 72 | + def jsonObject = jsonSlurper.parseText(commitStatusJson) |
| 73 | + |
| 74 | + def status = jsonObject.state |
| 75 | + |
| 76 | + println "Current commit status is $status" |
| 77 | + |
| 78 | + if (status == "success") { |
| 79 | + env.STATUS_CHECK = "true" |
| 80 | + println "Continue to deploy snapshot" |
| 81 | + } else { |
| 82 | + env.STATUS_CHECK = "false" |
| 83 | + println "Current commit status not allow to deploy snapshot" |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + stage('Snapshot version check') { |
| 90 | + when { |
| 91 | + expression { |
| 92 | + return env.STATUS_CHECK == "true"; |
| 93 | + } |
| 94 | + } |
| 95 | + steps { |
| 96 | + sh 'env' |
| 97 | + sh 'java -version' |
| 98 | + sh './mvnw clean install -pl "dubbo-dependencies-bom" && ./mvnw clean install -DskipTests=true && ./mvnw clean validate -Psnapshot-ci-deploy -pl "dubbo-all"' |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + stage('Deploy snapshot') { |
| 103 | + when { |
| 104 | + expression { |
| 105 | + return env.STATUS_CHECK == "true"; |
| 106 | + } |
| 107 | + } |
| 108 | + steps { |
| 109 | + timeout(35) { |
| 110 | + sh './mvnw --version' |
| 111 | + sh './mvnw clean package deploy -pl dubbo-dependencies-bom && ./mvnw clean package deploy -DskipTests=true' |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + stage('Save deployed commit id') { |
| 117 | + steps { |
| 118 | + script { |
| 119 | + if (env.STATUS_CHECK != "true") { |
| 120 | + println "Not pass status check" |
| 121 | + env.COMMIT_ID = env.DEPLOYED_COMMIT_ID |
| 122 | + } |
| 123 | + } |
| 124 | + writeFile file: 'DEPLOY_COMMIT_ID', text: "${env.COMMIT_ID}" |
| 125 | + archiveArtifacts 'DEPLOY_COMMIT_ID' |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + post { |
| 131 | + failure { |
| 132 | + mail bcc: '', body: '''Project: ${env.JOB_NAME} |
| 133 | + Build Number: ${env.BUILD_NUMBER} |
| 134 | + URL: ${env.BUILD_URL}''', cc: '', from: '', replyTo: '', subject: 'Apache Dubbo snapshot deployment fail', to: '[email protected]' |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments