os: linux
branches:
  except:
    - release-4.2
    - release-4.3

language: go
go_import_path: github.com/operator-framework/operator-sdk

# go modules require xenial for mercurial TLS 1.2 support
dist: xenial

# Python 3.6 is the default Python when language: python.
# But when not language: python, Python 3 can not be used by default.
# There is pip (/usr/bin/pip), but not pip3.
# Because of it the global env is required.
env:
  global:
    - PATH=/opt/python/3.6.7/bin:$PATH

# Install python3 and utilities dependencies
addons:
  apt:
    packages:
      - "python3"
      - "python3-pip"

cache:
  directories:
    - $HOME/.cache/go-build

go:
- 1.13.x

# The `x_base_steps` top-level key is unknown to travis,
# so we can use it to create a bunch of common build step
# YAML anchors which we use in our build jobs.
x_base_steps:
  # Base go, ansbile, and helm job
  - &test
    stage: test
    env:
    - CLUSTER=k8s
    # before_install for jobs that require go builds and do not run for doc-only changes
    before_install:
      # hack/ci/check-doc-only-update.sh needs to be sourced so
      # that it can properly exit the test early with success
      - source hack/ci/check-doc-only-update.sh
      - travis_retry make tidy
    install:
      - make install
      - hack/ci/setup-${CLUSTER}.sh
      - export KUBECONFIG="$(kind get kubeconfig-path --name="kind")"
    after_success:
      - echo "Build succeeded, operator was generated, memcached operator is running on $CLUSTER, and unit/integration tests pass"
    after_failure:
      - echo "Build failed, operator failed to generate, memcached operator is not running on $CLUSTER, or unit/integration tests failed"
      - kubectl get all --all-namespaces
      - kubectl get events --all-namespaces --field-selector=type=Warning
    services:
      - docker

  # Base deploy job
  - &deploy
    stage: deploy
    if: type != pull_request AND ( tag IS present OR branch = master OR commit_message =~ /\[travis deploy\]/ )
    before_install:
      - travis_retry make tidy
    install: make install
    before_script:
      - git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
      - git fetch --unshallow --tags
    after_success:
      - echo "Image build succeeded, and docker image tagged and pushed to repository"
    after_failure:
      - echo "Image build, docker image tagging, or docker image pushing to repository failed"
    services:
      - docker

  # Manifest list deploy job
  - &manifest-deploy
    stage: "Deploy multi-arch manifest lists"
    if: type != pull_request AND ( tag IS present OR branch = master OR commit_message =~ /\[travis deploy\]/ )
    before_script:
      - git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
      - git fetch --unshallow --tags
      # TODO: remove the chmod command when docker-ce on travis is upgraded to 18.09.0+.
      # We need /etc/docker to be accessible to non-root users.
      # See https://github.com/moby/moby/pull/37847.
      - sudo chmod 0755 /etc/docker
    after_success:
      - echo "Manifest list push to registry succeeded"
    after_failure:
      - echo "Manifest list creation or push to registry failed"
    services:
      - docker

jobs:
  include:
    # Build and test go
    - <<: *test
      name: Go on Kubernetes
      script:
      - make test-e2e-go
      - make test-integration

    # Build and test ansible and test ansible using molecule
    - <<: *test
      name: Ansible on Kubernetes
      before_script:
        - pip3 install --upgrade setuptools pip
        - pip install --user ansible
      script:
        - make test-e2e-ansible
        - make test-e2e-ansible-molecule

    # Build and test helm
    - <<: *test
      name: Helm on Kubernetes
      script: make test-e2e-helm

    # Test subcommands
    - <<: *test
      name: Subcommands on Kubernetes
      script: make test-subcommand

    # Run the unit, sanity and markdown tests
    - stage: test
      name: Unit, Sanity, and Markdown Tests
      before_install:
        - (cd / && go get github.com/mattn/goveralls)
      script:
        - make test-sanity test-unit test-markdown
        - $GOPATH/bin/goveralls -service=travis-ci -coverprofile=coverage.out -repotoken=$COVERALLS_TOKEN
      after_success: echo 'Tests Passed'
      after_failure: echo 'Failure in unit, sanity and markdown tests'

    # Build and deploy amd64 ansible-operator docker image
    - <<: *deploy
      name: Docker image for ansible-operator (amd64)
      arch: amd64
      script:
        - make image-build-ansible
        - make image-push-ansible

    # Build and deploy ppc64le ansible-operator docker image
    - <<: *deploy
      name: Docker image for ansible-operator (ppc64le)
      arch: ppc64le
      script:
        - make image-build-ansible
        - make image-push-ansible

    # Build and deploy s390x ansible-operator docker image
    - <<: *deploy
      name: Docker image for ansible-operator (s390x)
      arch: s390x
      script:
        - make image-build-ansible
        - make image-push-ansible

    # Build and deploy amd64 helm-operator docker image
    - <<: *deploy
      name: Docker image for helm-operator (amd64)
      arch: amd64
      script:
        - make image-build-helm
        - make image-push-helm

    # Build and deploy ppc64le helm-operator docker image
    - <<: *deploy
      name: Docker image for helm-operator (ppc64le)
      arch: ppc64le
      script:
        - make image-build-helm
        - make image-push-helm

    # Build and deploy s390x helm-operator docker image
    - <<: *deploy
      name: Docker image for helm-operator (s390x)
      arch: s390x
      script:
        - make image-build-helm
        - make image-push-helm

    # Build and deploy amd64 scorecard-proxy docker image
    - <<: *deploy
      name: Docker image for scorecard-proxy (amd64)
      arch: amd64
      script:
        - make image-build-scorecard-proxy
        - make image-push-scorecard-proxy

    # Build and deploy ppc64le scorecard-proxy docker image
    - <<: *deploy
      name: Docker image for scorecard-proxy (ppc64le)
      arch: ppc64le
      script:
        - make image-build-scorecard-proxy
        - make image-push-scorecard-proxy

    # Build and deploy s390x scorecard-proxy docker image
    - <<: *deploy
      name: Docker image for scorecard-proxy (s390x)
      arch: s390x
      script:
        - make image-build-scorecard-proxy
        - make image-push-scorecard-proxy

    # Build and deploy ansible multi-arch manifest list
    - <<: *manifest-deploy
      name: Manifest list for ansible-operator
      script:
        - make image-push-ansible-multiarch

    # Build and deploy helm multi-arch manifest list
    - <<: *manifest-deploy
      name: Manifest list for helm-operator
      script:
        - make image-push-helm-multiarch

    # Build and deploy scorecard-proxy multi-arch manifest list
    - <<: *manifest-deploy
      name: Manifest list for scorecard-proxy
      script:
        - make image-push-scorecard-proxy-multiarch

