image: node:20-slim

stages:
  - install
  - lint_and_format
  - build
  - test
  - security_scan
  - deploy

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - node_modules/

install:
  stage: install
  script:
    - npm install
  artifacts:
    paths:
      - node_modules/

format-code:
  stage: lint_and_format
  script:
    - npm run format-test

lint-code:
  stage: lint_and_format
  script:
    - npm run lint

type-check:
  stage: build
  script:
    - npm run type-check
    - npm run build

unit-tests:
  stage: test
  script:
    - npm run test:unit

test:e2e:
  image: cypress/browsers:node16.14.2-slim-chrome100-ff99-edge
  stage: test
  script:
    - npm ci --cache .npm --prefer-offline
    - npx cypress verify
    - npm run test:e2e
  dependencies:
    - install

test-coverage:
  stage: test
  script:
    - npm run test:coverage

include:
  - template: SAST.gitlab-ci.yml

sast:
  stage: security_scan
  script:
    - echo "Running SAST..."

deploy:
  stage: deploy
  image: docker:latest
  tags:
    - dind
  services:
    - name: docker:dind
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_IMAGE_NAME: $CI_REGISTRY_USER/idatt2106_2024_02_frontend-app
    DOCKER_TLS_CERTDIR: ""
    DOCKER_HOST: tcp://docker:2375
  script:
    - echo "$CI_REGISTRY_PASSWORD" | docker login --username $CI_REGISTRY_USER --password-stdin
    - docker build -t $DOCKER_IMAGE_NAME:latest .
    - docker push $DOCKER_IMAGE_NAME:latest
