image: registry.gitlab.tugraz.at/dbp/web-components/toolkit/main:v1

cache:
  key: ${CI_PROJECT_PATH}
  paths:
    - _yarn_cache
    - _cypress_cache

before_script:
  - yarn config set cache-folder "$CI_PROJECT_DIR/_yarn_cache"
  - export CYPRESS_CACHE_FOLDER="$CI_PROJECT_DIR/_cypress_cache"

stages:
  - test
  - deploy
  - e2e-stage1
  - e2e-stage2
  - e2e-stage3

test:
  stage: test
  script:
    - yarn install
    - yarn run test

linting:
  stage: test
  script:
    - yarn install
    - yarn run lint

publish:
  stage: deploy
  only:
    refs:
      - deploy
      - master
  script:
    # https://www.npmjs.com/settings/dbp-deploy/tokens
    # NPM_TOKEN needs to be a "Publish" token with 2FA disabled!
    # "Automation" tokens don't work with lerna (even if they would work with "npm publish")
    - echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}' > ~/.npmrc
    - yarn install
    - yarn run build
    - yarn run publish

.deploy_defaults: &deploy_defaults
  except:
    - schedules
  stage: deploy
  script:
    # Add ssh key
    - mkdir -p ~/.ssh
    - echo "${DEPLOY_KEY}" | tr -d '\r' > ~/.ssh/id_rsa
    - chmod 700 ~/.ssh && chmod 600 ~/.ssh/id_rsa
    - ssh-keyscan -t rsa "${DEPLOY_HOST}" >> ~/.ssh/known_hosts
    # Deploy
    - cd toolkit-showcase
    - dep deploy "${CI_ENVIRONMENT_NAME}"
    - echo "Deployed to ${CI_ENVIRONMENT_URL}"
    # Simple health check
    - curl --max-time 10 --retry 3 --output /dev/null --silent --show-error --fail --location "${CI_ENVIRONMENT_URL}"

deploy_demo:
  only:
    refs:
      - demo
  environment:
    name: demo
    url: https://frontend-demo.tugraz.at/apps/demo
  variables:
    DEPLOY_HOST: mw01-dev.tugraz.at
    DEPLOY_KEY: "$DEPLOY_SSH_KEY"
  <<: *deploy_defaults

deploy_development:
  only:
    refs:
      - master
  environment:
    name: development
    url: https://mw-frontend-dev.tugraz.at/apps/demo
  variables:
    DEPLOY_HOST: mw01-dev.tugraz.at
    DEPLOY_KEY: "$DEPLOY_SSH_KEY"
  <<: *deploy_defaults

# For docker images see:
# https://github.com/cypress-io/cypress-docker-images/tree/master/browsers#cypressbrowsers

.e2e_test_defaults: &e2e_test_defaults
  only:
    refs:
      - master
  # We don't need artifacts from previous runs
  dependencies: []
  script:
    - git clone https://gitlab.tugraz.at/dbp/middleware/e2e-tests.git --depth 1
    - cd e2e-tests
    - yarn install
    #    - ./node_modules/.bin/cypress run --browser "$BROWSER" --spec "cypress/integration/toolkit-showcase/*.spec.js"
    - xvfb-run ./node_modules/.bin/cypress run --browser "$BROWSER" --headed --spec "cypress/integration/toolkit-showcase/*.spec.js"
  artifacts:
    paths:
      - e2e-tests/cypress/videos/*
      - e2e-tests/cypress/screenshots/*
    expire_in: 4 week
    when: always

ff75:
  image: cypress/browsers:node13.8.0-chrome81-ff75
  stage: e2e-stage1
  variables:
    MOZ_FORCE_DISABLE_E10S: 'true'
    BROWSER: 'firefox'
  <<: *e2e_test_defaults

chrome84:
  image: cypress/browsers:node14.7.0-chrome84
  stage: e2e-stage2
  variables:
    BROWSER: 'chrome'
  <<: *e2e_test_defaults

edge88:
  image: cypress/browsers:node14.10.1-edge88
  stage: e2e-stage3
  variables:
    BROWSER: 'edge'
  <<: *e2e_test_defaults