stages:
  - build
  - test
  - quality
  - deploy
  - publish

before_script:
  - export GNOW=$(date +"%Y%m%d")
  - export NOW=$(date +"%Y%m%d-%H%M%S")
  - export TAR_NAME_POSTFIX=$NOW
  - export SYS=`uname -s`
  - export CHAOS_LIB_HASH=`git log -n 1 --pretty="%h"`
  - export NPROC=$(nproc --ignore 1)
  - export ARCH=`uname -m`
  - export CHAOS_LIB_HASH=`git log -n 1 --pretty="%h"`;
  - export HEAPCHECK=strict

build_u_14_04_gcc49:
  tags:
    - docker
    - infncc
  stage: build
  image: baltig.infn.it:4567/bisegni/chaos-docker-compilation/ubuntu/14:gcc_49
  script:
    - git config --global user.name "Claudio Bisegni"
    - git config --global user.email "Claudio.Bisegni@lnf.infn.it"
    - tools/chaos_clean.sh
    - cmake  -DCHAOS_AGENT=OFF -DCHAOS_VERSION_MAJOR="$CI_COMMIT_REF_NAME.$CHAOS_LIB_HASH" -DCHAOS_VERSION_MINOR="$CI_JOB_STAGE.$CI_COMMIT_SHA" -DCHAOS_BUILD_ID=${CI_PIPELINE_ID} -DCHAOS_ARCHITECTURE_TEST=ON -DCHAOS_SANITIZER=TestFramework -DCMAKE_BUILD_TYPE=PROFILE .
    - make clean
    - /tmp/build-wrapper/build-wrapper-linux-x86-64 --out-dir bw-output make -j $NPROC install
  cache:
    paths:
        - /builds/chaos-lnf-control/chaosframework/config/CACHE/
  artifacts:
    paths:
        - chaos
        - ChaosMetadataService
        - chaos_metadata_service_client
        - chaos_micro_unit_toolkit
        - chaos_service_common
        - ccs
        - CHAOSFrameworkTests
        - bw-output
        - 'chaos-distrib-x86_64-Linux'
    expire_in: 8 hour
  only:
    refs:
      - web
    variables:
      - $BUILD_TYPE == "test"
  retry: 2

test_u_14_04_gcc49:
  dependencies:
    - build_u_14_04_gcc49
  tags:
    - docker
    - infncc
  stage: test
  image: baltig.infn.it:4567/bisegni/chaos-docker-compilation/ubuntu/14:gcc_49
  script:
    - git config --global user.name "Claudio Bisegni"
    - git config --global user.email "Claudio.Bisegni@lnf.infn.it"
    - apt-get -y update
    - apt-get -y install lcov gcovr
    - export PPROF_PATH=chaos-distrib-x86_64-Linux/bin/pprof
    - export LD_LIBRARY_PATH=/builds/chaos-lnf-control/chaosframework/chaos-distrib-x86_64-Linux/lib
    - export HEAPCHECK=normal
    - chaos-distrib-x86_64-Linux/bin/TestFramework --gtest_output=xml:report_test.xml --gtest_repeat=10 --gtest_break_on_failure
    - lcov -c -d chaos --output-file coverage.info
    - genhtml coverage.info --output-directory coverage_html
    - gcovr -k -r chaos --object-directory=chaos
    - /tmp/sonar-scanner/bin/sonar-scanner
      -Dsonar.branch.name=$CI_COMMIT_REF_NAME
      -Dsonar.projectKey=chaosframework
      -Dsonar.organization=bisegni-github
      -Dsonar.sources=chaos,ChaosMetadataService,chaos_metadata_service_client,chaos_micro_unit_toolkit,chaos_service_common,ccs
      -Dsonar.host.url=https://sonarcloud.io
      -Dsonar.login=369dba58f43885fc41db2f9076a67b36aa5b2584
      -Dsonar.cfamily.build-wrapper-output=bw-output
      -Dsonar.cfamily.threads=$NPROC
      -Dsonar.cfamily.gcov.reportsPath=.
      -Dsonar.tests=TestFramework
      -Dsonar.cxx.xunit.provideDetails=true
      -Dsonar.cxx.xunit.reportPath=report_test.xml
  artifacts:
    paths:
        - 'coverage_html'
    expire_in: 2 hour
  coverage: /^TOTAL.*\s+(\d+\%)$/
  only:
    refs:
      - web
    variables:
      - $BUILD_TYPE == "test"
  retry: 2

