image: registry.gitlab.com/openstapps/app

before_script:
  - npm ci

default:
  tags:
    - performance

stages:
  - setup
  - build
  - test
  - publish
  - deploy

setup:
  image: registry.gitlab.com/openstapps/projectmanagement/builder
  stage: setup
  only:
    - schedules
  variables:
    DOCKER_DRIVER: overlay2
  services:
    - docker:dind
  script:
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
    - docker build -t registry.gitlab.com/openstapps/app .
    - docker push registry.gitlab.com/openstapps/app
  cache: {} # disable irrelevant cache for this job
  before_script: [] # do not run irrelevant before script for this job
  tags:
    - docker

build:
  stage: build
  script:
    - npm run build
  artifacts:
    paths:
      - www

lint:
  stage: build
  script:
    - npm run lint

unit:
  stage: test
  script:
    - npm run test -- --watch=false --no-progress --code-coverage
  artifacts:
    paths:
    - coverage

e2e:
  stage: test
  script:
    - npm run e2e

audit:
  stage: test
  script:
    - npm audit
  allow_failure: true
  except:
    - schedules

scheduled-audit:
  stage: test
  script:
    - npm audit
  only:
    - schedules

pages:
  stage: publish
  script:
    - npm run documentation
    - mv docs public
  only:
    - master
  artifacts:
    paths:
      - public

executable:
  image: registry.gitlab.com/openstapps/projectmanagement/builder
  stage: publish
  variables:
    DOCKER_DRIVER: overlay2
  services:
    - docker:dind
  script:
    - npm run build:prod
    - export REGISTRY_BRANCH=$(.gitlab/ci/getRegistryBranch.sh "$CI_JOB_NAME")
    - export CORE_VERSION=$(openstapps-projectmanagement get-used-version @openstapps/core)
    - export IMAGETAG_BASE=$CI_REGISTRY_IMAGE/$REGISTRY_BRANCH
    - export IMAGETAG_CORE_VERSION=$IMAGETAG_BASE:core-$CORE_VERSION
    - export IMAGETAG_LATEST=$IMAGETAG_BASE:latest
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
    - docker build -t $IMAGETAG_CORE_VERSION -t $IMAGETAG_LATEST -f Dockerfile.Executable .
    - docker push $IMAGETAG_CORE_VERSION
    - .gitlab/ci/pushAsLatestVersion.sh "$CI_COMMIT_REF_NAME" "$IMAGETAG_BASE"
  only:
    - /^(v[0-9]+\.[0-9]+\.[0-9]+|master|develop)$/
  tags:
    - docker

review:
  stage: deploy
  script:
    - .gitlab/ci/enableGitlabReviewToolbar.sh www/index.html "$CI_PROJECT_ID" "$CI_OPEN_MERGE_REQUESTS"
    - cp www/index.html www/200.html
    - ./node_modules/.bin/surge -p ./www -d https://$CI_PROJECT_PATH_SLUG-$CI_ENVIRONMENT_SLUG.surge.sh/
  environment:
    name: review/$CI_PROJECT_PATH_SLUG-$CI_COMMIT_REF_NAME
    url: https://$CI_PROJECT_PATH_SLUG-$CI_ENVIRONMENT_SLUG.surge.sh/
    on_stop: stop_review
  tags:
    - secrecy
  except:
    - master
    - develop

stop_review:
  stage: deploy
  variables:
    GIT_STRATEGY: none
  script:
    - ./node_modules/.bin/surge teardown $CI_PROJECT_PATH_SLUG-$CI_ENVIRONMENT_SLUG.surge.sh
  when: manual
  environment:
    name: review/$CI_PROJECT_PATH_SLUG-$CI_COMMIT_REF_NAME
    action: stop
  tags:
    - secrecy

staging:
  stage: deploy
  script:
    - cp www/index.html www/200.html
    - ./node_modules/.bin/surge -p ./www -d https://$CI_PROJECT_PATH_SLUG-staging.surge.sh/
  environment:
    name: staging
    url: https://$CI_PROJECT_PATH_SLUG-staging.surge.sh/
  only:
  - develop
  tags:
    - secrecy

production_demo:
  stage: deploy
  script:
    - npm run build:prod
    - cp www/index.html www/200.html
    - ./node_modules/.bin/surge -p ./www -d https://$CI_PROJECT_PATH_SLUG.surge.sh/
  environment:
    name: production
    url: https://$CI_PROJECT_PATH_SLUG.surge.sh/
  only:
  - master
  tags:
    - secrecy
