Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ci): addresses issue about archiving in releases the APK (cycle 4.4) #2473

Merged
merged 17 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 36 additions & 1 deletion AR-builder.groovy
Original file line number Diff line number Diff line change
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()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In an ideal world, this can be simplified if ./jq would be available.
But grep, tr and cut can also manage to do it.

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'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed at all in Jenkins. Confirmed with QA.
We only run coverage in GH actions.

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
Original file line number Diff line number Diff line change
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