name: CI

on:
  push:
    branches:
      - '*'
  pull_request:
    branches: [ main ]

jobs:

## Build Back ##

  Build_Back:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    defaults:
      run:
        working-directory: ./back
    environment: Staging

    steps:
      - uses: actions/checkout@v3

      - name: Build Docker
        run: docker build -t testback .

## Build App ##

  Build_Front:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./front
    environment: Staging

    steps:
      - uses: actions/checkout@v3

      - name: Install Yarn
        run: npm install -g yarn

      - name: Install dependencies
        run: yarn install

      - name: 🏗 Setup Expo
        uses: expo/expo-github-action@v7
        with:
          expo-version: latest
          eas-version: 3.3.1
          token: ${{ secrets.EXPO_TOKEN }}

      - name: Build Android APK
        run: |
          eas build -p android --profile debug --local --non-interactive
          mv *.apk chromacase.apk

      - name: Upload Artifact
        if: github.ref == 'refs/heads/main'
        uses: actions/upload-artifact@v3
        with:
          name: chromacase.apk
          path: front/

## Test Backend ##

  Test_Back:
    runs-on: ubuntu-latest
    timeout-minutes: 15
    needs: [ Build_Back ]
    environment: Staging

    steps:
      - uses: actions/checkout@v3
        with:
          submodules: recursive
          fetch-depth: 0

      - name: Copy env file to github secret env file
        run: |
          touch .env
          echo "POSTGRES_USER=user" >> .env
          echo "POSTGRES_PASSWORD=eip" >> .env
          echo "POSTGRES_NAME=chromacase" >> .env
          echo "POSTGRES_HOST=db" >> .env
          echo "DATABASE_URL=postgresql://user:eip@db:5432/chromacase" >> .env
          echo "JWT_SECRET=wow" >> .env
          echo "POSTGRES_DB=chromacase" >> .env
          echo "API_URL=http://localhost:80/api" >> .env

      - name: Start the service
        run: docker-compose up -d back db

      - name: Perform healthchecks
        run: |
          docker-compose ps -a
          wget --retry-connrefused http://localhost:3000 # /healthcheck

      - name: Run scorometer tests
        run: |
          pip install -r scorometer/requirements.txt
          cd scorometer/tests && ./runner.sh

      - name: Run robot tests
        run: |
          pip install -r back/test/robot/requirements.txt
          robot -d out back/test/robot/
      - uses: actions/upload-artifact@v3
        if: always()
        with:
          name: results
          path: out

      - name: Write results to Pull Request and Summary
        if: always() && github.event_name == 'pull_request'
        uses: joonvena/robotframework-reporter-action@v2.1
        with:
          report_path: out/
          gh_access_token: ${{ secrets.GITHUB_TOKEN }}
          only_summary: false

      - name: Write results to Summary
        if: always() && github.event_name != 'pull_request'
        uses: joonvena/robotframework-reporter-action@v2.1
        with:
          report_path: out/
          gh_access_token: ${{ secrets.GITHUB_TOKEN }}
          only_summary: true

      - name: Remove .env && stop the service
        run: docker-compose down && rm .env

## Test App ##

## Deployement ##

  Deployement_Docker:
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    needs: [ Test_Back ]
    environment: Production

    steps:
      - uses: actions/checkout@v2

      - name: Docker meta back
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: ghcr.io/${{github.repository_owner}}/back
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v2
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      -
        name: Login to ghcr
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{github.repository_owner}}
          password: ${{secrets.GITHUB_TOKEN}}
      -
        name: Build and push
        uses: docker/build-push-action@v3
        with:
          context: ./back
          push: true
          tags: ${{steps.meta.outputs.tags}}
      - name: Docker meta front
        id: meta_front
        uses: docker/metadata-action@v4
        with:
          images: ghcr.io/${{github.repository_owner}}/front
      -
        name: Build and push front
        uses: docker/build-push-action@v3
        with:
          context: ./front
          push: true
          tags: ${{steps.meta_front.outputs.tags}}
          build-args: |
            API_URL=${{secrets.API_URL}}
            SCORO_URL=${{secrets.SCORO_URL}}
      - name: Docker meta scorometer
        id: meta_scorometer
        uses: docker/metadata-action@v4
        with:
          images: ghcr.io/${{github.repository_owner}}/scorometer
      -
        name: Build and push scorometer
        uses: docker/build-push-action@v3
        with:
          context: ./scorometer
          push: true
          tags: ${{steps.meta_scorometer.outputs.tags}}

