Skip to content

Commit

Permalink
chore(ci): addresses issue about archiving in releases the APK (cycle…
Browse files Browse the repository at this point in the history
… 4.4) (#2473)
  • Loading branch information
yamilmedina committed Nov 28, 2023
1 parent fa86e9c commit b653768
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
37 changes: 36 additions & 1 deletion AR-builder.groovy
Expand Up @@ -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")
Expand All @@ -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

Expand Down Expand Up @@ -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```")
}

Expand Down
13 changes: 11 additions & 2 deletions Jenkinsfile
Expand Up @@ -6,12 +6,13 @@ List<String> 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']
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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),
Expand Down

0 comments on commit b653768

Please sign in to comment.