image: docker:latest

services:
  - name: docker:dind
    command: ["--insecure-registry=10.20.2.132:8083"]

variables:
  DOCKER_DRIVER: overlay
  SPRING_PROFILES_ACTIVE: gitlab-ci
  MAVEN_OPTS: -Dmaven.repo.local=/cache/maven.repository
  
stages:
#  - test
  - build
  - package
  - deploy
  

maven-package:
  image: 10.20.2.132:8083/kg_maven:3-jdk-8
  stage: build
  only:
    - ci
  script:
    - mvn package -e -U
  artifacts:
    paths:
      - m**/target/*.jar


docker-build-eureka:
  stage: package
  when: manual
  dependencies:
    - maven-package
  only:
    - ci
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker build  --file microservice-discovery-eureka/Dockerfile -t "$CI_REGISTRY/$CI_REGISTRY_IMAGE_EUREKA" ./microservice-discovery-eureka
    - docker push "$CI_REGISTRY/$CI_REGISTRY_IMAGE_EUREKA"

docker-build-config:
  stage: package
  when: manual
  dependencies:
    - maven-package
  only:
    - ci
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker build  --file microservice-config-server/Dockerfile -t "$CI_REGISTRY/$CI_REGISTRY_IMAGE_CONFIG" ./microservice-config-server
    - docker push "$CI_REGISTRY/$CI_REGISTRY_IMAGE_CONFIG"


docker-build-zuul:
  stage: package
  when: manual
  dependencies:
    - maven-package
  only:
    - ci
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker build  --file microservice-gateway-zuul/Dockerfile -t "$CI_REGISTRY/$CI_REGISTRY_IMAGE_ZUUL" ./microservice-gateway-zuul
    - docker push "$CI_REGISTRY/$CI_REGISTRY_IMAGE_ZUUL"


docker-build-auth:
  stage: package
  when: manual
  dependencies:
    - maven-package
  only:
    - ci
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker build  --file microservice-authorization-server/Dockerfile -t "$CI_REGISTRY/$CI_REGISTRY_IMAGE_AUTH" ./microservice-authorization-server
    - docker push "$CI_REGISTRY/$CI_REGISTRY_IMAGE_AUTH"

docker-build-admin:
  stage: package
  when: manual
  dependencies:
    - maven-package
  only:
    - ci
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker build  --file microservice-admin/Dockerfile -t "$CI_REGISTRY/$CI_REGISTRY_IMAGE_ADMIN" ./microservice-admin
    - docker push "$CI_REGISTRY/$CI_REGISTRY_IMAGE_ADMIN"


docker-build-adapter:
  stage: package
  when: manual
  dependencies:
    - maven-package
  only:
    - ci
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker build  --file microservice-adapter/Dockerfile -t "$CI_REGISTRY/$CI_REGISTRY_IMAGE_ADAPTER" ./microservice-adapter
    - docker push "$CI_REGISTRY/$CI_REGISTRY_IMAGE_ADAPTER"


docker-build-alram:
  stage: package
  when: manual
  dependencies:
    - maven-package
  only:
    - ci
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker build  --file microservice-alram/Dockerfile -t "$CI_REGISTRY/$CI_REGISTRY_IMAGE_ALRAM" ./microservice-alram
    - docker push "$CI_REGISTRY/$CI_REGISTRY_IMAGE_ALRAM"


docker-build-equipment:
  stage: package
  when: manual
  dependencies:
    - maven-package
  only:
    - ci
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker build  --file microservice-equipment/Dockerfile -t "$CI_REGISTRY/$CI_REGISTRY_IMAGE_EQUIPMENT" ./microservice-equipment
    - docker push "$CI_REGISTRY/$CI_REGISTRY_IMAGE_EQUIPMENT"


docker-build-gis:
  stage: package
  when: manual
  dependencies:
    - maven-package
  only:
    - ci
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker build  --file microservice-gis/Dockerfile -t "$CI_REGISTRY/$CI_REGISTRY_IMAGE_GIS" ./microservice-gis
    - docker push "$CI_REGISTRY/$CI_REGISTRY_IMAGE_GIS"


docker-build-report:
  stage: package
  when: manual
  dependencies:
    - maven-package
  only:
    - ci
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker build  --file microservice-report/Dockerfile -t "$CI_REGISTRY/$CI_REGISTRY_IMAGE_REPORT" ./microservice-report
    - docker push "$CI_REGISTRY/$CI_REGISTRY_IMAGE_REPORT"


docker-build-transfer:
  stage: package
  when: manual
  dependencies:
    - maven-package
  only:
    - ci
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker build  --file microservice-transfer/Dockerfile -t "$CI_REGISTRY/$CI_REGISTRY_IMAGE_TRANSFER" ./microservice-transfer
    - docker push "$CI_REGISTRY/$CI_REGISTRY_IMAGE_TRANSFER"

docker-run-eureka:
  stage: deploy
  when: manual
  image: 10.20.2.132:8083/kg_kroniak_ssh-client
  dependencies:
    - docker-build-eureka
  before_script:
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - echo "$SSH_PRIVATE_KEY" 
    - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
  only:
    - ci
  script:
    - ssh 10.20.2.139 "sh /opt/docker-compose-file/eureka-docker-api.sh"

docker-run-config:
  stage: deploy
  when: manual
  image: 10.20.2.132:8083/kg_kroniak_ssh-client
  dependencies:
    - docker-build-config
  before_script:
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - echo "$SSH_PRIVATE_KEY" 
    - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
  only:
    - ci
  script:
    - ssh 10.20.2.139 "sh /opt/docker-compose-file/config-docker-api.sh"

docker-run-zuul:
  stage: deploy
  when: manual
  image: 10.20.2.132:8083/kg_kroniak_ssh-client
  dependencies:
    - docker-build-zuul
  before_script:
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - echo "$SSH_PRIVATE_KEY" 
    - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
  only:
    - ci
  script:
    - ssh 10.20.2.139 "sh /opt/docker-compose-file/zuul-docker-api.sh"

docker-run-auth:
  stage: deploy
  when: manual
  image: 10.20.2.132:8083/kg_kroniak_ssh-client
  dependencies:
    - docker-build-auth
  before_script:
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - echo "$SSH_PRIVATE_KEY" 
    - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
  only:
    - ci
  script:
    - ssh 10.20.2.139 "sh /opt/docker-compose-file/auth-docker-api.sh"


docker-run-admin:
  stage: deploy
  when: manual
  image: 10.20.2.132:8083/kg_kroniak_ssh-client
  dependencies:
    - docker-build-admin
  before_script:
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - echo "$SSH_PRIVATE_KEY" 
    - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
  only:
    - ci
  script:
    - ssh 10.20.2.139 "sh /opt/docker-compose-file/admin-docker-api.sh"

docker-run-adapter:
  stage: deploy
  when: manual
  image: 10.20.2.132:8083/kg_kroniak_ssh-client
  dependencies:
    - docker-build-adapter
  before_script:
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - echo "$SSH_PRIVATE_KEY" 
    - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
  only:
    - ci
  script:
    - ssh 10.20.2.139 "sh /opt/docker-compose-file/adapter-docker-api.sh"

docker-run-alram:
  stage: deploy
  when: manual
  image: 10.20.2.132:8083/kg_kroniak_ssh-client
  dependencies:
    - docker-build-alram
  before_script:
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - echo "$SSH_PRIVATE_KEY" 
    - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
  only:
    - ci
  script:
    - ssh 10.20.2.139 "sh /opt/docker-compose-file/alram-docker-api.sh"

docker-run-equipment:
  stage: deploy
  when: manual
  image: 10.20.2.132:8083/kg_kroniak_ssh-client
  dependencies:
    - docker-build-equipment
  before_script:
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - echo "$SSH_PRIVATE_KEY" 
    - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
  only:
    - ci
  script:
    - ssh 10.20.2.139 "sh /opt/docker-compose-file/equipment-docker-api.sh"

docker-run-gis:
  stage: deploy
  when: manual
  image: 10.20.2.132:8083/kg_kroniak_ssh-client
  dependencies:
    - docker-build-gis
  before_script:
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - echo "$SSH_PRIVATE_KEY" 
    - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
  only:
    - ci
  script:
    - ssh 10.20.2.139 "sh /opt/docker-compose-file/gis-docker-api.sh"

docker-run-report:
  stage: deploy
  when: manual
  image: 10.20.2.132:8083/kg_kroniak_ssh-client
  dependencies:
    - docker-build-report
  before_script:
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - echo "$SSH_PRIVATE_KEY" 
    - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
  only:
    - ci
  script:
    - ssh 10.20.2.139 "sh /opt/docker-compose-file/report-docker-api.sh"

docker-run-transfer:
  stage: deploy
  when: manual
  image: 10.20.2.132:8083/kg_kroniak_ssh-client
  dependencies:
    - docker-build-transfer
  before_script:
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - echo "$SSH_PRIVATE_KEY" 
    - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
  only:
    - ci
  script:
    - ssh 10.20.2.139 "sh /opt/docker-compose-file/transfer-docker-api.sh"

