---
#######################
# SPT DOCKER PIPELINE #
#######################

kind: pipeline
type: docker
name: Verify Code

concurrency:
  limit: 1

clone:
  depth: 3

trigger:
  event:
    - pull_request

steps:
  - name: Install
    image: registry.sp-tarkov.com/spt/pnpm:7.25.1
    volumes:
      - name: cache
        path: /drone/src/.pnpm-store/v3
    commands:
      - cd ./project
      - pnpm install

  - name: Run Biome
    image: registry.sp-tarkov.com/spt/pnpm:7.25.1
    commands:
      - cd ./project
      - pnpm run lint
      # - npm run check:circular

  # - name: Jest coverage
  #   image: registry.sp-tarkov.com/spt/pnpm:7.25.1
  #   commands:
  #     - cd ./project
  #     - pnpm run test:coverage

  - name: Check that Build is not broken
    image: registry.sp-tarkov.com/spt/node-wine:16
    commands:
      - cd ./project
      - npm run build:release

volumes:
  - name: cache
    host:
      path: /tmp/spt-cache/pnpm_store
image_pull_secrets:
  - dockerconfig
---
#######################
# SPT SONAR PIPELINE  #
#######################

kind: pipeline
type: docker
name: Sonar checks on branch

concurrency:
  limit: 1

clone:
  depth: 3

trigger:
  event:
    - push
    - tag
  branch:
    - master

steps:
  - name: Install
    image: registry.sp-tarkov.com/spt/pnpm:7.25.1
    volumes:
      - name: cache
        path: /drone/src/.pnpm-store/v3
    commands:
      - cd ./project
      - pnpm install

  - name: Run Biome
    image: registry.sp-tarkov.com/spt/pnpm:7.25.1
    commands:
      - cd ./project
      - pnpm run lint
      # - npm run check:circular
    depends_on:
      - Install
    when:
      event:
        - push

  # - name: Jest coverage
  #   image: registry.sp-tarkov.com/spt/pnpm:7.25.1
  #   commands:
  #     - cd ./project
  #     - pnpm run test:coverage
  #   depends_on:
  #     - Run Biome
  #   when:
  #     event:
  #       - push

  - name: Run Sonar scanner on push
    image: sonarsource/sonar-scanner-cli:4
    environment:
      SONAR_PROJECT:
        from_secret: SONAR_PROJECT
      SONAR_URL:
        from_secret: SONAR_URL
      SONAR_TOKEN:
        from_secret: SONAR_TOKEN
    commands:
      - >
        sonar-scanner
        -Dsonar.projectKey=$SONAR_PROJECT
        -Dsonar.sources="./project/src"
        -Dsonar.language="ts"
        -Dsonar.host.url=$SONAR_URL
        -Dsonar.login=$SONAR_TOKEN
        -Dsonar.sourceEncoding="utf-8"
        -Dsonar.javascript.lcov.reportPaths=./project/coverage/lcov.info
    depends_on:
      # - Jest coverage
      - Run Biome
    when:
      event:
        - push

  - name: Check that Build is not broken
    image: registry.sp-tarkov.com/spt/node-wine:16
    commands:
      - cd ./project
      - npm run build:release
    depends_on:
      - Run Sonar scanner on push
    when:
      event:
        - push

  - name: Run Sonar scanner on tag
    image: sonarsource/sonar-scanner-cli:4
    environment:
      SONAR_PROJECT:
        from_secret: SONAR_PROJECT
      SONAR_URL:
        from_secret: SONAR_URL
      SONAR_TOKEN:
        from_secret: SONAR_TOKEN
    commands:
      - >
        sonar-scanner
        -Dsonar.projectKey=$SONAR_PROJECT
        -Dsonar.sources="./project/src"
        -Dsonar.language="ts"
        -Dsonar.host.url=$SONAR_URL
        -Dsonar.login=$SONAR_TOKEN
        -Dsonar.sourceEncoding="utf-8"
        -Dsonar.projectVersion=$DRONE_TAG
    depends_on:
      # - Jest coverage
      - Run Biome
    when:
      event:
        - tag

  - name: Build and publish docs docker image
    image: plugins/kaniko:1.6.6-kaniko1.8.1
    settings:
      repo: registry.sp-tarkov.com/spt/aki-docs
      registry: registry.sp-tarkov.com
      enable_cache: true
      cache_repo: registry.sp-tarkov.com/spt/aki-docs
      password:
        from_secret: DOCKER_PASSWORD
      username:
        from_secret: DOCKER_USERNAME
      dockerfile: project/Dockerfile.docs
      context: project/
      tags: latest
    depends_on:
      - Check that Build is not broken

  - name: Deploy docs
    image: docker:19.03.3-dind
    volumes:
      - name: docker_sock
        path: /var/run/docker.sock
    commands:
      - "docker stack deploy --compose-file project/docker-compose.yml aki-docs"
      - "docker service update --force 	aki-docs_docs"
    depends_on:
      - Build and publish docs docker image

volumes:
  - name: docker_sock
    host:
      path: /var/run/docker.sock
  - name: cache
    host:
      path: /tmp/spt-cache/pnpm_store
image_pull_secrets:
  - dockerconfig
---
kind: signature
hmac: 9edf557994aab21f2961498d21c6469ee3a97ec002728764f379760609fa4e4b

...