build_u_16_04_gcc5:
  tags:
    - docker
    - infncc
  stage: build
  image: baltig.infn.it:4567/bisegni/chaos-docker-compilation/ubuntu/16:llvm6
  script:
    - git config --global user.name "Claudio Bisegni"
    - git config --global user.email "Claudio.Bisegni@lnf.infn.it"
    - tools/chaos_clean.sh
    - cmake -DCHAOS_VERSION_MAJOR="$CI_COMMIT_REF_NAME.$CHAOS_LIB_HASH" -DCHAOS_VERSION_MINOR="$CI_JOB_STAGE.$CI_COMMIT_SHA" -DCHAOS_BUILD_ID=${CI_PIPELINE_ID} -DCHAOS_ARCHITECTURE_TEST=ON  -DCHAOS_SANITIZER=TestFramework .
    - make clean
    - make  -j $NPROC
    - make install
  cache:
    paths:
        - /builds/chaos-lnf-control/chaosframework/config/CACHE/
  artifacts:
    paths:
        - chaos
        - CHAOSFrameworkTests
        - 'chaos-distrib-x86_64-Linux'
    expire_in: 8 hour
  only:
    refs:
      - web
    variables:
      - $BUILD_TYPE == "test"
  retry: 2

test_u_16_04_gcc5_test:
  dependencies:
    - build_u_16_04_gcc5
  tags:
    - docker
    - infncc
  stage: test
  image: baltig.infn.it:4567/bisegni/chaos-docker-compilation/ubuntu/16:llvm6
  script:
  - git config --global user.name "Claudio Bisegni"
  - git config --global user.email "Claudio.Bisegni@lnf.infn.it"
  - export PPROF_PATH=chaos-distrib-x86_64-Linux/bin/pprof
  - export LD_LIBRARY_PATH=/builds/chaos-lnf-control/chaosframework/chaos-distrib-x86_64-Linux/lib
  - export HEAPCHECK=normal
  - chaos-distrib-x86_64-Linux/bin/TestFramework --gtest_repeat=10 --gtest_break_on_failure
  artifacts:
    paths:
        - 'chaos-distrib-x86_64-Linux'
    expire_in: 8 hour
  only:
    refs:
      - web
    variables:
      - $BUILD_TYPE == "test"
  retry: 2

build_u_18_04_gcc73:
  tags:
    - docker
    - infncc
  stage: build
  image: baltig.infn.it:4567/bisegni/chaos-docker-compilation/ubuntu/18:gcc7.3
  script:
    - git config --global user.name "Claudio Bisegni"
    - git config --global user.email "Claudio.Bisegni@lnf.infn.it"
    - apt -y update
    - apt -y install zlib1g-dev libcurl4-gnutls-dev
    - tools/chaos_clean.sh
    - cmake  -DCHAOS_PROMETHEUS=ON -DCHAOS_VERSION_MAJOR="$CI_COMMIT_REF_NAME.$CHAOS_LIB_HASH" -DCHAOS_VERSION_MINOR="$CI_JOB_STAGE.$CI_COMMIT_SHA" -DCHAOS_BUILD_ID=${CI_PIPELINE_ID} -DCHAOS_ARCHITECTURE_TEST=ON  -DUSE_GPT=ON .
    - make clean
    - make  -j $NPROC
    - make install
  cache:
    paths:
        - /builds/chaos-lnf-control/chaosframework/config/CACHE/
  artifacts:
    paths:
        - chaos
        - CHAOSFrameworkTests
        - 'chaos-distrib-x86_64-Linux'
    expire_in: 8 hour
  only:
    refs:
      - web
    variables:
      - $BUILD_TYPE == "test"
  retry: 2

