name: build_nightly

on:
  push:
    branches:
      - develop
  workflow_dispatch:
defaults:
  run:
    working-directory: packages/stream_chat_v1

jobs:
  cleanup-runs:
    runs-on: ubuntu-latest
    steps:
    - uses: rokroskar/workflow-run-cleanup-action@master
      env:
        GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
  build_and_deploy_ios:
    runs-on: [macos-latest]
    steps:
      - uses: actions/checkout@v2
      - name: Install RubyGems
        run: |
          cd ios
          bundle install
      - uses: subosito/flutter-action@v1.4.0
        with:
          channel: 'stable'
      - name: Install firebase-tools
        run: npm install -g firebase-tools
      - name: Flutter setup
        run: |
          flutter pub get
      - name: Build and release
        env:
          MATCH_PASSWORD: ${{ secrets.MATCH_PASSWORD }}
          MATCH_GIT_BASIC_AUTHORIZATION: ${{ secrets.MATCH_GIT_BASIC_AUTHORIZATION }}
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
        run: |
          flutter build ios --release --no-codesign
          cd ios
          bundle exec fastlane deploy_to_firebase
  build_and_deploy_android:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-java@v1
        with:
          java-version: '12.x'
      - uses: subosito/flutter-action@v1.4.0
        with:
          channel: 'stable'
      - run: |
          flutter pub get
      - name: Build
        run: |
          flutter build apk
      - name: Deploy
        uses: wzieba/Firebase-Distribution-Github-Action@v1.3.2
        with:
          appId: ${{secrets.FIREBASE_ANDROID_APPID}}
          token: ${{secrets.FIREBASE_TOKEN}}
          groups: stream-testers
          debug: true
          file: packages/stream_chat_v1/build/app/outputs/apk/release/app-release.apk
      - name: upload apk
        uses: actions/upload-artifact@v2
        with:
          name: android-stream-chat-v1
          path: packages/stream_chat_v1/build/app/outputs/apk/release/app-release.apk
  build_and_deploy_web:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: config git
        run: |
          git config --global user.email "$(git log --format='%ae' HEAD^!)"
          git config --global user.name "$(git log --format='%an' HEAD^!)"
          git fetch origin gh-pages:gh-pages
      - uses: subosito/flutter-action@v1.4.0
        with:
          channel: 'stable'
      - run: flutter pub get
      - name: Copy production config
        run: echo "${{ secrets.PRODUCTION_CONFIG }}" > lib/app_config.dart
      - uses: erickzanardo/flutter-gh-pages@v3
        with:
          webRenderer: canvaskit
          workingDir: packages/stream_chat_v1


