stages:
 - build
 - test
 - deploy


# #################################################################################################
# runs
.config:run:compatibility_only: &config_run_only_compatibility
  only:
    variables:
     - $SCHEDULED_COMPATIBILITY

.config:run:nightly_only: &config_run_only_nightly
  only:
    variables:
     - $SCHEDULED_NIGHTLY

.config:run:nightly_except: &config_run_except_nightly
  except:
    variables:
     - $SCHEDULED_NIGHTLY

.config:run:release_tag: &config_run_only_release
  only:
    - /^v.*$/
  except:
    - branches

# #################################################################################################
# distro builds and tests config
.config: &config_buildbase
  <<: *config_run_except_nightly

.config:builder: &config_builder
  image: gitlab.fit.cvut.cz:5000/algorithms-library-toolkit/ci-docker-images/alt-builder:latest
  <<: *config_buildbase

.config:arch: &config_arch
  image: archlinux/base
  <<: *config_buildbase
  <<: *config_run_only_compatibility

.config:alpine: &config_alpine
  image: alpine:3.9
  <<: *config_buildbase
  <<: *config_run_only_compatibility

.config:debian-testing: &config_debiantesting
  image: amd64/debian:testing-slim
  <<: *config_buildbase
  <<: *config_run_only_compatibility

.config:debian-testing:clang: &config_debiantesting_clang
  image: amd64/debian:testing-slim
  <<: *config_buildbase
  <<: *config_run_only_compatibility

.config:debian-stable: &config_debianstable
  image: amd64/debian:stable-slim
  <<: *config_buildbase
  <<: *config_run_only_compatibility

.config:opensuse-leap: &config_opensuseleap
  image: opensuse/leap:latest
  <<: *config_buildbase
  <<: *config_run_only_compatibility

.config:ubuntu-lts: &config_ubuntults
  image: ubuntu:latest
  <<: *config_buildbase
  <<: *config_run_only_compatibility

.config:ubuntu-rolling: &config_ubunturolling
  image: ubuntu:rolling
  <<: *config_buildbase
  <<: *config_run_only_compatibility

# #################################################################################################
# stage: build

# -----------------------------------------------
# static tests

.static: &static_template
  <<: *config_run_except_nightly
  stage: build
  image: gitlab.fit.cvut.cz:5000/algorithms-library-toolkit/ci-docker-images/alt-builder:latest
  before_script:
    - mkdir -p release && pushd release
    - ../CMake/generate.py -wm
    - cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .. -GNinja
    - popd
  allow_failure: true

static:cppcheck:
  <<: *static_template
  script:
    - cppcheck -q --enable=all --project=release/compile_commands.json --suppress="*:*/contrib/*" --error-exitcode=1

static:clang-tidy:
  <<: *static_template
  script:
    - jq ".[].file" release/compile_commands.json | tr -d "\"" | grep -v "test-src" | xargs -n1 -P$(grep -c processor /proc/cpuinfo) clang-tidy-9 -p release/

# -----------------------------------------------
# build

.build: &template_build
  stage: build
  script:
    - mkdir -p release && cd release
    - ../CMake/generate.py -wm
    - cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .. -GNinja
    - ninja -j $(grep -c processor /proc/cpuinfo)
  artifacts:
    paths:
      - release/
    expire_in: 1 day

build:builder:gcc:
  <<: *config_builder
  <<: *template_build

build:builder:clang:
  <<: *config_builder
  <<: *template_build
  variables:
    CXX: clang++-9

build:arch:
  <<: *config_arch
  <<: *template_build
  before_script:
    - pacman -Syu --noconfirm base-devel cmake ninja python libxml2 tclap readline qt5-base graphviz jsoncpp
    - pacman -Syu --noconfirm binutils && strip --remove-section=.note.ABI-tag /usr/lib/libQt5Core.so.5 # qt hack

build:alpine:
  <<: *config_alpine
  <<: *template_build
  before_script:
    - apk add --no-cache bash build-base cmake ninja python3 libexecinfo-dev libxml2-dev tclap-dev readline-dev qt5-qtbase-dev graphviz-dev jsoncpp-dev

#build:debian:stable:
#  <<: *config_debianstable
#  <<: *template_build
#  before_script:
#    - apt-get update && apt-get install -y build-essential cmake ninja-build python3 libxml2-dev libtclap-dev libreadline-dev qtbase5-dev graphviz-dev libjsoncpp-dev

build:debian:testing:
  <<: *config_debiantesting
  <<: *template_build
  before_script:
    - apt-get update && apt-get install -y build-essential cmake ninja-build python3 libxml2-dev libtclap-dev libreadline-dev qtbase5-dev graphviz-dev libjsoncpp-dev

build:debian:testing:clang:
  <<: *config_debiantesting_clang
  <<: *template_build
  before_script:
    - apt-get update && apt-get install -y build-essential cmake ninja-build python3 libxml2-dev libtclap-dev libreadline-dev qtbase5-dev graphviz-dev libjsoncpp-dev ninja-build
    - apt-get install -y clang-7 lld
  variables:
    CXX: clang++-7

