stages:
  - download-models
  - test-and-build
  - build-docker
  - deploy

cache:
    key: one-key-to-rule-them-all
    paths:
      - emtest/target
      - venv

download-models:
  stage: download-models
  tags:
    - derby
  before_script:
    - source scripts/dev-setup
  script:
    - make download-models
  artifacts:
    paths:
      - detection/model
  except:
    - main
  cache:
    key: models
    paths:
      - detection/model

test:
  stage: test-and-build
  except:
    - main
  tags:
    - derby
  before_script:
    - source scripts/dev-setup
  script:
    - make opencv -j$(nproc --ignore=2)
    - make -C alg qrtool -j$(nproc --ignore=2)
    - make test
  dependencies:
    - download-models

build-alg:
  stage: test-and-build
  except:
    - main
  tags:
    - derby
  script:
    - make opencv -j$(nproc --ignore=2)
    - make build/alg/qrtool -j$(nproc --ignore=2)
  artifacts:
    paths:
      - build
      - alg/qrtool

build-web:
  stage: test-and-build
  except:
    - main
  tags:
    - derby
  before_script:
    - (cd web; npm install)
  script:
    - make web
  artifacts:
    paths:
      - build

build-docker:
  stage: build-docker
  except:
    - main
  tags:
    - derby
  script:
    - make docker-build
    - make docker-push
  dependencies:
    - build-web
    - build-alg
    - download-models
  except:
    - main

deploy-dev:
  stage: deploy
  except:
    - main
  tags:
    - derby
  only:
    - dev
  script:
    - make deploy-api-dev
  cache: []
  dependencies: []

dev-smoke:
  stage: test-and-build
  except:
    - main
  tags:
    - derby
  allow_failure: true
  script:
    - ./scripts/emcli --env dev activate 0074253255108
    - ./api/scripts/api_smoke.py -p $EMBLEM_CI_PASSWORD
  cache: []

deploy-prod:
  stage: deploy
  tags:
    - derby
  only:
    - main
  script:
    - make deploy-api-prod
  cache: []
  dependencies: []

deploy-roi-worker:
  tags:
    - emblem-s1
  stage: deploy
  when: manual
  script:
    - make deploy-roi-worker
  cache: []
