Skip to content

Instantly share code, notes, and snippets.

@dmitriy-chernysh
Last active January 25, 2023 18:50
Show Gist options
  • Save dmitriy-chernysh/b420d91788ff012da246fede67eae7c3 to your computer and use it in GitHub Desktop.
Save dmitriy-chernysh/b420d91788ff012da246fede67eae7c3 to your computer and use it in GitHub Desktop.
CircleCI 2.1 | Config | Run tests, build & deploy app release | Android
version: 2.1
# Updated on May 10, 2022
config_docker: &config_docker
docker:
- image: cimg/android:2021.10.2
resource_class: large
working_directory: ~/code
config_env: &config_env
environment:
JAVA_HOME: "/usr/lib/jvm/java-11-openjdk-amd64"
JAVA_TOOL_OPTIONS: "-Xmx4096m -XX:+UseParallelGC"
GRADLE_OPTS: "-Dorg.gradle.daemon=true -Dorg.gradle.workers.max=2 -Dkotlin.incremental=true -Dkotlin.compiler.execution.strategy=in-process"
TERM: dumb
update_sdk: &update_sdk
run:
name: Update SDK
command: |
yes | sdkmanager --licenses || true
sdkmanager "platform-tools" "platforms;android-31"
restore_cache: &restore_cache
restore_cache:
key: jars-{{ checksum "build.gradle" }}-{{ checksum "app/build.gradle" }}
fix_permissions: &fix_permissions
run:
name: Chmod permissions #if permission for Gradlew Dependencies fail, use this.
command: sudo chmod +x ./gradlew
update_dependencies: &update_dependencies
run:
name: Download Dependencies
command: ./gradlew dependencies
update_cache: &update_cache
save_cache:
paths:
- ~/.gradle/caches
- ~/.gradle/wrapper
key: jars-{{ checksum "build.gradle" }}-{{ checksum "app/build.gradle" }}
decrypt_release_key: &decrypt_release_key
deploy:
name: Decrypt release key
command: ./ci-decrypt-release-key.sh
deploy_to_dropbox: &deploy_to_dropbox
deploy:
name: Deploy to DropBox
command: ./ci-deploy-dropbox.sh
push_to_owner_repo: &push_to_backup_repo
run:
name: Push to backup repository
command: ./ci-push-to-backup-repo.sh
cache_docker: &cache_docker
setup_remote_docker:
docker_layer_caching: false # it costs extra credits
filter_master_only: &filter_master_only
filters:
branches:
only: master
filter_tag_version_only: &filter_tag_version_only
filters:
tags:
only: /^v.*/
branches:
ignore: /.*/
jobs:
job_run_tests:
<<: *config_docker
<<: *config_env
steps:
- <<: *update_sdk
- checkout
# Docker caching is not available in free plans
- <<: *cache_docker
- <<: *restore_cache
- <<: *fix_permissions
- <<: *update_dependencies
- <<: *update_cache
- run:
name: Run Tests
command: ./gradlew testDebug
- run:
name: Save test results (html page, see Artifacts)
command: |
mkdir -p test-results/html/
find . -type d -regex ".*/*reports/tests" -exec cp -avr {} test-results/html/ \;
when: always
- run:
name: Save test results (xml results, see Tests)
command: |
mkdir -p test-results/xml/
find . -type f -regex ".*/*test-results/.*xml" -exec cp {} test-results/xml/ \;
when: always
- store_test_results:
path: test-results
- store_artifacts:
path: test-results
destination: reports/
job_build_n_deploy_release:
<<: *config_docker
<<: *config_env
steps:
- <<: *update_sdk
- checkout
- <<: *restore_cache
- <<: *fix_permissions
- <<: *update_dependencies
- <<: *update_cache
- <<: *decrypt_release_key
- run:
name: Assemble release build
command: ./gradlew clean bundleRelease --stacktrace
- store_artifacts:
path: app/build/outputs/apk/
destination: apks/
- run:
name: Remove release key
command: rm ./app/release.jks
- <<: *deploy_to_dropbox
workflows:
version: 2
# run tests on every commits to master
run-tests-only:
jobs:
- job_run_tests
# build release and deploy on tags and not branches
buid-n-deploy:
jobs:
- job_build_n_deploy_release:
<<: *filter_tag_version_only
# See https://circleci.com/docs/2.0/deployment-integrations/ for deploy examples
#! /bin/sh
#
# How to get KEYSTORE_SECRET string: "gpg -c --armor release.jks"
#
# Before adding this string as ENV to Circle CI, do the following:
# 1) First echo out the original key with $ instead of \n (meaning, wherever there’s a newline character, give me $). This can be done with cat -e PRIVATE_GPG_KEY.asc
# 2) Copy the output into a text editor, and replace all occurences of $ with \n ("\n" should be there instead of line breaks)
# 3) Insert that whole string into an environment variable in CircleCI.
#
echo "Saving to .asc ..." &&
echo "$KEYSTORE_SECRET" >./app/release.asc &&
echo "Decrypting .asc to .jks ..." &&
gpg -d --passphrase "$KEYSTORE_SECRET_PASSPHRASE" --batch ./app/release.asc >./app/release.jks &&
rm ./app/release.asc
#! /bin/sh
#Token from Dropbox Settings.
#DROPBOX_TOKEN="add this token to Circle CI Environment Variables in the project settings"
APK_FILES="./app/build/outputs/apk/*/*/*-release.apk"
for FILE in $APK_FILES
do
#check file is exist and not empty
if [ -f "${FILE}" ] && [ -s "${FILE}" ]
then
echo "Found APK: ${FILE}"
FILE_NAME=$(basename "${FILE}")
curl -X POST https://content.dropboxapi.com/2/files/upload \
--header "Authorization: Bearer ${DROPBOX_TOKEN}" \
--header "Dropbox-API-Arg: {\"path\": \"\/${FILE_NAME}\",\"mode\": \"overwrite\",\"autorename\": true,\"mute\": false}" \
--header "Content-Type: application/octet-stream" \
--data-binary @"${FILE}"
else
echo "File ${FILE} is empty"; exit 1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment