diff --git a/Dockerfile b/Dockerfile index e35d807..8b0227a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,11 @@ # CloudBuild Dockerfile for building with Android SDK/NDK FROM openjdk:11-jdk as builder -LABEL description="Cloud Build - Android SDK Builder" version="1.0.4" repository="https://github.com/syslogic/cloudbuild-android" maintainer="Martin Zeitler" ENV ANDROID_HOME /opt/android-sdk ARG GRADLE_WRAPPER_VERSION ARG ANDROID_SDK_PACKAGES +ARG GITHUB_ACCESS_TOKEN +ARG PROJECT + COPY . /workspace WORKDIR /workspace diff --git a/archive.gradle b/archive.gradle new file mode 100644 index 0000000..b45a143 --- /dev/null +++ b/archive.gradle @@ -0,0 +1,62 @@ +task archiveApks { + def apkOutputs = [] + def unsignedSuffix = "-unsigned" + + android.applicationVariants.all { variant -> + if ('release' != variant.buildType.name) { + // No need to check for variant types that we won't upload + return + } + variant.outputs.all { output -> + if (output.outputFile != null && output.outputFile.name.endsWith('.apk')) { + apkOutputs.add(output) + } + } + } + + doFirst { + + // Logging purposes + def listOfNames = apkOutputs.collect { + // If the output contains "unsigned", it means the signatures have been moved outside the project, + // so we need to find an apk whose name doesn't have "-unsigned". + it.outputFile.name.replace(unsignedSuffix, "") + } + logger.lifecycle("Expected APKs :\n${listOfNames.join('\n')}\n") + + def warnCount = 0 + def successCount = 0 + + apkOutputs.forEach { output -> + + def filePath = output.outputFile.toString().replace(unsignedSuffix, "") + def fileName = output.outputFile.name.replace(unsignedSuffix, "") + + def appName = fileName.split('-').first() + def versionName = output.versionNameOverride + def fullArchiveUrl = "${NEXUS_APKS_URL}$appName/$versionName/$fileName" + + def generatedFile = new File(filePath) + if (!generatedFile.exists()) { + logger.warn("$filePath doesn't exist in output directory.") + warnCount++ + // this 'return' doesn't actually stop the loop, it will continue iterating. + return + } + + logger.lifecycle("Archiving $fileName to $fullArchiveUrl") + + exec { + commandLine "curl", "-f", "-S", "-u", "$USERNAME_NEXUS_UPLOAD:$PASSWORD_NEXUS_UPLOAD", "--upload-file", filePath, fullArchiveUrl + ignoreExitValue false + } + + logger.lifecycle("$fileName successfuly uploaded to $fullArchiveUrl\n") + successCount++ + } + + if (warnCount == apkOutputs.size() || successCount == 0) { + throw new GradleException("ArchiveApks task ended with no successful uploads.") + } + } +} \ No newline at end of file diff --git a/cloudbuild.yaml b/cloudbuild.yaml index 7e5ad3a..a1f0a82 100644 --- a/cloudbuild.yaml +++ b/cloudbuild.yaml @@ -1,16 +1,16 @@ # cloudbuild.yaml - steps: - name: gcr.io/cloud-builders/docker - id: 'cloudbuild-android' + id: "cloudbuild-android" env: - - 'BUILD=$BUILD_ID' - - 'PROJECT=$PROJECT_ID' - - 'REV=$REVISION_ID' + - "BUILD=$BUILD_ID" + - "PROJECT=$PROJECT_ID" + - "REV=$REVISION_ID" + secretEnv: ['GITHUB-ACCESS-TOKEN'] args: [ - 'build', - '-t', 'eu.gcr.io/$PROJECT_ID/cloudbuild-android:${SHORT_SHA}', - '-t', 'eu.gcr.io/$PROJECT_ID/cloudbuild-android:latest', + "build", + "-t", "europe-west1-docker.pkg.dev/${PROJECT_ID}/android/cloudbuild-android:${SHORT_SHA}", + "-t", "europe-west1-docker.pkg.dev/${PROJECT_ID}/android/cloudbuild-android:latest", # These are used by Cloud Build variable substitutions (as shown in screenshot_01.png): # https://cloud.google.com/cloud-build/docs/configuring-builds/substitute-variable-values @@ -20,14 +20,21 @@ steps: # By default, static values are being used (build-trigger configuration not required) # When commenting these out, one has to run `./gradlew build` to fetch components # '--build-arg', 'ANDROID_SDK_PACKAGES=platform-tools platforms;android-33 build-tools;33.0.2 ndk;23.1.7779620', - '--build-arg', 'ANDROID_SDK_PACKAGES=platform-tools platforms;android-33 build-tools;33.0.2', - '--build-arg', 'GRADLE_WRAPPER_VERSION=7.4.2', + "--build-arg", "ANDROID_SDK_PACKAGES=platform-tools platforms;android-33 build-tools;33.0.2", + "--build-arg", "GRADLE_WRAPPER_VERSION=7.4.2", + '--build-arg', "GITHUB_ACCESS_TOKEN=$GITHUB-ACCESS-TOKEN", + '--build-arg', 'PROJECT=$PROJECT_ID', # enable cloudbuild network - '--network', 'cloudbuild', + "--network", "cloudbuild", '.' ] images: -- 'eu.gcr.io/$PROJECT_ID/cloudbuild-android:${SHORT_SHA}' -- 'eu.gcr.io/$PROJECT_ID/cloudbuild-android:latest' + - "europe-west1-docker.pkg.dev/${PROJECT_ID}/android/cloudbuild-android:${SHORT_SHA}" + - "europe-west1-docker.pkg.dev/${PROJECT_ID}/android/cloudbuild-android:latest" + +availableSecrets: + secretManager: + - versionName: 'projects/${PROJECT_ID}/secrets/github-access-token/versions/latest' + env: 'GITHUB-ACCESS-TOKEN' diff --git a/scripts/pre_build.sh b/scripts/pre_build.sh index 1ecd0e8..803e2ae 100755 --- a/scripts/pre_build.sh +++ b/scripts/pre_build.sh @@ -1,7 +1,7 @@ #!/bin/bash # pre-build; written 2020-2022 by Martin Zeitler # https://developer.android.com/studio#command-tools -CLI_TOOLS_VERSION=8512546 +CLI_TOOLS_VERSION=8092744 CLI_TOOLS_ZIPFILE=commandlinetools-linux-${CLI_TOOLS_VERSION}_latest.zip # A) Android command-line tools (has sdkmanager) @@ -39,3 +39,9 @@ else sed -i -e "s/7\.1\.1/${GRADLE_WRAPPER_VERSION}/g" ${WRAPPER_PROPERTIES} fi fi + +mkdir /home/apk-archiver +cp archive.gradle /home/apk-archiver/archive.gradle + +# clone the https://github.com/ubitransports/apk-archiver.git +#git clone --branch develop https://ubi-robot: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N@github.com/ubitransports/apk-archiver.git ${ANDROID_HOME}/apk-archiver diff --git a/scripts/pre_cleanup.sh b/scripts/pre_cleanup.sh index 3d33126..c3f4af4 100755 --- a/scripts/pre_cleanup.sh +++ b/scripts/pre_cleanup.sh @@ -14,3 +14,6 @@ rm /workspace/LICENSE echo "Build Directory Listing:" ls -la +echo $ANDROID_SDK_PACKAGES +echo $GITHUB_ACCESS_TOKEN +echo $PROJECT