name: Build app

on:
  push:
    branches: 
      - master

jobs:
  build-Ace-Code:
    runs-on: ubuntu-latest
    steps:
     - uses: actions/checkout@v3
     - uses: actions/setup-node@v3
       with:
         node-version: 16
     
     - run: sudo npm install --save-dev webpack webpack-cli
     - run: cd www && mkdir dist && cd ..
     - run: npm run build
     - run: ls
     - run: sudo npm install -g cordova@11.0.0 
     - run: sudo cordova platform add android
     - run: ls
     - run: sudo cordova plugin add cordova-plugin-file

     - uses: actions/setup-java@v3
       with:
         distribution: temurin
         java-version: 11
     
     - uses: gradle/gradle-build-action@v2.4.2
     - name: Setup sdk
       uses: android-actions/setup-android@v2

     - run: sh clean.sh
     - run: |
         cd platforms
         sudo cordova build android
         ls -a
         ls -lh android/app/build/outputs/apk/debug/
         cd android/app/build/outputs/apk/debug/
         sudo mv app-debug.apk /home/runner/work/Ace-Code/Ace-Code

     - uses: actions/upload-artifact@v3
       with:
         name: upload_debug_apk
         path: /home/runner/work/Ace-Code/Ace-Code/app-debug.apk
         retention-days: 7
     
     - run: ls
  
  release:
     name: Release apk
     needs: build-Ace-Code
     runs-on: ubuntu-latest
     steps:
       - name: Download apk from build
         uses: actions/download-artifact@v1
         with:
           name: upload_debug_apk

       - name: Create Release
         id: create_release
         uses: actions/create-release@v1
         env:
           GITHUB_TOKEN: ${{ secrets.TK }}
         with:
           tag_name: ${{ github.run_number }}
           release_name: ${{ github.event.repository.name }} v4.0.6

       - name: upload Release Apk
         id: upload_release_assets
         uses: actions/upload-release-asset@v1.0.1
         env:
            GITHUB_TOKEN: ${{ secrets.TK }}
         with:
           upload_url: ${{ steps.create_release.outputs.upload_url }}
           asset_path:  upload_debug_apk/app-debug.apk
           asset_name: ${{ github.event.repository.name }}.apk
           asset_content_type: application/zip