image: node:16-alpine
stages:
  - setup
  - test
  - build
  - dockerize


before_script:
- apk add --update git
- apk add --update nodejs npm
- npm i -g yarn

install-dependencies:
  stage: setup
  interruptible: true
  only:
    - main
    - merge_requests
  cache:
    key:
      files:
        - yarn.lock
    paths:
      - node_modules
      - .yarn
  script:
    - apk --no-cache add git
    - yarn install --pure-lockfile --cache-folder .yarn
  artifacts:
    paths:
      - node_modules

.distributed:
  interruptible: true
  only:
    - main
    - merge_requests
  needs:
    - install-dependencies
  artifacts:
    paths:
      - node_modules/.cache/nx

test:
  stage: test
  extends: .distributed
  script:
    - yarn nx affected --base=HEAD~1 --target=test --parallel --max-parallel=4

build:
  stage: build
  only:
    - main
  needs:
    - install-dependencies
  artifacts:
    paths:
      - node_modules/.cache/nx
      - dist/apps/web-graphpolaris
  script:
    # - yarn nx affected --base=HEAD~1 --target=build --parallel --max-parallel=3
    # only build web-graphpolaris
    - yarn nx build web-graphpolaris --prod

build-docker:
  image: docker:stable
  stage: dockerize
  tags:
    - docker
  only:
    - main
  script:
    - docker build --progress plain -t $CI_PROJECT_NAME-webserver-service:latest .
  # after_script:
  #   - docker login datastropheregistry.azurecr.io -u $REGISTRY_USERNAME -p $REGISTRY_PASSWORD
  #   - if [[ ! -z $CI_COMMIT_BRANCH+x ]]; then DOCKER_TAG=$CI_COMMIT_BRANCH; else DOCKER_TAG=$CI_MERGE_REQUEST_TARGET_BRANCH_NAME; fi
  #   - docker tag $CI_PROJECT_NAME-webserver-service datastropheregistry.azurecr.io/$CI_PROJECT_NAME-webserver-service:$DOCKER_TAG
  #   - docker push datastropheregistry.azurecr.io/$CI_PROJECT_NAME-webserver-service:$DOCKER_TAG
  dependencies:
    - build
