Skip to content

Merge pull request #623 from Countly/sbs-fix #1629

Merge pull request #623 from Countly/sbs-fix

Merge pull request #623 from Countly/sbs-fix #1629

name: The Countly Android SDK Unit Test
env:
EMULATOR_REPO: us-docker.pkg.dev/android-emulator-268719/images/28-playstore-x64:30.1.2
JAVA_V: 17
JAVA_DIST: corretto
# Every Gradle call gets -Pcoverage, so the connected tests run against the jacoco-instrumented debug SDK.
ORG_GRADLE_PROJECT_coverage: true
permissions:
checks: write
pull-requests: write
on:
push:
branches:
- master
- staging
- staging-nw
pull_request:
branches:
- master
- staging
- staging-nw
jobs:
setup:
runs-on: ubuntu-latest
name: Build and Test the SDK
steps:
- name: Install Docker to the Runner
run: sudo apt-get install containerd.io
- name: Pull Emulator from the Repo
run: docker pull ${{ env.EMULATOR_REPO }}
- name: Checkout SDK code
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
with:
persist-credentials: false
- name: Setup Java
uses: actions/setup-java@e9fbacdec3bb3b6036605a3e6f7995d66773a8c6 # v3.14.2
with:
java-version: ${{ env.JAVA_V }}
distribution: ${{ env.JAVA_DIST }}
- name: Setup Android SDK
uses: android-actions/setup-android@9fc6c4e9069bf8d3d10b2204b1fb8f6ef7065407 # v3.2.2
with:
# The action's default is "tools platform-tools", but Google has removed the
# obsolete "tools" package from the SDK repository, so the default now fails.
packages: platform-tools
- name: Start ADB Server
run: adb start-server
- name: Run Android Emulator Container
run: docker run -d -e "ADBKEY=$(cat ~/.android/adbkey)" --device /dev/kvm --publish 5555:5555/tcp ${{ env.EMULATOR_REPO }}
- name: Connect to the Emulator
run: adb connect localhost:5555
- name: Set AAPT2 for android 35
run: echo "android.aapt2FromMavenOverride=/usr/local/lib/android/sdk/build-tools/35.0.0/aapt2" | tee -a ${{ github.workspace }}/gradle.properties
- name: Build the SDK
if: always()
run: ./gradlew sdk:build
# The native module ships prebuilt .so files that no Gradle task recompiles. Its unit tests
# read those committed binaries, so this is the only thing standing between a bad manual
# rebuild and a release. Runs on the JVM, no emulator needed.
- name: Build and Test the Native SDK
if: always()
run: ./gradlew sdk-native:build
- name: Build and Test the Symbol Upload Plugin
if: always()
run: ./gradlew upload-plugin:build
- name: Run the Android Unit Tests
if: always()
run: ./gradlew sdk:connectedAndroidTest
- name: Publish Test Results to PR
# the composite linux variant runs from the pinned source; the root action pulls a mutable Docker tag
uses: EnricoMi/publish-unit-test-result-action/linux@d0a4676d0e0b938bc201470d88276b7c74c712b3 # v2.24.0
if: always()
with:
check_name: "Unit Test Results :rocket:"
comment_title: "Unit Test Results :rocket:"
files: |
sdk/build/outputs/androidTest-results/**/*.xml
sdk-native/build/test-results/**/*.xml
upload-plugin/build/test-results/**/*.xml
# Runs after failed tests too: the coverage the emulator wrote is still valid. The test tasks are
# excluded so a failure is not re-run. No .ec file means the instrumentation process died before
# writing one, and a report built from nothing would reach Codacy as 0%.
- name: Generate Coverage Reports
id: coverage
if: always()
env:
CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }}
run: |
if [ -z "$CODACY_PROJECT_TOKEN" ]; then
echo "No CODACY_PROJECT_TOKEN on this run (fork or Dependabot), skipping coverage."
exit 0
fi
if ! ls sdk/build/outputs/code_coverage/debugAndroidTest/connected/*/*.ec > /dev/null 2>&1; then
echo "::warning::The emulator produced no coverage data, so nothing is uploaded to Codacy."
exit 0
fi
./gradlew sdk:createDebugAndroidTestCoverageReport -x sdk:connectedDebugAndroidTest \
upload-plugin:jacocoTestReport -x upload-plugin:test
reports=$(ls sdk/build/reports/coverage/androidTest/debug/connected/report.xml \
upload-plugin/build/reports/jacoco/test/jacocoTestReport.xml 2> /dev/null | paste -sd, -)
echo "reports=$reports" >> "$GITHUB_OUTPUT"
# a pinned, checksum-verified reporter binary, and best effort, so a Codacy outage does not fail this build
- name: Upload Coverage to Codacy
if: always() && steps.coverage.outputs.reports != ''
continue-on-error: true
env:
CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }}
CODACY_REPORTER_VERSION: 14.1.3
CODACY_REPORTER_SHA512: 73a6c93fc5db509fd0423d6e545759c3f61d8360ee12054e32a9277daab6add0da6c6639248f6ed6c0042366440293ebc0b6144325aef52c5d3d4b230d65eb17
COVERAGE_REPORTS: ${{ steps.coverage.outputs.reports }}
run: |
curl -fsSL -o codacy-coverage-reporter "https://github.com/codacy/codacy-coverage-reporter/releases/download/${CODACY_REPORTER_VERSION}/codacy-coverage-reporter-linux"
echo "${CODACY_REPORTER_SHA512} codacy-coverage-reporter" | sha512sum -c -
chmod +x codacy-coverage-reporter
report_params=()
IFS=',' read -ra reports <<< "$COVERAGE_REPORTS"
for report in "${reports[@]}"; do
if [ -n "$report" ]; then
report_params+=(-r "$report")
fi
done
./codacy-coverage-reporter report --project-token "$CODACY_PROJECT_TOKEN" "${report_params[@]}" --partial
./codacy-coverage-reporter final --project-token "$CODACY_PROJECT_TOKEN"