test_u_18_04_gcc73:
  dependencies:
    - build_u_18_04_gcc73
  tags:
    - docker
    - infncc
  stage: test
  image: baltig.infn.it:4567/bisegni/chaos-docker-compilation/ubuntu/18:gcc7.3
  script:
  - git config --global user.name "Claudio Bisegni"
  - git config --global user.email "Claudio.Bisegni@lnf.infn.it"
  - export PPROF_PATH=chaos-distrib-x86_64-Linux/bin/pprof
  - export LD_LIBRARY_PATH=/builds/chaos-lnf-control/chaosframework/chaos-distrib-x86_64-Linux/lib
  - export HEAPCHECK=normal
  - chaos-distrib-x86_64-Linux/bin/TestFramework --gtest_repeat=10 --gtest_break_on_failure
  artifacts:
    paths:
        - 'chaos-distrib-x86_64-Linux'
    expire_in: 8 hour
  only:
    refs:
      - web
    variables:
      - $BUILD_TYPE == "test"
  retry: 2

build_dbg_c7:
  tags:
    - docker
    - infncc
  stage: build
  image: baltig.infn.it:4567/bisegni/chaos-docker-compilation/centos7:devtools7
  script:
    - git config --global user.name "Claudio Bisegni"
    - git config --global user.email "Claudio.Bisegni@lnf.infn.it"
    - tools/chaos_clean.sh
    - yum -y update
    - yum -y install libcurl-devel
    - cmake  -DCHAOS_PROMETHEUS=ON -DCHAOS_AGENT=OFF -DCHAOS_VERSION_MAJOR="$CI_COMMIT_REF_NAME.$CHAOS_LIB_HASH" -DCHAOS_VERSION_MINOR="$CI_JOB_STAGE.$CI_COMMIT_SHA" -DCHAOS_BUILD_ID=${CI_PIPELINE_ID} -DCHAOS_ARCHITECTURE_TEST=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo .
    - make clean
    - make  -j $NPROC
    - make install
  cache:
    paths:
        - /builds/chaos-lnf-control/chaosframework/config/CACHE/
  artifacts:
    paths:
        - chaos
        - CHAOSFrameworkTests
        - 'chaos-distrib-x86_64-Linux'
    expire_in: 8 hour
  only:
    refs:
      - web
    variables:
      - $BUILD_TYPE == "test"
  retry: 2

test_dbg_c7:
  dependencies:
    - build_dbg_c7
  tags:
    - docker
    - infncc
  stage: test
  image: baltig.infn.it:4567/bisegni/chaos-docker-compilation/centos7:devtools7
  script:
    - git config --global user.name "Claudio Bisegni"
    - git config --global user.email "Claudio.Bisegni@lnf.infn.it"
    - export PPROF_PATH=chaos-distrib-x86_64-Linux/bin/pprof
    - export LD_LIBRARY_PATH=/builds/chaos-lnf-control/chaosframework/chaos-distrib-x86_64-Linux/lib:/builds/chaos-lnf-control/chaosframework/chaos-distrib-x86_64-Linux/lib64
    - export HEAPCHECK=normal
    - chaos-distrib-x86_64-Linux/bin/TestFramework --gtest_repeat=10 --gtest_break_on_failure
  artifacts:
    paths:
        - 'chaos-distrib-x86_64-Linux'
    expire_in: 8 hour
  only:
    refs:
      - web
    variables:
      - $BUILD_TYPE == "test"
  retry: 2

