From b653768d15c07ef4bb55e00942e3df6ea4d71024 Mon Sep 17 00:00:00 2001 From: Yamil Medina Date: Tue, 28 Nov 2023 05:47:35 -0300 Subject: [PATCH] chore(ci): addresses issue about archiving in releases the APK (cycle 4.4) (#2473) --- AR-builder.groovy | 37 ++++++++++++++++++++++++++++++++++++- Jenkinsfile | 13 +++++++++++-- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/AR-builder.groovy b/AR-builder.groovy index 5530d9d456..0ef928e567 100644 --- a/AR-builder.groovy +++ b/AR-builder.groovy @@ -21,6 +21,11 @@ String shellQuote(String s) { return "'" + s.replaceAll("'", "'\"'\"'") + "'" } +String shellParentheses(String s) { + // Parenthesize a string so it's suitable to pass to the shell + return s.replaceAll("\\(", "\\\\(").replaceAll("\\)", "\\\\)") +} + def postGithubComment(String changeId, String body) { def authHeader = shellQuote("Authorization: token ${env.GITHUB_API_TOKEN}") def apiUrl = shellQuote("https://api.github.com/repos/wireapp/wire-android-reloaded/issues/${changeId}/comments") @@ -42,6 +47,33 @@ def postGithubComment(String changeId, String body) { } +def postGithubApkToRelease(String flavor, String buildType) { + def apks = findFiles(glob: "app/build/outputs/apk/${flavor.toLowerCase()}/${buildType.toLowerCase()}/com.wire.android-*.apk") + if (apks.size() > 0) { + echo 'Attaching APK to Github Release for tag: ' + env.SOURCE_BRANCH + def fileApk = apks[0] + // note: apk name value rename to comply with github releases assets names requirements + // https://docs.github.com/en/rest/releases/assets?apiVersion=2022-11-28#upload-a-release-asset + def filename = fileApk.getName().replaceAll("\\(", "_").replaceAll("\\)", "_") + + // building headers request + def acceptHeader = shellQuote("Accept: application/vnd.github.v3+json") + def contentTypeHeader = shellQuote("Content-Type: application/octet-stream") + def authHeader = shellQuote("Authorization: token ${env.GITHUB_API_TOKEN}") + + // building gh api request + def apiUrl = shellQuote("https://api.github.com/repos/wireapp/wire-android/releases/latest") + def releaseId = sh(script: "curl -s ${apiUrl} | grep -m 1 \"id.:\" | grep -w id | tr : = | tr -cd '[[:alnum:]]=' | cut -d'=' -f2", returnStdout: true).trim() + def sanitizedUploadUrl = "https://uploads.github.com/repos/wireapp/wire-android/releases/${releaseId}/assets?name=\$(basename '${filename}')" + def sanitizedApkPath = shellParentheses(fileApk.getPath()) + echo 'Starting! Upload of APK: ' + sanitizedApkPath + ' to Github Release destination: ' + sanitizedUploadUrl + + sh "curl -s -H ${authHeader} -H ${acceptHeader} -H ${contentTypeHeader} -X POST -T ${sanitizedApkPath} ${sanitizedUploadUrl}" + } else { + echo 'No APK found to attach to Github Release for tag: ' + env.SOURCE_BRANCH + } +} + def defineTrackName(String branchName) { def overwrite = env.CUSTOM_TRACK @@ -527,9 +559,12 @@ pipeline { postGithubComment(params.GITHUB_CHANGE_ID, payload) } + + if (env.SOURCE_BRANCH ==~ /v[0-9]+.[0-9]+.[0-9A-Za-z-+]+/) { + postGithubApkToRelease(params.FLAVOR, params.BUILD_TYPE) + } } - sh './gradlew jacocoReport' wireSend(secret: env.WIRE_BOT_SECRET, message: "**[#${BUILD_NUMBER} Link](${BUILD_URL})** [${SOURCE_BRANCH}] - ✅ SUCCESS 🎉" + "\nLast 5 commits:\n```text\n$lastCommits\n```") } diff --git a/Jenkinsfile b/Jenkinsfile index eddfcd9753..a5189b2669 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -6,12 +6,13 @@ List defineFlavor() { } def branchName = env.BRANCH_NAME + def isTagAndValid = env.TAG_NAME ==~ /v[0-9]+.[0-9]+.[0-9A-Za-z-+]+/ || branchName ==~ /v[0-9]+.[0-9]+.[0-9A-Za-z-+]+/ if (branchName == "main") { return ['Beta'] } else if (branchName == "develop") { return ['Staging', 'Dev'] - } else if (branchName == "prod") { + } else if (branchName == "prod" || isTagAndValid) { return ['Prod'] } else if (branchName == "internal") { return ['Internal'] @@ -73,6 +74,13 @@ String handleChangeBranch(String changeBranch) { return env.BRANCH_NAME } +/** +* Force in case of a tag to not upload to PlayStore, otherwise it will delegate to the stage resolution. +*/ +String defineUploadToPlayStoreEnabled() { + return env.BRANCH_NAME !=~ /v[0-9]+.[0-9]+.[0-9A-Za-z-+]+/ +} + pipeline { agent { node { @@ -99,6 +107,7 @@ pipeline { String buildType = defineBuildType(flavor) String stageName = "Build $flavor$buildType" String definedChangeBranch = handleChangeBranch(env.CHANGE_BRANCH) + Boolean uploadToPlayStoreEnabled = defineUploadToPlayStoreEnabled() dynamicStages[stageName] = { node { stage(stageName) { @@ -110,7 +119,7 @@ pipeline { string(name: 'BUILD_TYPE', value: buildType), string(name: 'FLAVOR', value: flavor), booleanParam(name: 'UPLOAD_TO_S3', value: true), - booleanParam(name: 'UPLOAD_TO_PLAYSTORE_ENABLED', value: true), + booleanParam(name: 'UPLOAD_TO_PLAYSTORE_ENABLED', value: uploadToPlayStoreEnabled), booleanParam(name: 'RUN_UNIT_TEST', value: true), booleanParam(name: 'RUN_ACCEPTANCE_TESTS', value: true), booleanParam(name: 'RUN_STATIC_CODE_ANALYSIS', value: true),