image: registry.gitlab.com/fdroid/fdroidserver:buildserver-bullseye

variables:
  GIT_SUBMODULE_STRATEGY: recursive
  GIT_SUBMODULE_FORCE_HTTPS: "true"

before_script:
  - export GRADLE_USER_HOME=`pwd`/.gradle

.base:
  before_script:
    - test -e /etc/apt/sources.list.d/bullseye-backports.list
      || echo "deb http://deb.debian.org/debian bullseye-backports main" >> /etc/apt/sources.list
    - apt update
    - apt-get -qy install -t bullseye-backports --no-install-recommends git sdkmanager

    - test -n "$ANDROID_HOME" || source /etc/profile.d/bsenv.sh

    - export cmdline_tools_latest="$ANDROID_HOME/cmdline-tools/latest/bin"
    - test -e $cmdline_tools_latest && export PATH="$cmdline_tools_latest:$PATH"

    - export GRADLE_USER_HOME=$PWD/.gradle
    - export ANDROID_COMPILE_SDK=`sed -n 's,.*compileSdk\s*\([0-9][0-9]*\).*,\1,p' app/build.gradle`
    - echo y | sdkmanager "platforms;android-${ANDROID_COMPILE_SDK}" > /dev/null

    - apt-get update || apt-get update
    - apt-get install -y openjdk-17-jdk-headless
    - update-java-alternatives -s java-1.17.0-openjdk-amd64
  after_script:
    # this file changes every time but should not be cached
    - rm -f $GRADLE_USER_HOME/caches/modules-2/modules-2.lock
    - rm -fr $GRADLE_USER_HOME/caches/*/plugin-resolution/
  cache:
    paths:
      - .gradle/wrapper
      - .gradle/caches


# Basic android and gradle stuff
# Check linting
lintDebug:
  extends: .base
  interruptible: true
  stage: build
  script:
    - ./gradlew -Pci --console=plain :app:lintDebug -PbuildDir=lint --write-verification-metadata sha256
    - git diff --quiet gradle/verification-metadata.xml || (echo 'Verification of dependencies failed!' && exit 1)
  artifacts:
    when: on_failure
    paths:
      - gradle/verification-metadata.xml


# Make Project
assembleDebug:
  extends: .base
  interruptible: true
  stage: build
  tags:
    - server_artectrex
  script:
    - ./gradlew assembleDebug
  artifacts:
    paths:
      - app/build/outputs/

# Run all tests, if any fails, interrupt the pipeline (fail it)
debugTests:
  extends: .base
  interruptible: true
  stage: test
  script:
    - ./gradlew -Pci --console=plain :app:testDebug -x lint

.connected-template: &connected-template
  extends: .base
  interruptible: true
  allow_failure: true
  image: briar/ci-image-android-emulator:latest
  stage: test
  script:
    - export JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"
    - ./gradlew assembleStaging
    - export AVD_SDK=`echo $CI_JOB_NAME | awk '{print $2}'`
    - export AVD_TAG=`echo $CI_JOB_NAME | awk '{print $3}'`
    - export AVD_ARCH=`echo $CI_JOB_NAME | awk '{print $4}'`
    - export AVD_PACKAGE="system-images;android-${AVD_SDK};${AVD_TAG};${AVD_ARCH}"
    - echo $AVD_PACKAGE
    - $ANDROID_HOME/cmdline-tools/latest/bin/avdmanager --verbose delete avd --name "$NAME_AVD"
    - export AVD="$AVD_PACKAGE"
    - echo y | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager --install "$AVD"
    - echo no | $ANDROID_HOME/cmdline-tools/latest/bin/avdmanager --verbose create avd --name "$NAME_AVD" --package "$AVD" --device "pixel"
    - start-emulator.sh
    - ./gradlew installStaging
    - adb shell am start -n org.pixeldroid.app.debug/org.pixeldroid.app.MainActivity
    - if [ $AVD_SDK -lt 25 ] || ! emulator -accel-check; then
      export FLAG=-Pandroid.testInstrumentationRunnerArguments.notAnnotation=androidx.test.filters.LargeTest;
      fi
    - ./gradlew connectedStagingAndroidTest $FLAG --info || (adb -e logcat -d > logcat.txt; exit 1)
  artifacts:
    paths:
      - logcat.txt

connected 27 default x86_64:
  <<: *connected-template


#inspired from https://gitlab.com/mvglasow/satstat/-/blob/master/.gitlab-ci.yml
fdroid build:
  stage: build
  image: registry.gitlab.com/mvglasow/ci-images-server:latest
  tags:
    - server_artectrex
  allow_failure: true
  artifacts:
    # name: "${CI_PROJECT_PATH}_${CI_JOB_STAGE}_${CI_COMMIT_REF_NAME}_${CI_COMMIT_SHA}"
    paths:
      - signed/
    when: always
  only:
    - tags
  cache:
    key: "$CI_JOB_NAME"
    paths:
      - .android
      - .gradle
  script:
      # Put the correct versionName and versionCode in the .fdroid.yml
    - sed -e "s/\${versionName}/$(grep "versionName " app/build.gradle | awk '{print $2}' | tr -d \")$(grep "versionCode" app/build.gradle -m 1 | awk '{print $2}')/" -e "s/\${versionCode}/$(grep "versionCode" app/build.gradle -m 1 | awk '{print $2}')/" .fdroid.yml.template > .fdroid.yml
    - rm .fdroid.yml.template
    # each `fdroid build --on-server` run expects sudo, then uninstalls it
    - set -x
    - apt-get install sudo
    - fdroid fetchsrclibs --verbose
    # this builds the latest version of the app from its source dir, using the build recipe in .fdroid.yml
    - fdroid build --verbose --on-server --no-tarball
    # create a keystore if we don’t have one
    - ls .android || mkdir .android
    - ls .android/debug.keystore || keytool -genkey -v -keystore .android/debug.keystore -storepass android -alias androiddebugkey -keypass android -keyalg RSA -keysize 2048 -validity 10000 -dname 'C=US, O=Android, CN=Android Debug'
    # sign the apk
    - cp -R unsigned signed
    - jarsigner -verbose -keystore .android/debug.keystore -storepass android -keypass android signed/*.apk androiddebugkey
