name: Android CI

on:
  push:
  pull_request_target:
    branches: [ master ]

jobs:

  ci-build:
    runs-on: ubuntu-latest
    steps:

    - name: Checkout code
      uses: actions/checkout@v2
      with:
        submodules: 'recursive'

    - name: Load secrets
      run: |
        rm ./app/src/main/res/values/mapbox-token.xml
        echo -e "<resources>\n    <string name=\"mapbox_access_token\">$MAPBOXTOKEN</string>\n</resources>" > ./app/src/main/res/values/mapbox-token.xml
        mkdir -p ~/.gradle
        echo "MAPBOX_DOWNLOADS_TOKEN=$MAPBOXTOKEN" >>~/.gradle/gradle.properties
      env:
        MAPBOXTOKEN: ${{ secrets.MAPBOXTOKEN }}

    - name: Mock files for CI
      run: |
        rm ./app/google-services.json
        cp ./app/google-services-example.json ./app/google-services.json
        rm ./app/src/main/res/values/curfirmwareversion.xml
        cp ./app/special/curfirmwareversion.xml ./app/src/main/res/values/

    - name: Set up JDK 11
      uses: actions/setup-java@v2
      with:
        java-version: 11
        distribution: 'adopt'
        cache: gradle
        
    - name: Validate Gradle wrapper
      uses: gradle/wrapper-validation-action@v1
      
    - name: Integration test
      run: ./gradlew test --stacktrace

    - name: Build debug APK
      run: ./gradlew assembleDebug

    - name: Upload APK
      uses: actions/upload-artifact@v2
      with:
        name: app-debug
        path: app/build/outputs/apk/debug/app-debug.apk
        retention-days: 30