# regression_c7:
#   dependencies:
#     - test_dbg_c7
#   allow_failure: true
#   tags:
#     - shared
#     - docker
#   stage: deploy
#   image: baltig.infn.it:4567/chaos-lnf-control/chaos_bundle_compilation:lite
#   script:
#     - git config --global user.name "Claudio Bisegni"
#     - git config --global user.email "Claudio.Bisegni@lnf.infn.it"
#     - echo "Prearing deploy for chaos-distrib-x86_64-centos7_$TAR_NAME_POSTFIX.tar.gz"
#     - tar cfz chaos-distrib-x86_64-centos7_$TAR_NAME_POSTFIX.tar.gz chaos-distrib-x86_64-Linux
#     - ls -la
#     - echo "Ensure directory for short timed branch"
#     - ssh chaosweb@opensource.lnf.infn.it "mkdir -p /var/www/html/binary/chaos/short_branch/x86_64/centos/7"
#     - scp chaos-distrib-x86_64-centos7_$TAR_NAME_POSTFIX.tar.gz chaosweb@opensource.lnf.infn.it:/var/www/html/binary/chaos/short_branch/x86_64/centos/7/chaos-distrib-$TAR_NAME_POSTFIX.tar.gz
#     - ssh chaosweb@opensource.lnf.infn.it "rm -f /var/www/html/binary/chaos/short_branch/x86_64/centos/7/*"
#     - ssh chaosweb@opensource.lnf.infn.it "ln -sf /var/www/html/binary/chaos/short_branch/x86_64/centos/7/chaos-distrib-$TAR_NAME_POSTFIX.tar.gz /var/www/html/binary/chaos/short_branch/x86_64/centos/7/latest.tar.gz"
#     - curl -X POST -F token=d42ffdee47536fd7cfac5ec4df9af2 -F "variables[CHAOS_DISTRIB_NAME]=short_branch/x86_64/centos/7/latest.tar.gz" -F "ref=$REGRESSION_BRANCH" https://baltig.infn.it/api/v4/projects/1328/trigger/pipeline
#   only:
#     refs:
#       - web
#     variables:
#       - $BUILD_TYPE == "test"
#       - $TARGET_TYPE == "regression"
#   retry: 2

build_rel_c7:
  tags:
    - docker
    - infncc
  stage: build
  image: baltig.infn.it:4567/bisegni/chaos-docker-compilation/centos7:devtools7
  script:
    - git config --global user.name "Claudio Bisegni"
    - git config --global user.email "Claudio.Bisegni@lnf.infn.it"
    - tools/chaos_clean.sh
    - yum -y update
    - yum -y install libcurl-devel
    - cmake  -DCHAOS_PROMETHEUS=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCHAOS_VERSION_MAJOR="$CI_COMMIT_REF_NAME.$CHAOS_LIB_HASH" -DCHAOS_VERSION_MINOR="$CI_JOB_STAGE.$CI_COMMIT_SHA" -DCHAOS_BUILD_ID=${CI_PIPELINE_ID} -DCHAOS_ARCHITECTURE_TEST=ON .
    - make clean
    - make  -j $NPROC
    - make install
  cache:
    paths:
        - /builds/chaos-lnf-control/chaosframework/config/CACHE/
  artifacts:
    paths:
        - chaos
        - CHAOSFrameworkTests
        - 'chaos-distrib-x86_64-Linux'
    expire_in: 8 hour
  only:
    refs:
      - web
    variables:
      - $BUILD_TYPE == "deploy"
  retry: 2

test_rel_c7:
  dependencies:
    - build_rel_c7
  tags:
    - docker
    - infncc
  stage: test
  image: baltig.infn.it:4567/bisegni/chaos-docker-compilation/centos7:devtools7
  script:
    - git config --global user.name "Claudio Bisegni"
    - git config --global user.email "Claudio.Bisegni@lnf.infn.it"
    - export PPROF_PATH=chaos-distrib-x86_64-Linux/bin/pprof
    - export LD_LIBRARY_PATH=/builds/chaos-lnf-control/chaosframework/chaos-distrib-x86_64-Linux/lib
    - export HEAPCHECK=normal
    - chaos-distrib-x86_64-Linux/bin/TestFramework --gtest_repeat=10 --gtest_break_on_failure
  artifacts:
    paths:
        - 'chaos-distrib-x86_64-Linux'
    expire_in: 8 hour
  only:
    refs:
      - web
    variables:
      - $BUILD_TYPE == "deploy"
  retry: 2

