image: node:16-alpine
stages:
  - test
  - build
  - deploy

# Prevent detached merge request pipelines
# https://docs.gitlab.com/ee/ci/merge_request_pipelines/
# Those do not run the environment on_stop jobs when MRs are closed/merged
# or when branches get deleted, unlike branch pipelines.
workflow:
  rules:
    - if: '$CI_MERGE_REQUEST_ID'
      when: never
    - when: always

before_script:
  - npm ci

frontend-test:
  stage: test
  script:
    - npm run test -- --reporter=xunit --reporter-option output=test-report.xml

  artifacts:
    when: always
    reports:
      junit:
        - test-report.xml

frontend-lint:
  stage: test

  before_script:
    - npm ci
    - apk add py3-pip bash git build-base python3-dev
    - pip3 install pre-commit

  script:
    - pre-commit run -a

frontend-audit:
  stage: test
  script:
    - npm audit --parseable
  allow_failure: true

.build:
  stage: build
  # Ignore frontend-audit to speed up CI builds
  needs:
    - frontend-test
    - frontend-lint
  artifacts:
    paths:
      - dist
    expire_in: 2 weeks

frontend-build-surge:
  extends: .build

  # Only deploy branches on surge
  rules:
    - if: '$CI_COMMIT_BRANCH && $CI_COMMIT_BRANCH != "master"'
      when: on_success
    - when: never

  variables:
    # On merge requests we build using preprod to be able
    # to test on surge.sh
    VUE_APP_API_BASE_URL: https://preprod.arkindex.teklia.com/api/v1

  script:
    - npm run surge

frontend-build:
  extends: .build

  # Build only for master and tags
  rules:
    - if: '$CI_COMMIT_BRANCH && $CI_COMMIT_BRANCH != "master"'
      when: never
    - when: on_success

  script:
    - npm run production

docker-build:
  stage: build
  image: docker:20.10
  services:
    - docker:dind

  # Needed to avoid triggering global npm ci
  before_script:
    - ''
  except:
    - schedules

  script:
    - ci/build.sh

frontend-deploy:
  image: python:3-alpine
  stage: deploy

  rules:
    - if: '$CI_COMMIT_TAG'
      when: on_success
    - when: never

  dependencies:
    - frontend-build

  # Needed to avoid triggering global npm ci
  before_script:
    - ''
  script:
    - ci/deploy.sh

sentry-source-maps:
  image: getsentry/sentry-cli
  stage: deploy

  rules:
    - if: '$CI_COMMIT_TAG'
      when: on_success
    - when: never

  variables:
    RELEASE: "arkindex-frontend@$CI_COMMIT_TAG"
    SENTRY_ORG: teklia
    SENTRY_PROJECT: arkindex-frontend

  dependencies:
    - frontend-build

  # Needed to avoid triggering global npm ci
  before_script:
    - ''
  script:
    - sentry-cli releases new $RELEASE
    - sentry-cli releases files $RELEASE upload-sourcemaps dist
    - sentry-cli releases finalize $RELEASE

.surge:
  stage: deploy

  environment:
    name: ${CI_COMMIT_REF_SLUG}

  before_script:
    - npm install -g surge

deploy-surge:
  extends: .surge

  # Only deploy branches on surge
  rules:
    - if: '$CI_COMMIT_BRANCH && $CI_COMMIT_BRANCH != "master"'
      when: on_success
    - when: never

  environment:
    url: https://${CI_COMMIT_REF_SLUG}-teklia.surge.sh
    on_stop: stop-surge

  script:
    - surge dist ${CI_ENVIRONMENT_URL}

stop-surge:
  extends: .surge

  # Only deploy branches on surge
  rules:
    - if: '$CI_COMMIT_BRANCH && $CI_COMMIT_BRANCH != "master"'
      when: manual
      allow_failure: true
    - when: never

  # Do not try to checkout the branch if it was deleted
  variables:
    GIT_STRATEGY: none

  environment:
    action: stop

  script:
    - surge teardown ${CI_ENVIRONMENT_URL}

release-notes:
  stage: deploy
  image: registry.gitlab.teklia.com/infra/devops:latest

  only:
    - tags

  # Needed to avoid triggering global npm ci
  before_script:
    - ''

  script:
    - devops release-notes
