stages:
  - build_dev
  - build_test
  - build_prod
  - deploy_to_test
  - run_tests
  - deploy_node_to_prod
  - deploy_other_nodes_to_prod

variables:
    DOCKER_DRIVER: overlay2

build_dev:
  stage: build_dev
  script:
    - docker login -u "gitlab-ci-token" -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
    - docker build -t ${CI_REGISTRY_IMAGE}/dev:${CI_PIPELINE_ID} .
    - docker push ${CI_REGISTRY_IMAGE}/dev:${CI_PIPELINE_ID}
  environment:
    name: ${CI_COMMIT_REF_NAME}
    url: https://xvideos.com
  only:
    - dev
  tags:
    - buidl1-dind

build_test:
  stage: build_test
  script:
    - docker login -u "gitlab-ci-token" -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
    - docker build -t ${CI_REGISTRY_IMAGE}/test:${CI_PIPELINE_ID} .
    - docker push ${CI_REGISTRY_IMAGE}/test:${CI_PIPELINE_ID}
  environment:
    name: ${CI_COMMIT_REF_NAME}
    url: https://xvideos.com
  only:
    - test
  tags:
    - buidl1-dind

build_prod:
  stage: build_prod
  script:
    - docker login -u "gitlab-ci-token" -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
    - docker build -t ${CI_REGISTRY_IMAGE}/prod:${CI_PIPELINE_ID} .
    - docker push ${CI_REGISTRY_IMAGE}/prod:${CI_PIPELINE_ID}
  environment:
    name: ${CI_COMMIT_REF_NAME}
    url: https://xvideos.com
  only:
    - release
  tags:
    - buidl1-dind

deploy_to_test:
  stage: deploy_to_test
  script:
    - sed 's|_IMAGE_NAME_|'"${CI_REGISTRY_IMAGE}/test"'|g; s|_VERSION_|'"${CI_PIPELINE_ID}"'|g' app.tpl.yml > app.yml; cat app.yml
    - wget https://storage.googleapis.com/kubernetes-release/release/v1.13.3/bin/linux/amd64/kubectl && chmod +x ./kubectl
    - wget https://github.com/garethr/kubeval/releases/download/0.7.3/kubeval-linux-amd64.tar.gz && tar xvf kubeval-linux-amd64.tar.gz && ./kubeval app.yml
    - ./kubectl apply -f app.yml
  environment:
    name: ${CI_COMMIT_REF_NAME}
    url: https://xvideos.com
  only:
    - test
  tags:
    - buidl1-dind

run_test:
  stage: run_tests
  script:
    - docker build -f Dockerfile-tests .
  allow_failure: true
  environment:
    name: ${CI_COMMIT_REF_NAME}
    url: https://xvideos.com
  only:
    - test1
  tags:
    - buidl1-dind

deploy_node_to_prod:
  stage: deploy_node_to_prod
  script:
    - cd ~/infra && git reset --hard HEAD && git pull
    - ansible-playbook -i ~/infra/ansible/inventory/hosts ~/infra/ansible/playbooks/cloud.yml -e "CI_PIPELINE_ID=${CI_PIPELINE_ID}" -t "destream-blockchain" -u build1-shell
  environment:
    name: ${CI_COMMIT_REF_NAME}
    url: https://xvideos.com
  only:
    - release
  tags:
    - buidl1-shell

deploy_other_nodes_to_prod:
  stage: deploy_other_nodes_to_prod
  image: docker:stable-git
#  services:
#    - docker:dind
  script:
    - sed 's|_IMAGE_NAME_|'"${CI_REGISTRY_IMAGE}/prod"'|g; s|_VERSION_|'"${CI_PIPELINE_ID}"'|g' app-dst-client.tpl.yml > app-dst-client.yml; cat app-dst-client.yml
    - sed 's|_IMAGE_NAME_|'"${CI_REGISTRY_IMAGE}/prod"'|g; s|_VERSION_|'"${CI_PIPELINE_ID}"'|g' app-nako-scanner.tpl.yml > app-nako-scanner.yml; cat app-nako-scanner.yml
    - wget https://storage.googleapis.com/kubernetes-release/release/v1.13.3/bin/linux/amd64/kubectl && chmod +x ./kubectl
    - wget https://github.com/garethr/kubeval/releases/download/0.7.3/kubeval-linux-amd64.tar.gz && tar xvf kubeval-linux-amd64.tar.gz 
    - ./kubeval app-dst-client.yml
    - ./kubeval app-nako-scanner.yml
    - ./kubectl apply -f app-dst-client.yml
    - ./kubectl apply -f app-nako-scanner.yml
  environment:
    name: ${CI_COMMIT_REF_NAME}
    url: https://xvideos.com
  only:
    - release
  tags:
    - buidl1-dind
