os:
  - linux

language: go
go:
  - "1.13.x"

services:
  - docker

# need to onboard in sonarcloud later
addons:
  sonarcloud:
    organization: "open-cluster-management"
    token:
      secure: "DqdTvWtdljB/Uu3wy5szBZnxAfNlKHyvD7gubDEnqJqMCImZ3PCHW9TuCJk90tdDWwipnsjTMD7kn09eLETzy0TO4HQeoVd9EoATgU42oXgrRgL199X3XuXQI1vlmO7sS0SnW6/ZPvKdwRzMXjzVh63kOhjDhCFprBHLu7ayOPW9PtmBHF4kf69st4XrGmRHAXIK51NZP6AJOg5sVsdi5y+Ijdfm6NdwOCMsXWv+94BYUpjFpnIkTEqqb64H0suFCeus2s73mMQAfXwnLWM6hBkOyTwfI/RDceVHvvYJZPFnxDInYaP7ZrOnWOHeJ6jfMAk9oUxwo4rjbH7lj9u1YAh2Mpn3QMEmqSSqJezIvrkARM7f309RcVYphWB289/edDyXQNkj//Rab7Yyrf+RgOxpjf6kwV8YnGlHz7s2C2IDAv3CClcDnnZ+iTsE4r8JeJQgt+EOOetILdo0RTQC4HzfZ2liKYNxpfNf5H16+DZTsXhF3KyX00NTAVBmTOULnjWjwzCRCNKZvYEL8h4jo5JjtkYb/2kqQZeojjTQIdYGL4lFEOb1Md9UFqVd/KSazX8ffgr1SDhz8IFCTSBdcWKmOZ8sfgz5e4gHwDxCL/jQbic97F6eXvQWL6Gri9+383DvdXrybADOo/SqG5Mmh51hlfVwyY56bagQ+mVDVvU="

#branches:
#  only:
#    - master
#    - /^release-[0-9]+\..*$/

env:
  global:
    # Required
    - OS=linux
    - COMPONENT_TAG_EXTENSION="-${TRAVIS_COMMIT}"

    # Component Specific
    - COMPONENT_INIT_COMMAND=${TRAVIS_BUILD_DIR}/cicd-scripts/install-dependencies.sh
    - COMPONENT_BUILD_COMMAND=${TRAVIS_BUILD_DIR}/cicd-scripts/build.sh
    - COMPONENT_UNIT_TEST_COMMAND=${TRAVIS_BUILD_DIR}/cicd-scripts/run-unit-tests.sh
    - COMPONENT_DEPLOY_COMMAND=${TRAVIS_BUILD_DIR}/cicd-scripts/deploy-to-cluster.sh
    - COMPONENT_E2E_TEST_COMMAND=${TRAVIS_BUILD_DIR}/cicd-scripts/run-e2e-tests.sh

stages:
  - build
  - unit-test
  - test-e2e
  - release-ff
  - publish

before_script:
  - make init
  - echo "https://${GITHUB_TOKEN}:x-oauth-basic@github.com" >> ~/.git-credentials
  - export GOPRIVATE=github.com/open-cluster-management
  - git config --global url."https://$GITHUB_TOKEN@github.com/open-cluster-management".insteadOf "https://github.com/open-cluster-management"
  
jobs:
  include:
    - stage: build
      name: "Build the test image and push it"
      script:
        - if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then export COMPONENT_TAG_EXTENSION="-PR${TRAVIS_PULL_REQUEST}-${TRAVIS_COMMIT}"; fi;
        - |
          make
          make component/build
          make component/push
          make imagescan/status
          make security/scans
    - stage: unit-test
      name: "Run unit tests"
      script:
        # Set the image tag differently for PRs
        - if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then export COMPONENT_TAG_EXTENSION="-PR${TRAVIS_PULL_REQUEST}-${TRAVIS_COMMIT}"; fi;
        # Bootstrap the build harness, pull test image, and run unit tests.  
        - |
          make 
          make component/test/unit
          make sonar/go
          make copyright-check TRAVIS_BRANCH=${TRAVIS_BRANCH}
    - stage: test-e2e
      name: "Deploy the image to a cluster and run e2e tests"
      script:
        #Check out a clusterpool, set up oc, deploy, run e2e tests, and return clusterpool cluster
        - if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then export COMPONENT_TAG_EXTENSION="-PR${TRAVIS_PULL_REQUEST}-${TRAVIS_COMMIT}"; fi;
        - |
          make 
          make component/pull 
          make component/test/e2e
    - stage: release-ff
      name: "Push commits to current release branch"
      if: type = push AND branch =~ /^master$/
      script:
        - make
        - make release-ff
    - 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:
        - |
          make 
          make pipeline-manifest/update PIPELINE_MANIFEST_COMPONENT_SHA256=${TRAVIS_COMMIT} PIPELINE_MANIFEST_COMPONENT_REPO=${TRAVIS_REPO_SLUG} PIPELINE_MANIFEST_BRANCH=${TRAVIS_BRANCH}