before_script:
  - 'which ssh-agent || ( yum install -qq openssh-client )'
  - eval $(ssh-agent -s)
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - ssh-keyscan -t rsa 192.168.1.201 >> ~/.ssh/known_hosts
  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
  - whoami
  - unset SSH_ASKPASS

variables:
  QUALITY_GATE: "OK"

stages:
  - checkout_code
  - compile
  - sonnar_scanner
  - check_quality_gate
  - docker_build
  - docker_up
  - docker_push
  - send_email

checkout_code:
  stage: checkout_code
  only:
    - master
    - merge_request
  script:
    - cd /root/GitlabRunner/repo/spring-ci-advanced
    - git checkout
    - git pull

compile:
  stage: compile
  only:
    - master
    - merge_request
  script:
    - cd /root/GitlabRunner/repo/spring-ci-advanced
    - ls
    - chmod +x ./build-test.sh
    - ./build-test.sh compile

sonnar_scanner:
  stage: sonnar_scanner
  only:
    - master
    - merge_requests
  script:
    - cd /root/GitlabRunner/repo/spring-ci-advanced
    - ls
    - chmod +x ./build-test.sh
    - ./build-test.sh sonar-scan

check_quality_gate:
  stage: check_quality_gate
  only:
    - master
    - merge_requests
  script:
    - cd /root/GitlabRunner/repo/spring-ci-advanced
    - ls
    - chmod +x ./build-test.sh
    - export QUALITY_GATE=$(./build-test.sh quality-gate)
    - echo $QUALITY_GATE
    - echo "Sonar statistic result at --> http://192.168.1.210:9000/dashboard?id=spring-ci-advanced"
    - sh ./stop_job.sh # force pipeline exit and fail job if QUALITY GATE is FAIL

docker_build:
  stage: docker_build
  only:
    - master
  #    - merge_requesttt
  script:
    - cd /root/GitlabRunner/repo/spring-ci-advanced
    - ls
    - chmod +x ./build-test.sh
    - ./build-test.sh docker-build

docker_up:
  stage: docker_up
  only:
    - master
  #    - merge_request
  script:
    - cd /root/GitlabRunner/repo/spring-ci-advanced
    - ls
    - chmod +x ./build-test.sh
    - ./build-test.sh docker-up

docker_push:
  stage: docker_push
  only:
    - master
  #    - merge_request
  script:
    - cd /root/GitlabRunner/repo/spring-ci-advanced
    - ls
    - chmod +x ./build-test.sh
    - ./build-test.sh docker-push

send_email:
  stage: send_email
  only:
    - master
    - merge_requests
  script:
    - cd /root/GitlabRunner/repo/spring-ci-advanced
    - ls
    - chmod +x ./build-test.sh
    - ./build-test.sh send-email