---
stages:
  - test
  - e2e
  - functional
  - bundle
#  - test_bundle
  - build_ui
  - package

variables:
  DOCKER_DRIVER: overlay

# Cache gems in between stages for this pipeline only
cache:
  key: "$CI_PIPELINE_ID"
  paths:
    - .tox/py27-dev

linux_test:
  image: 0xacab.org:4567/leap/soledad:latest
  stage: test
  script:
    # In this first job, recreate the tox venv from scratch
    # on start of every pipeline
    - dpkg -l tox > /dev/null || apt-get -y --no-install-recommends install tox
    - tox --recreate -e py27-dev
  tags:
    - linux

# Currently, our only OSX gitlab runner is unavailable too often
# and the tests fail in CI anyway so we disable it for now until
# we fix both.
#osx_test:
#  stage: test
#  allow_failure: true
#  script:
#    - tox --recreate -e py27-dev
#  tags:
#    - osx

bitmask_latest_bundle:
  image: 0xacab.org:4567/leap/bitmask-dev:latest
  stage: bundle
  script:
    - pkg/build_bundle_with_venv.sh
    - mv "dist/bitmask-`cat pkg/next-version`" .
  artifacts:
    paths:
      - "bitmask-`cat pkg/next-version`"
    name: "Bitmask_linux64_latest_${CI_BUILD_REF}"
    expire_in: 1 month
  tags:
    - linux

e2e_tests:
  image: 0xacab.org:4567/leap/bitmask-dev:latest
  stage: e2e
  allow_failure: true
  dependencies:
      - linux_test
  script:
    - dpkg -l tox > /dev/null || apt-get -y --no-install-recommends install tox
    - tox -e py27-dev --notest
    - source .tox/py27-dev/bin/activate
    - make dev-latest-backend
    - mkdir -p /root/.config/leap/
    - make install_helpers
    - make test_e2e
  tags:
    - linux

functional_tests:
  image: 0xacab.org:4567/leap/bitmask-dev:latest
  stage: functional
  dependencies:
      - e2e_tests
  before_script:
    - dpkg -l tox > /dev/null || apt-get -y --no-install-recommends install tox
    - tox -e py27-dev --notest
    - source .tox/py27-dev/bin/activate
    - make dev-latest-all
    - make test_functional_setup
  script:
    - make test_functional
  artifacts:
    when: on_failure
    paths:
      - failures
    name: "Bitmask_linux64_${CI_BUILD_REF}_e2e_screenshots"
    expire_in: 1 month
  tags:
    - linux

# This is Work In Progress
#functional_test_bundle:
#  image: 0xacab.org:4567/leap/bitmask-dev:latest
#  stage: test_bundle
#  allow_failure: true
#  dependencies:
#    - bitmask_latest_bundle
#  script:
#    - TEST_MODE='bundle_ci' make test_functional
#  artifacts:
#    when: on_failure
#    paths:
#      - failures
#    name: "Bitmask_linux64_${CI_BUILD_REF}_e2e_screenshots"
#    expire_in: 1 month
#  tags:
#    - linux

build_ui:
  image: 0xacab.org:4567/leap/bitmask-dev:latest
  stage: build_ui
  script:
    - cd ui && make dev-build
  tags:
    - linux
  cache:
    paths:
    - ui/node_modules/

build_docker_image:
  image: 0xacab.org:4567/leap/bitmask-dev:latest
  stage: test
  services:
    - docker:dind
  tags:
    - docker-in-docker
  only:
    - branches@leap/bitmask-dev
  before_script:
     - >
       export LAST_COMMIT=$(curl -s --header "PRIVATE-TOKEN: ${LEAP_CODE_O_MATIC_PRIVATE_TOKEN}" https://0xacab.org/api/v4/projects/574/pipelines |
       python -c "import sys, json; print json.load(sys.stdin)[1]['sha']")
  script:
    - >
      if git diff $LAST_COMMIT HEAD --name-only|grep tests/docker; then
        docker --version
        docker info
        docker login -u gitlab-ci-token -e sysdev@leap.se -p $CI_JOB_TOKEN $CI_REGISTRY
        docker build -t ${CI_REGISTRY_IMAGE}:latest tests/docker
        docker push ${CI_REGISTRY_IMAGE}:latest
      fi

.job_template: &job_definition
  stage: package
  image: "0xacab.org:4567/leap/gitlab-buildpackage:build_${DIST}_${ARCH}"
  script:
    - "pwd; git describe"
    - build-build-package
    # Test the package with lintian
    - build-test-lintian
    - upload-package
    # sleep 1h to allow debugging of running container
    #- sleep 3600
  artifacts:
    expire_in: 1w
    paths:
    - '*_*.xz'
    - '*_*.dsc'
    - '*_amd64.changes'
    - '*.deb'
    - 'results/*'

package:amd64_stretch:
  variables:
    ARCH: "amd64"
    DIST: "stretch"
    REPONAMES: "client"
    # Default is to fail on warnings, we disable it here
    # unless all lintian warnings are fixed (#9033)
    LINTIAN_OPTS: "-X filename-length, --suppress-tags newer-standards-version"
  <<: *job_definition

package:amd64_buster:
  variables:
    ARCH: "amd64"
    DIST: "buster"
    REPONAMES: "client"
    # Default is to fail on warnings, we disable it here
    # unless all lintian warnings are fixed (#9033)
    LINTIAN_OPTS: "-X filename-length"
  <<: *job_definition

package:amd64_sid:
  variables:
    ARCH: "amd64"
    DIST: "sid"
    REPONAMES: "client"
    # Default is to fail on warnings, we disable it here
    # unless all lintian warnings are fixed (#9033)
    LINTIAN_OPTS: "-X filename-length, --suppress-tags newer-standards-version"
  <<: *job_definition

package:amd64_zesty:
  variables:
    ARCH: "amd64"
    DIST: "zesty"
    REPONAMES: "client"
    # Default is to fail on warnings, we disable it here
    # unless all lintian warnings are fixed (#9033)
    LINTIAN_OPTS: "-X filename-length, --suppress-tags newer-standards-version"
  <<: *job_definition

package:amd64_artful:
  variables:
    ARCH: "amd64"
    DIST: "artful"
    REPONAMES: "client"
    # Default is to fail on warnings, we disable it here
    # unless all lintian warnings are fixed (#9033)
    LINTIAN_OPTS: "-X filename-length, --suppress-tags newer-standards-version"
  <<: *job_definition