deploy_rel_c7:
  dependencies:
    - build_rel_c7
  allow_failure: false
  tags:
    - docker
    - infncc
  stage: deploy
  image: baltig.infn.it:4567/chaos-lnf-control/chaos_bundle_compilation:lite
  script:
    - git config --global user.name "Claudio Bisegni"
    - git config --global user.email "Claudio.Bisegni@lnf.infn.it"
    - echo "Prearing deploy for chaos-distrib-x86_64-centos7_$TAR_NAME_POSTFIX.tar.gz"
    - tar cfz chaos-distrib-x86_64-centos7_$TAR_NAME_POSTFIX.tar.gz chaos-distrib-x86_64-Linux
    - ls -la
    - scp chaos-distrib-x86_64-centos7_$TAR_NAME_POSTFIX.tar.gz chaosweb@opensource.lnf.infn.it:/var/www/html/binary/chaos/$BUILD_ENVIRONMENT/x86_64/centos/7/chaos-distrib-$TAR_NAME_POSTFIX.tar.gz
    - ssh chaosweb@opensource.lnf.infn.it "rm -f /var/www/html/binary/chaos/$BUILD_ENVIRONMENT/x86_64/centos/7/latest.tar.gz"
    - ssh chaosweb@opensource.lnf.infn.it "ln -sf /var/www/html/binary/chaos/$BUILD_ENVIRONMENT/x86_64/centos/7/chaos-distrib-$TAR_NAME_POSTFIX.tar.gz /var/www/html/binary/chaos/$BUILD_ENVIRONMENT/x86_64/centos/7/latest.tar.gz"
    - curl -X POST -F token=d42ffdee47536fd7cfac5ec4df9af2 -F "ref=$CI_COMMIT_REF_NAME" https://baltig.infn.it/api/v4/projects/1328/trigger/pipeline
  environment:
    name: $BUILD_ENVIRONMENT
  only:
    refs:
      - web
    variables:
      - $BUILD_TYPE == "deploy"
  retry: 2

build_u_14_04_gcc48_c98:
  tags:
    - docker
    - infncc
  stage: build
  image: baltig.infn.it:4567/bisegni/chaos-docker-compilation/ubuntu/14:gcc
  script:
    - git config --global user.name "Claudio Bisegni"
    - git config --global user.email "Claudio.Bisegni@lnf.infn.it"
    - tools/chaos_clean.sh
    - cmake -DCHAOS_ENABLE_C11=OFF -DCHAOS_AGENT=OFF -DCHAOS_MDS=OFF -DCHAOS_ARCHITECTURE_TEST=ON  -DCHAOS_VERSION_MAJOR="$CI_COMMIT_REF_NAME.$CHAOS_LIB_HASH" -DCHAOS_VERSION_MINOR="$CI_JOB_STAGE.$CI_COMMIT_SHA" -DCHAOS_BUILD_ID=${CI_PIPELINE_ID} -DCHAOS_ARCHITECTURE_TEST=ON -DCHAOS_SANITIZER=TestFramework -DCMAKE_BUILD_TYPE=PROFILE .
    - make clean
    - make  -j $NPROC
    - make install
  cache:
    paths:
        - /builds/chaos-lnf-control/chaosframework/config/CACHE/
  artifacts:
    paths:
        - 'chaos-distrib-x86_64-Linux'
    expire_in: 8 hour
  only:
    refs:
      - web
    variables:
      - $BUILD_TYPE == "test"
  retry: 2

test_u_14_04_gcc48_c98:
  dependencies:
    - build_u_14_04_gcc48_c98
  tags:
    - docker
    - infncc
  stage: test
  image: baltig.infn.it:4567/bisegni/chaos-docker-compilation/ubuntu/14:gcc
  script:
    - git config --global user.name "Claudio Bisegni"
    - git config --global user.email "Claudio.Bisegni@lnf.infn.it"
    - export PPROF_PATH=chaos-distrib-x86_64-Linux/bin/pprof
    - export LD_LIBRARY_PATH=/builds/chaos-lnf-control/chaosframework/chaos-distrib-x86_64-Linux/lib
    - export HEAPCHECK=normal
    - chaos-distrib-x86_64-Linux/bin/TestFramework --gtest_repeat=10 --gtest_break_on_failure
  only:
    refs:
      - web
    variables:
      - $BUILD_TYPE == "test"
  retry: 2

