stages:
  - backend build
  - frontend build
  - backend test
  - frontend test
  - deploy

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - .m2/repository/
    - target/
    - .yarn
    - android-sdk/
    - .gradle/wrapper
    - .gradle/caches

variables:
  # Specify the SDK tools version and build tools version to use
  ANDROID_COMPILE_SDK: 33
  ANDROID_BUILD_TOOLS: 33.0.0
  ANDROID_SDK_TOOLS: 7583922
  ANDROID_HOME: "/usr/local/android-sdk"
  GIT_STRATEGY: clone


Prettier:
  image: node:16.10.0
  stage: backend test
  needs: []
  script:
    - cd backend
    - yarn
    - yarn prettier --check .
  retry: 1

Typescript-compile:
  image: node:16.10.0
  stage: backend build
  needs: []
  script:
      - cd backend
      - yarn
      - yarn tsc

API-test:
  image: cypress/base:14.17.0
  stage: backend test
  needs: [Typescript-compile]
  variables:
    DISPLAY: ":99"
  script:
      - apt-get update && apt-get install -y xvfb
      - Xvfb :99 -screen 0 1920x1080x24 &
      - cd backend
      - echo "FB_PRIV_KEY_JSON_ENCODED=$FB_PRIV_KEY_JSON_ENCODED" >> .env
      # - echo "export FB_PRIV_KEY_JSON_ENCODED=$FB_PRIV_KEY_JSON_ENCODED"
      - node keys/decode.js
      - chmod 400 keys/fb-key.json
      - yarn
      - yarn start:local & sleep 10
      - yarn test
  retry: 1
  artifacts:
    paths:
      - cypress/videos/*.mp4
    expire_in: 1 week # Set the artifact expiration time according to your needs
  only:
    refs:
      - main




Gradle-build:
  image: gradle:7.5.0-jdk11
  stage: frontend test
  needs: []
  script:
      # Restore Android SDK from cache
  #- if [ -d android-sdk ]; then mv android-sdk/* $ANDROID_HOME/; fi
  # Download and install Android SDK
  - wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/commandlinetools-linux-${ANDROID_SDK_TOOLS}_latest.zip
  - unzip -q -o android-sdk.zip -d android-sdk
  - rm -rf $ANDROID_HOME/platforms/android-33
  - echo y | android-sdk/cmdline-tools/bin/sdkmanager --sdk_root=$ANDROID_HOME "platforms;android-${ANDROID_COMPILE_SDK}" "build-tools;${ANDROID_BUILD_TOOLS}"
  - cd frontend
  - ./gradlew clean
  - ./gradlew build --refresh-dependencies
  - gradle build

Backend (NTNU-VM):
  image: node:16.10.0
  stage: deploy
  needs: [Typescript-compile]
  before_script:
    - mkdir -p ~/.ssh
    - echo "$NTNU_VM_SSH_KEY" | tr -d '\r' > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
    - ssh-keyscan 10.212.26.72 >> ~/.ssh/known_hosts
    - ssh-keygen -p -f ~/.ssh/id_rsa

  script:
    - ssh -v -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ubuntu@10.212.26.72 -p 22 "bash /home/git/tdt4240-tank-wars/backend/vm.sh </dev/null >/dev/null 2>&1"
    
  only:
    refs:
      - main
