name: CI

on:
  push:
    branches:
      - main
      - renovate/**

  pull_request:
    branches:
      - main

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout repository
      uses: actions/checkout@v3.0.2

    - name: Set up JDK
      uses: actions/setup-java@v3.4.1
      with:
        distribution: temurin
        java-version: 18

    - name: Run unit tests
      uses: gradle/gradle-build-action@v2.2.2
      with:
        arguments: check --stacktrace --no-configuration-cache

    - name: (Fail-only) Upload test report
      if: failure()
      uses: actions/upload-artifact@v3.1.0
      with:
          name: Test report
          path: android/build/reports

  deploy-release-snapshot:
    runs-on: ubuntu-latest
    if: "github.event_name == 'push' && github.event.ref == 'refs/heads/main'"
    needs: [ "check" ]
    steps:
    - name: Checkout repository
      uses: actions/checkout@v3.0.2

    - name: Set up JDK
      uses: actions/setup-java@v3.4.1
      with:
        distribution: temurin
        java-version: 18

    - name: Decrypt secrets
      run: scripts/signing-setup.sh "$ENCRYPT_KEY"
      env:
        ENCRYPT_KEY: ${{ secrets.ENCRYPT_KEY }}

    - name: Build release app
      uses: gradle/gradle-build-action@v2.2.2
      with:
        arguments: collectReleaseApks --no-configuration-cache

    - name: Clean secrets
      run: scripts/signing-cleanup.sh

    - name: Deploy snapshot
      run: scripts/deploy-snapshot.sh
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
