sudo: required
language: node_js

services:
  - docker

branches:
  only:
    - master
    - /^release-[0-9]+\..*$/
addons:
  chrome: stable
  sonarcloud:
    organization: "open-cluster-management"
    token:
      secure: "stfsRk8QKfns8hqznhrVmaEN0jvisW3aGF16wKh9necbxU5YxmA4lyusGbgltqQrZbAA5w23u5g829thLEdQUYcZOWn2UciYAhCzz1YfP34Ac+RIztJ92gc1VauQ6cSuSZbQ/2kmpaLD3OgdG5AJjrJtlcHGNAal2y5JJk+O+gB5RddADiA2TqpvBYcMZTiSBgcfgTYVWgSANbtjTtcj62dLLeaVRT4FADk6jrukD363RKTnJnQ+xBz+STn2vyBs3OENznciXBc5o0tCsebO7DOQAnbHccyxaNEp+qK34tH1xfVY04opy0f0cJ6UUI9PmI6cwt7BNTupYLfPsBknlz8mEDOazR+aaxUGloJDpQZ5oqPYYOjNIj0SI3zTgxLUgZ5PHOmhf+j9Ld/LJM2U3QiulL65C/j7Y/GuOi+VzUTHh8mtnPP30k2Y4rh1SPaInJhtTqOeytU4y2/FSmD9N5JBY8CguoKz6RVjnoBxBySvC6MIob6CfJgYlXuc2tu+l6lHqokPaZemnKXc3ahWJlADTWH3CI1zyXINLj7KAv4GLmi64BMFehwqH41BQUVQL2dwGWwfGLVhJ0mMohPL5J/HG7c4WPpvtzxI5Xg9SeihqCWavz0o9/5z/+Fcc6BV/u0BPDkNDCG8OGAwWK/idbm2V3Iw5iMBRLt8ufkxJ+8="

env:
  global:
    # Required
    - OS=linux
    - COMPONENT_TAG_EXTENSION="-${TRAVIS_COMMIT}"
    # Component Specific
    - COMPONENT_INIT_COMMAND=${TRAVIS_BUILD_DIR}/build/install-dependencies.sh
    - COMPONENT_BUILD_COMMAND=${TRAVIS_BUILD_DIR}/build/build.sh
    - COMPONENT_UNIT_TEST_COMMAND=${TRAVIS_BUILD_DIR}/build/run-unit-tests.sh
    - COMPONENT_E2E_TEST_COMMAND=${TRAVIS_BUILD_DIR}/build/run-e2e-tests.sh

stages:
  - build
  - unit-test
  - e2e-test
  - publish
 
before_script:
  - make init

jobs:
  include:
    - stage: build
      name: "Build the image"
      script:
        - set -e
        - if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then export COMPONENT_TAG_EXTENSION="-PR${TRAVIS_PULL_REQUEST}-${TRAVIS_COMMIT}"; fi;
        - make copyright-check TRAVIS_BRANCH=${TRAVIS_BRANCH}
        - echo '"start grc-ui building..."' && echo -en 'travis_fold:start:script.build\\r'
        - make component/build
        - echo '"finished grc-ui build"' && echo -en 'travis_fold:end:script.build\\r'
        - echo '"start image pushing..."' && echo -en 'travis_fold:start:script.build\\r'
        - make component/push
        - echo '"finished image push"' && echo -en 'travis_fold:end:script.build\\r'
        - if [ "$IMAGE_SCAN" != "false" ]; then make security/scans; fi;
        - if [ "$TRAVIS_PULL_REQUEST" == "false" -a "$TRAVIS_BRANCH" == "master" ]; then 
            COMPONENT_NEWTAG="latest-dev" make component/tag;
            COMPONENT_VERSION="latest" COMPONENT_TAG_EXTENSION="-dev" make component/push;
          fi;
        - if [ "$IMAGE_SCAN" != "false" ]; then make security/scans; fi;
    - stage: build
      name: "Build the test image"
      if: type != pull_request AND branch = master
      script:
        - set -e
        - if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then export COMPONENT_TAG_EXTENSION="-PR${TRAVIS_PULL_REQUEST}-${TRAVIS_COMMIT}"; fi;
        - export COMPONENT_BUILD_COMMAND="docker build . -f Dockerfile.e2etest -t "
        - export COMPONENT_NAME="grc-ui-tests"
        - make build-test-image
        - make push-test-image
        - COMPONENT_NEWTAG="latest-dev" make component/tag;
        - COMPONENT_VERSION="latest" COMPONENT_TAG_EXTENSION="-dev" make component/push;
    - stage: build
      name: "Build the test image"
      if: type = push AND branch =~ /^release-[0-9]+\..*$/
      script:
        - set -e
        - if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then export COMPONENT_TAG_EXTENSION="-PR${TRAVIS_PULL_REQUEST}-${TRAVIS_COMMIT}"; fi;
        - export COMPONENT_BUILD_COMMAND="docker build . -f Dockerfile.e2etest -t "
        - export COMPONENT_NAME="grc-ui-tests"
        - make build-test-image
        - make push-test-image
    - stage: unit-test
      name: "Build and run unit tests"
      script:
        - set -e
        - make component/test/unit
    - stage: e2e-test
      name: "Run unit tests and e2e tests"
      if: type = pull_request
      script:
        - if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then export COMPONENT_TAG_EXTENSION="-PR${TRAVIS_PULL_REQUEST}-${TRAVIS_COMMIT}"; fi;
        - make sonar/js/jest-init
        - echo '"start unit testing..."' && echo -en 'travis_fold:start:script.build\\r'
        - make component/test/unit
        - echo '"finished unit test"' && echo -en 'travis_fold:end:script.build\\r'
        - echo '"start e2e testing..."' && echo -en 'travis_fold:start:script.build\\r'
        - make component/test/e2e
        - echo '"finished e2e test"' && echo -en 'travis_fold:end:script.build\\r'
        - npm run merge:coverage
        - echo '"start sonar checking..."' && echo -en 'travis_fold:start:script.build\\r'
        - make sonar/js
        - echo '"finished sonar check"' && echo -en 'travis_fold:end:script.build\\r'
      after_failure:
        - make travis-slack-reporter
        - docker logs grcuiapi
    - stage: publish
      name: "Publish the image to quay with an official version/sha tag and publish entry to integration pipeline stage"
      if: type = push AND branch =~ /^release-[0-9]+\..*$/
      script:
        - set -e
        - make pipeline-manifest/update PIPELINE_MANIFEST_COMPONENT_SHA256=${TRAVIS_COMMIT} PIPELINE_MANIFEST_COMPONENT_REPO=${TRAVIS_REPO_SLUG} PIPELINE_MANIFEST_BRANCH=${TRAVIS_BRANCH}
        - echo "Waiting 60s before publishing test image..." && sleep 60
        - make publish-test-image