build:opensuse-leap:
  <<: *config_opensuseleap
  <<: *template_build
  before_script:
    - zypper -n install python3 cmake gcc-c++ bash ninja libxml2-devel tclap readline-devel libQt5Widgets-devel libQt5Xml-devel graphviz-devel jsoncpp-devel

build:ubuntu:lts:
  <<: *config_ubuntults
  <<: *template_build
  before_script:
    - apt-get update && apt-get install -y build-essential cmake ninja-build python3 libxml2-dev libtclap-dev libreadline-dev qtbase5-dev graphviz-dev libjsoncpp-dev

build:ubuntu:rolling:
  <<: *config_ubunturolling
  <<: *template_build
  before_script:
    - apt-get update && apt-get install -y build-essential cmake ninja-build python3 libxml2-dev libtclap-dev libreadline-dev qtbase5-dev graphviz-dev libjsoncpp-dev

# -----------------------------------------------

build:doc:
  <<: *config_run_except_nightly
  image: alpine:3.9
  stage: build
  before_script:
    - apk add --no-cache doxygen graphviz
  script:
    - doxygen
  allow_failure: true
  artifacts:
    name: docs
    paths:
      - doc/
    expire_in: 1 day

# #################################################################################################
# stage: test

# -----------------------------------------------
# smoke tests

.test: &template_test
  stage: test
  script:
    - cd release
    - ctest . --output-on-failure -j $(grep -c processor /proc/cpuinfo)

test:builder:gcc:
  <<: *config_builder
  <<: *template_test
  dependencies:
    - build:builder:gcc

test:builder:clang:
  <<: *config_builder
  <<: *template_test
  dependencies:
    - build:builder:clang

test:arch:
  <<: *config_arch
  <<: *template_test
  dependencies:
    - build:arch
  before_script:
    - pacman -Syu --noconfirm base-devel bash bc coreutils cmake ninja python libxml2 tclap readline qt5-base graphviz jsoncpp
    - pacman -Syu --noconfirm binutils && strip --remove-section=.note.ABI-tag /usr/lib/libQt5Core.so.5 # qt hack

test:alpine:
  <<: *config_alpine
  <<: *template_test
  dependencies:
    - build:alpine
  before_script:
    - apk add --no-cache bash bc coreutils python3 cmake libexecinfo libxml2 tclap readline qt5-qtbase qt5-qtbase-x11 graphviz jsoncpp

#test:debian:stable:
#  <<: *config_debianstable_test
#  <<: *template_test
#  dependencies:
#   - build:debian:stable
#  before_script:
#   - apt-get update && apt-get install -y build-essential bash cmake libxml2 libreadline7 libqt5widgets5 libqt5xml5 graphviz libjsoncpp1

test:debian:testing:
  <<: *config_debiantesting
  <<: *template_test
  dependencies:
    - build:debian:testing
  before_script:
    - apt-get update && apt-get install -y build-essential bash cmake libxml2 libreadline7 libqt5widgets5 libqt5xml5 graphviz libjsoncpp1

test:debian:testing:clang:
  <<: *config_debiantesting_clang
  <<: *template_test
  dependencies:
    - build:debian:testing:clang
  before_script:
    - apt-get update && apt-get install -y build-essential bash cmake libxml2 libreadline7 libqt5widgets5 libqt5xml5 graphviz libjsoncpp1

test:opensuse-leap:
  <<: *config_opensuseleap
  <<: *template_test
  dependencies:
    - build:opensuse-leap
  before_script:
    - zypper -n install bash cmake libxml2 readline libQt5Widgets5 libQt5Xml5 graphviz libjsoncpp19

test:ubuntu:lts:
  <<: *config_ubuntults
  <<: *template_test
  dependencies:
    - build:ubuntu:lts
  before_script:
    - apt-get update && apt-get install -y build-essential bash cmake libxml2 libreadline7 libqt5widgets5 libqt5xml5 graphviz libjsoncpp1

test:ubuntu:rolling:
  <<: *config_ubunturolling
  <<: *template_test
  before_script:
    - apt-get update && apt-get install -y build-essential bash cmake libxml2 libreadline8 libqt5widgets5 libqt5xml5 graphviz libjsoncpp1
  dependencies:
    - build:ubuntu:rolling

# #################################################################################################
# stage: deploy

# -----------------------------------------------
# docker images

