olegbo

Untitled

Jun 9th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.96 KB | None | 0 0
  1. import groovy.json.JsonSlurper
  2. import groovy.json.JsonBuilder
  3.  
  4. node('master') {
  5. sendNotification("\uD83D\uDE80 Build started: ${env.JOB_NAME} <a href='${env.BUILD_URL}'>#${env.BUILD_ID}</a>")
  6. try {
  7. stage('checkout') {
  8. git credentialsId: '30e4e61b-f18e-4aec-92b1-da1cb44b72ea', branch: 'develop', url: 'https://stash.olegb.ru/scm/webimclntandr/webim-client-sdk-android.git'
  9. }
  10. stage('clean') {
  11. sh 'rm -rf sdk/build/libs/* && rm -rf sdk/build/outputs/aar/* && rm -rf demo/build/outputs/apk/*'
  12. }
  13. def version
  14. stage('read version') {
  15. if(!env.OVERRIDE_VERSION.isEmpty()) {
  16. version = ['version': env.OVERRIDE_VERSION]
  17. } else {
  18. def gitDesc = sh(script: 'git describe --tags --long', returnStdout: true).trim()
  19. def lastVTag = sh(script: "git log --decorate=full --simplify-by-decoration --pretty=oneline HEAD | " +
  20. "sed -r -e 's#^[^\\(]*\\(([^\\)]*)\\).*\$#\\1#' -e 's#,#\\n#g' | grep 'tag: v' | " +
  21. "sed -r -e 's#[[:space:]]*tag:[[:space:]]*##' | head -1", returnStdout: true).trim()
  22. if(lastVTag.isEmpty()) throw new RuntimeException('No tags prefixed with "v" found. Add version tag vefore build')
  23. version = getVersion(gitDesc, lastVTag);
  24. }
  25. echo 'Version: ' + version.version
  26. currentBuild.description = version.version
  27. }
  28. stage('build') {
  29. def tasks = ['build']
  30. if(env.BINTRAY_UPLOAD == 'true') tasks << 'bintrayUpload'
  31. if(env.CRASHLYTICS_UPLOAD == 'true') tasks << 'crashlyticsUploadDistributionRelease'
  32. withCredentials([[$class: 'StringBinding', credentialsId: 'bintray_api_key', variable: 'BINTRAY_API_KEY'],
  33. [$class: 'FileBinding', credentialsId: 'google_play_demo_app.jks', variable: 'KEYSTORE_FILE'],
  34. [$class: 'StringBinding', credentialsId: 'google_play_demo_app_jks_password', variable: 'KEYSTORE_PASSWORD'],
  35. [$class: 'StringBinding', credentialsId: 'google_play_demo_app_jks_alias', variable: 'KEYSTORE_ALIAS'],
  36. [$class: 'StringBinding', credentialsId: 'google_play_demo_app_jks_alias_password', variable: 'KEYSTORE_ALIAS_PASSWORD']]) {
  37. sh(
  38. './gradlew ' +
  39. tasks.join(' ') +
  40. ' --stacktrace' +
  41. ' -PversionName=' + version.version +
  42. ' -PversionCode=' + env.VERSION_CODE +
  43.  
  44. ' -Pbintray.user=Webimdev' +
  45. ' -Pbintray.apikey=$BINTRAY_API_KEY' +
  46. ' -Pbintray.pkg.userOrg=webimdev' +
  47. ' -Pbintray.pkg.name=WebimSdkAndroid_dev' +
  48. ' -Pbintray.override=' + env.BINTRAY_OVERRIDE +
  49.  
  50. ' -Pkeystore.path=$KEYSTORE_FILE' +
  51. ' -Pkeystore.password=$KEYSTORE_PASSWORD' +
  52. ' -Pkeystore.key.release.alias=$KEYSTORE_ALIAS' +
  53. ' -Pkeystore.key.release.password=$KEYSTORE_ALIAS_PASSWORD' +
  54.  
  55. ' -PbetaDistributionEmails=\'' + env.CRASHLYTICS_DIST_EMAILS + '\''
  56. )
  57. }
  58. }
  59. def bintrayUrl = 'https://dl.bintray.com/webimdev/maven/com/webimapp/sdk/webimclientsdkandroid/' + version.version
  60. stage('archive') {
  61. // sh 'mkdir -p demo/build/outputs/lint && cp demo/build/outputs/lint-results-debug.html demo/build/outputs/lint/lint-results-debug.html'
  62. // sh 'mkdir -p sdk/build/outputs/lint && cp sdk/build/outputs/lint-results-debug.html sdk/build/outputs/lint/lint-results-debug.html'
  63. // publishHTML([allowMissing: true, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'demo/build/outputs/lint/', reportFiles: 'lint-results-debug.html',
  64. // reportName: 'Demo android lint results'])
  65. // publishHTML([allowMissing: true, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'sdk/build/outputs/lint/', reportFiles: 'lint-results-debug.html',
  66. // reportName: 'SDK android lint results'])
  67. archiveArtifacts allowEmptyArchive: true, artifacts: 'sdk/build/outputs/aar/webimclientsdkandroid-release.aar, demo/build/outputs/apk/demo-release.apk',
  68. onlyIfSuccessful: true
  69. if(env.BINTRAY_UPLOAD == 'true')
  70. manager.createSummary('document.png')
  71. .appendText("<a href='$bintrayUrl'>Bintray release</a>", false)
  72. }
  73. stage('write version') {
  74. if(env.OVERRIDE_VERSION.isEmpty())
  75. saveVersion(version)
  76. }
  77.  
  78. def deployedList = []
  79. if(env.BINTRAY_UPLOAD == 'true') deployedList << "<a href='$bintrayUrl'>bintray</a> (dev)"
  80. if(env.CRASHLYTICS_UPLOAD == 'true') deployedList << 'Crashlytics Beta'
  81. sendNotification("\uD83D\uDE03 Build successful: ${env.JOB_NAME} <a href='${env.BUILD_URL}'>#${env.BUILD_ID}</a>\n" +
  82. "Version: ${version.version}" +
  83. (deployedList.isEmpty() ? '' : '\nPublished to: ' + deployedList.join(', ')))
  84. } catch(e) {
  85. try {
  86. sendNotification("\uD83D\uDE21 Build failed: ${env.JOB_NAME} <a href='${env.BUILD_URL}console'>#${env.BUILD_ID}</a>\n" + e.toString())
  87. } catch(ignored) {}
  88. throw e
  89. }
  90. }
  91.  
  92. def sendNotification(def html) {
  93. //httpRequest 'https://api.telegram.org/bot274408872:AAHaYG_WTeaqnANal55uJHroiBmU74q4wek/sendMessage?' +
  94. // 'chat_id=-1001094070329&disable_web_page_preview=true&parse_mode=html&text=' + java.net.URLEncoder.encode(html)
  95. }
  96.  
  97. @NonCPS
  98. def getVersion(def gitDesc, def lastVTag) {
  99. def (tag, commit) = getGitTagAndCommit(gitDesc)
  100. tag = lastVTag.substring(1) //removing 'v'
  101. def json = new JsonSlurper().parseText(env.VERSIONS_JSON)
  102.  
  103. def revision = 0
  104. if(json.containsKey(tag)) {
  105. def part = json.get(tag)
  106. revision = part['revision'].toInteger()
  107. if(part['commit'] != commit || env.FORCE_INC_REV == 'true')
  108. revision++
  109. }
  110.  
  111. def version;
  112. def suffix = env.PRE_RELEASE_SUFFIX
  113. if(suffix.isEmpty()) {
  114. version = tag+'.'+revision;
  115. } else {
  116. String[] parts = tag.split('\\.')
  117. String major = parts[0];
  118. int minor = parts[1].isEmpty() || parts[1] =='#' ? 0 : (parts[1].toInteger() + 1)
  119. version = major+'.'+minor+'.0-'+suffix+(revision == 0 ? '' : '.'+revision)
  120. }
  121.  
  122. return ['version': version, 'tag': tag, 'revision': revision, 'commit': commit]
  123. }
  124.  
  125. @NonCPS
  126. def saveVersion(def version) {
  127. def json = new JsonSlurper().parseText(env.VERSIONS_JSON)
  128. json[version.tag] = ['revision': version.revision, 'commit': version.commit]
  129.  
  130. setPropertyDefaultValue('VERSIONS_JSON', new JsonBuilder(json).toPrettyString())
  131. }
  132.  
  133. @NonCPS
  134. def setPropertyDefaultValue(def name, def newDefaultValue) {
  135. def paramsDef = manager.build.getParent().getProperty(ParametersDefinitionProperty.class)
  136. if (paramsDef) {
  137. paramsDef.parameterDefinitions.each{ param ->
  138. if (param.name == name) {
  139. if(param.defaultValue != newDefaultValue) {
  140. echo "Changing parameter ${param.name} default value was '${param.defaultValue}' to '${newDefaultValue}'"
  141. param.defaultValue = newDefaultValue
  142. manager.build.getParent().save()
  143. } else {
  144. echo "Parameter ${param.name} has not changed"
  145. }
  146. }
  147. }
  148. }
  149. }
  150.  
  151. @NonCPS
  152. def getGitTagAndCommit(def gitDesc) {
  153. String[] parts = gitDesc.split('-')
  154. if(parts.length != 3) throw new RuntimeException('no git tags found')
  155. def tag = parts[0]
  156. def commit = parts[2].substring(1) //removing 'g'
  157. return [tag, commit]
  158. }
Add Comment
Please, Sign In to add comment