include:
  - template: Code-Quality.gitlab-ci.yml
  - template: Dependency-Scanning.gitlab-ci.yml
  - template: License-Scanning.gitlab-ci.yml
  - template: SAST.gitlab-ci.yml
  - template: Secret-Detection.gitlab-ci.yml

stages:
  - build
  - test
  - dockerize
  - python-test
  - deploy

build-mainnet:
  stage: build
  script:
    - rm -rf .git/modules/docs .git/modules/libraries/fc ./docs ./libraries/fc
    - git submodule sync
    - git submodule update --init --recursive
    - rm -rf build
    - mkdir build
    - cd build
    - cmake -DCMAKE_BUILD_TYPE=Release ..
    - make -j$(nproc)
  artifacts:
    untracked: true
    paths:
      - build/libraries/
      - build/programs/
      - build/tests/
  tags:
    - builder

test-mainnet:
  stage: test
  dependencies:
    - build-mainnet
  script:
    - ./build/libraries/fc/tests/all_tests
    - ./build/tests/betting_test --log_level=message
    - ./build/tests/chain_test --log_level=message
    - ./build/tests/cli_test --log_level=message
  tags:
    - builder

dockerize-mainnet:
  stage: dockerize
  variables:
    IMAGE: $CI_REGISTRY_IMAGE/mainnet/$CI_COMMIT_REF_SLUG:$CI_COMMIT_SHA
  before_script:
    - docker info
    - docker builder prune -a -f
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - docker build --no-cache -t $IMAGE .
    - docker push $IMAGE
  after_script:
    - docker rmi $IMAGE
  tags:
    - builder
  timeout:
    3h

build-testnet:
  stage: build
  script:
    - rm -rf .git/modules/docs .git/modules/libraries/fc ./docs ./libraries/fc
    - git submodule sync
    - git submodule update --init --recursive
    - rm -rf build
    - mkdir build
    - cd build
    - cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_PEERPLAYS_TESTNET=1 ..
    - make -j$(nproc)
  artifacts:
    untracked: true
    paths:
      - build/libraries/
      - build/programs/
      - build/tests/
  when: manual
  tags:
    - builder

deploy-testnet:
  stage: deploy
  dependencies:
    - build-testnet
  script:
    - sudo systemctl stop witness
    - rm $WORK_DIR/peerplays/witness_node || true
    - cp build/programs/witness_node/witness_node $WORK_DIR/peerplays/
    - sudo systemctl restart witness
  rules:
    - if: $CI_COMMIT_BRANCH == "master"
      when: always
  environment:
    name: devnet
    url: $DEVNET_URL
  tags: 
    - devnet

test-testnet:
  stage: test
  dependencies:
    - build-testnet
  script:
    - ./build/libraries/fc/tests/all_tests
    - ./build/tests/betting_test --log_level=message
    - ./build/tests/chain_test --log_level=message
    - ./build/tests/cli_test --log_level=message
  tags:
    - builder
  when:
    manual
  timeout:
    1h

dockerize-testnet:
  stage: dockerize
  variables:
    IMAGE: $CI_REGISTRY_IMAGE/testnet/$CI_COMMIT_REF_SLUG:$CI_COMMIT_SHA
  before_script:
    - docker info
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - docker build --no-cache -t $IMAGE .
    - docker push $IMAGE
  after_script:
    - docker rmi $IMAGE
  tags:
    - builder
  when:
    manual
  timeout:
    3h

test-e2e:
  stage: python-test
  variables:
    IMAGE: $CI_REGISTRY_IMAGE/mainnet/$CI_COMMIT_REF_SLUG:$CI_COMMIT_SHA
  before_script:
    - docker info
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - git clone https://gitlab.com/PBSA/tools-libs/peerplays-utils.git
    - cd peerplays-utils/peerplays-qa-environment
    - git checkout origin/feature/python-e2e-tests-for-CI
    - cd e2e-tests/
    - python3 -m venv venv
    - source venv/bin/activate
    - pip3 install -r requirements.txt
    - docker-compose down --remove-orphans
    - docker ps -a
    - docker pull $IMAGE
    - docker tag $IMAGE peerplays-base:latest
    - docker image ls -a
    - docker-compose build
    - python3 main.py --start all
    - docker ps -a
    - python3 -m pytest test_btc_init_state.py test_hive_inital_state.py test_pp_inital_state.py
    - python3 main.py --stop
    - deactivate
    - docker ps -a
  after_script:
    - docker rmi $(docker images -a | grep -v 'hive-for-peerplays\|ethereum-for-peerplays\|bitcoin-for-peerplays\|ubuntu-for-peerplays' | awk '{print $3}')
  tags:
    - python-tests
  when:
    manual