.docker: &docker_template
  stage: deploy
  image: alpine:3.9
  cache: {}  # disable
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_HOST: docker
    # ---------
    IMAGE_CLI: $CI_REGISTRY_IMAGE/cli
    IMAGE_GUI: $CI_REGISTRY_IMAGE/gui
    IMAGE_ALL: $CI_REGISTRY_IMAGE
    TAG_NIGHTLY: nightly
    TAG_RELEASE: $CI_COMMIT_REF_NAME
    TAG_LATEST:  latest
  services:
    - docker:dind
  before_script:
    - apk add --no-cache docker curl
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker info
  after_script:
    - curl -X POST -F token="$PY_ALIB_TOKEN" -F ref=master https://gitlab.fit.cvut.cz/api/v4/projects/11497/trigger/pipeline
  dependencies: []

deploy:docker:nightly: &docker_nightly_template
  <<: *docker_template
  <<: *config_run_only_nightly
  environment:
    name: docker-nightly
  script:
    - docker build --target=deploy-cli -f extra/docker/Dockerfile.master -t "$IMAGE_CLI:$TAG_NIGHTLY" .
    - docker build --target=deploy-all -f extra/docker/Dockerfile.master -t "$IMAGE_ALL:$TAG_NIGHTLY" .
    - docker tag "$IMAGE_ALL:$TAG_NIGHTLY" "$IMAGE_GUI:$TAG_NIGHTLY" # gui-version is now only a symlink to all-version
    - docker run "$IMAGE_CLI:$TAG_NIGHTLY" /usr/bin/aql2 --help
    - docker run "$IMAGE_GUI:$TAG_NIGHTLY" /usr/bin/aql2 --help
    - docker run "$IMAGE_ALL:$TAG_NIGHTLY" /usr/bin/aql2 --help
    - docker push "$IMAGE_CLI:$TAG_NIGHTLY"
    - docker push "$IMAGE_GUI:$TAG_NIGHTLY"
    - docker push "$IMAGE_ALL:$TAG_NIGHTLY"

deploy:docker:release:
  <<: *docker_template
  <<: *config_run_only_release
  environment:
    name: docker-production
  script:
    - docker build -f extra/docker/Dockerfile.master     -t "$IMAGE_ALL:$TAG_RELEASE" -t "$IMAGE_ALL:$TAG_LATEST" .
    - docker build -f extra/docker/Dockerfile.master-cli -t "$IMAGE_CLI:$TAG_RELEASE" -t "$IMAGE_CLI:$TAG_LATEST" .
    - docker tag "$IMAGE_ALL:$TAG_RELEASE" "$IMAGE_GUI:$TAG_RELEASE" # gui-version is now only a symlink to all-version
    - docker tag "$IMAGE_ALL:$TAG_LATEST"  "$IMAGE_GUI:$TAG_LATEST"  # gui-version is now only a symlink to all-version
    - docker run "$IMAGE_ALL:$TAG_RELEASE" /usr/bin/aql2 --help
    - docker run "$IMAGE_CLI:$TAG_RELEASE" /usr/bin/aql2 --help
    - docker run "$IMAGE_GUI:$TAG_RELEASE" /usr/bin/aql2 --help
    - docker push "$IMAGE_ALL:$TAG_LATEST"
    - docker push "$IMAGE_CLI:$TAG_LATEST"
    - docker push "$IMAGE_GUI:$TAG_LATEST"
    - docker push "$IMAGE_ALL:$TAG_RELEASE"
    - docker push "$IMAGE_CLI:$TAG_RELEASE"
    - docker push "$IMAGE_GUI:$TAG_RELEASE"

# -----------------------------------------------
# obs push

.obs: &obs_template
  stage: deploy
  image: gitlab.fit.cvut.cz:5000/algorithms-library-toolkit/ci-docker-images/osc:latest
  before_script:
    - echo -e "[general]\napiurl = https://api.opensuse.org\n\n[https://api.opensuse.org]\nuser = ${OBS_USER}\npass = ${OBS_PASS}\n" > ~/.oscrc
    - mkdir -p /obs_repo /distrofiles
  script:
    - extra/obs/make-distrofiles.sh ${OBS_MODE} /distrofiles/
    - cd /obs_repo && osc co ${OBS_PROJECT} ${OBS_PACKAGE}
    - cd ${OBS_PROJECT}/${OBS_PACKAGE}
    - mv /distrofiles/* .
    - osc addremove
    - osc ci -n -m "CI autocommit (${CI_COMMIT_SHA}, tag=${CI_COMMIT_TAG})"
  dependencies: []

deploy:obs:nightly:
  <<: *obs_template
  <<: *config_run_only_nightly
  environment:
    name: obs-nightly
    url: https://build.opensuse.org/package/show/home:algorithms-library/algorithms-library-all-nightly
  variables:
    OBS_PROJECT: home:algorithms-library
    OBS_PACKAGE: algorithms-library-all-nightly
    OBS_MODE: nightly

deploy:obs:release:
  <<: *obs_template
  <<: *config_run_only_release
  environment:
    name: obs-production
    url: https://build.opensuse.org/package/show/home:algorithms-library/algorithms-library
  variables:
    OBS_PROJECT: home:algorithms-library
    OBS_PACKAGE: algorithms-library
    OBS_MODE: release