build_arm:
  tags:
    - docker
    - infncc
  stage: build
  image: baltig.infn.it:4567/bisegni/chaos-docker-compilation:latest
  script:
    - git config --global user.name "Claudio Bisegni"
    - git config --global user.email "Claudio.Bisegni@lnf.infn.it"
    - tools/chaos_clean.sh
    - cmake -DCHAOS_TARGET=armhf -DCHAOS_AGENT=OFF -DCHAOS_MDS=OFF .
    - make clean
    - make  -j $NPROC
    - make install
  cache:
    paths:
        - /builds/chaos-lnf-control/chaosframework/config/CACHE/
  artifacts:
    paths:
        - 'chaos-distrib-armhf'
    expire_in: 8 hour
  only:
    refs:
      - web
    variables:
      - $BUILD_TYPE == "test"
  retry: 2

# build_x86_64_llvm:
#   tags:
#     - chaos
#     - docker
#     - linux
#     - fast
#   stage: build
#   image: baltig.infn.it:4567/bisegni/chaos-docker-compilation/llvm_coverity_u1710
#   script:
#     - git config --global user.name "Claudio Bisegni"
#     - git config --global user.email "Claudio.Bisegni@lnf.infn.it"
#     - tools/chaos_clean.sh
#     - cmake -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCHAOS_ARCHITECTURE_TEST=ON  -DUSE_GPT=ON .
#     - make clean
#     - make  -j $NPROC
#     - make install
#   cache:
#     key: llvm_x86_64_$CI_BUILD_REF_NAME
#     paths:
#         - /builds/chaos-lnf-control/chaosframework/config/CACHE/
#   artifacts:
#     paths:
#         - chaos
#         - CHAOSFrameworkTests
#         - 'chaos-distrib-x86_64-Linux'
#     expire_in: 8 hour
#   only:
#     refs:
#       - web
#     variables:
#       - $BUILD_TYPE == "test"
#   retry: 2

# test_x86_64_llvm:
#   tags:
#     - chaos
#     - docker
#     - linux
#     - fast
#   stage: test
#   image: baltig.infn.it:4567/bisegni/chaos-docker-compilation/llvm_coverity_u1710
#   dependencies:
#     - build_x86_64_llvm
#   script:
#     - git config --global user.name "Claudio Bisegni"
#     - git config --global user.email "Claudio.Bisegni@lnf.infn.it"
#     - export PPROF_PATH=chaos-distrib-x86_64-Linux/bin/pprof
#     - export LD_LIBRARY_PATH=/builds/chaos-lnf-control/chaosframework/chaos-distrib-x86_64-Linux/lib
#     - export HEAPCHECK=normal
#     - chaos-distrib-x86_64-Linux/bin/TestFramework --gtest_repeat=10 --gtest_break_on_failure
#   only:
#     refs:
#       - web
#     variables:
#       - $BUILD_TYPE == "test"
#   retry: 2

exp_llvm_scan_coverity:
  tags:
    - docker
    - infncc
  stage: build
  image: baltig.infn.it:4567/bisegni/chaos-docker-compilation/llvm_coverity_u1710
  script:
    - git config --global user.name "Claudio Bisegni"
    - git config --global user.email "Claudio.Bisegni@lnf.infn.it"
    - mkdir -p /tmp/cov-int
    - cmake -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCHAOS_ARCHITECTURE_TEST=ON .
    - make clean
    - /tmp/coverity/bin/cov-build --dir /tmp/cov-int make -j 4
    - cd /tmp
    - tar czvf chaos.tgz cov-int
    - curl --form token=i9B9MI9n53zWdx2iSS4Qvg --form email=bisegni@lnf.infn.it --form file=@/tmp/chaos.tgz --form version="1.0.0" --form description="Beta"  https://scan.coverity.com/builds?project=%21CHAOS+Control+System
  cache:
    paths:
        - /builds/chaos-lnf-control/chaosframework/config/CACHE/
  only:
    refs:
      - schedules
      - web
    variables:
      - $BUILD_TYPE == "coverity"
  retry: 2

pages:
  stage: publish
  dependencies:
    - test_u_14_04_gcc49
  script:
    - mv coverage_html public
    - ls -la
  artifacts:
    paths:
      - public
    expire_in: 7 days
  only:
    refs:
      - web
    variables:
      - $BUILD_TYPE == "test"
