image: "k8r.eu/justjanne/android-sdk:be7526df"

cache:
  key: "$CI_PROJECT_NAME"
  paths:
    - ".gradle/caches"

before_script:
  - "mkdir -p $(pwd)/.gradle"
  - "export GRADLE_USER_HOME=$(pwd)/.gradle"
  - "echo $SIGNING_KEYSTORE | base64 -d > /root/signing.keystore"
  - "echo $SIGNING_PROPERTIES | base64 -d > signing.properties"
  - "chmod +x ./gradlew"

stages:
  - "test"
  - "release"

test:
  stage: "test"
  script:
    - "./gradlew assembleRelease -x lintRelease -x lintVitalRelease"
    - "cp app/build/outputs/apk/release/*.apk ."
    - "./gradlew check -x connectedCheck --stacktrace"
  artifacts:
    paths:
      - "*.apk"
      - "*/build/test-results/**/TEST-*.xml"
      - "*/build/reports/*.xml"
    reports:
      junit:
        - "*/build/test-results/**/TEST-*.xml"
        - "*/build/reports/*.xml"

version:
  stage: "test"
  script:
    - "export VERSION_NAME=$(git describe --abbrev=0 --tags HEAD)"
    - "export VERSION_CODE=$(git rev-list --count $VERSION_NAME)"
    - "echo \"{\\\"name\\\":\\\"$VERSION_NAME\\\",\\\"code\\\":$VERSION_CODE}\" > version.json"
  artifacts:
    paths:
      - "version.json"

deploy-local:
  stage: "release"
  image: "k8r.eu/justjanne/docker-s3cmd:latest"
  cache: { }
  dependencies:
    - "test"
    - "version"
  script:
    - "echo $S3_CONFIG | base64 -d > $HOME/.s3cfg"
    - "export VERSION=$(ls *.apk)"
    - "s3cmd put $VERSION s3://releases/quasseldroid-ng/$VERSION"
    - "s3cmd put version.json s3://releases/quasseldroid-ng/version.json"
    - "s3cmd cp s3://releases/quasseldroid-ng/$VERSION s3://releases/quasseldroid-ng/Quasseldroid-latest.apk"
  rules:
    - if: "$S3_CONFIG != ''"
      when: on_success

deploy-beta:
  stage: "release"
  image: "k8r.eu/justjanne/docker-fastlane:latest"
  cache: { }
  dependencies:
    - "test"
  script:
    - "echo $FASTLANE_CONFIG | base64 -d > $HOME/key.json"
    - "export VERSION=$(ls *.apk)"
    - "fastlane supply --apk $VERSION --track beta --json_key $HOME/key.json --package_name com.iskrembilen.quasseldroid --skip_upload_metadata=true --skip_upload_images=true --skip_upload_screenshots=true"
  rules:
    - if: "$FASTLANE_CONFIG != ''"
      when: on_success
