---
stages:
  - preperations
  - test
  - build
  - postbuild_test
  - deploy_api
  - deploy_web

variables:
  ANSIBLE_IMAGE_TAG: "${CI_REGISTRY_IMAGE}/ansible:latest"
  API_IMAGE_TAG: "${CI_REGISTRY_IMAGE}/api:${CI_COMMIT_SHORT_SHA}"
  CLIENT_IMAGE_TAG: "${CI_REGISTRY_IMAGE}/client:${CI_COMMIT_SHORT_SHA}"
  SERVER_IMAGE_TAG: "${CI_REGISTRY_IMAGE}/server:${CI_COMMIT_SHORT_SHA}"
  FRONTEND_PATH: ${CI_PROJECT_DIR}/client
  DEPLOY_PROJECT_NAME: "selfservice"
  WEB_DOMAIN: "selfservice.example.org"
  TRAEFIK_VERSION: "2.11"


.ansible-template:
  before_script:
    - chmod 0640 ${CI_PROJECT_DIR} -R
    - chmod 0600 ${PROJECT_SSH_KEY}

build-debian-ansible:
  image: docker:latest
  stage: preperations
  when: manual
  services:
    - docker:dind

  script:
    - cd ansible/debian
    - docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
    - docker build -t ${CI_REGISTRY_IMAGE}/debian-ansible:${CI_COMMIT_SHORT_SHA} .
    - docker tag ${CI_REGISTRY_IMAGE}/debian-ansible:${CI_COMMIT_SHORT_SHA} ${CI_REGISTRY_IMAGE}/debian-ansible:latest
    - docker tag ${CI_REGISTRY_IMAGE}/debian-ansible:${CI_COMMIT_SHORT_SHA} ${ANSIBLE_IMAGE_TAG}
    - docker push ${CI_REGISTRY_IMAGE}/debian-ansible:${CI_COMMIT_SHORT_SHA}
    - docker push ${CI_REGISTRY_IMAGE}/debian-ansible:latest
    - docker push ${ANSIBLE_IMAGE_TAG}

build-alma-ansible:
  image: docker:latest
  stage: preperations
  when: manual
  services:
    - docker:dind

  script:
    - cd ansible/alma
    - docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}   
    - docker build -t ${CI_REGISTRY_IMAGE}/alma-ansible:${CI_COMMIT_SHORT_SHA} .
    - docker tag ${CI_REGISTRY_IMAGE}/alma-ansible:${CI_COMMIT_SHORT_SHA} ${CI_REGISTRY_IMAGE}/alma-ansible:latest
    - docker tag ${CI_REGISTRY_IMAGE}/alma-ansible:${CI_COMMIT_SHORT_SHA} ${ANSIBLE_IMAGE_TAG}
    - docker push ${CI_REGISTRY_IMAGE}/alma-ansible:${CI_COMMIT_SHORT_SHA}
    - docker push ${CI_REGISTRY_IMAGE}/alma-ansible:latest
    - docker push ${ANSIBLE_IMAGE_TAG}

build-python-ansible:
  image: docker:latest
  stage: preperations
  when: manual
  services:
    - docker:dind

  script:
    - cd ansible/python
    - docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}   
    - docker build -t ${CI_REGISTRY_IMAGE}/python-ansible:${CI_COMMIT_SHORT_SHA} .
    - docker tag ${CI_REGISTRY_IMAGE}/python-ansible:${CI_COMMIT_SHORT_SHA} ${CI_REGISTRY_IMAGE}/python-ansible:latest
    - docker tag ${CI_REGISTRY_IMAGE}/python-ansible:${CI_COMMIT_SHORT_SHA} ${ANSIBLE_IMAGE_TAG}
    - docker push ${CI_REGISTRY_IMAGE}/python-ansible:${CI_COMMIT_SHORT_SHA}
    - docker push ${CI_REGISTRY_IMAGE}/python-ansible:latest
    - docker push ${ANSIBLE_IMAGE_TAG}

build-api:
  image: docker:latest
  stage: build
  when: manual
  services:
    - docker:dind

  script:
    - cd api
    - docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
    - docker build -t ${API_IMAGE_TAG} .
    - docker push ${API_IMAGE_TAG}

build-server:
  image: docker:latest
  stage: build
  when: manual
  services:
    - docker:dind

  script:
    - cd server
    - docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
    - docker build -t ${SERVER_IMAGE_TAG} .
    - docker push ${SERVER_IMAGE_TAG}

build-client:
  image: docker:latest
  stage: build
  when: manual
  services:
    - docker:dind

  script:
    - cd client
    - docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
    - docker build -t ${CLIENT_IMAGE_TAG} .
    - docker push ${CLIENT_IMAGE_TAG}

test-client:
  image: docker:latest
  stage: test
  allow_failure: true
  when: manual
  services:
    - docker:dind

  script:
    - cd client
    - docker build -t ${CLIENT_IMAGE_TAG} -f Dockerfile.test .

test-server:
  image: docker:latest
  stage: test
  allow_failure: true
  when: manual
  services:
    - docker:dind

  script:
    - cd server
    - docker build -t ${CLIENT_IMAGE_TAG} -f Dockerfile.test .

.deploy-api:
  extends: .ansible-template
  image: ${ANSIBLE_IMAGE_TAG}
  variables:
    HOSTS_LIMIT: all
  stage: deploy_api
  when: manual
  script:
    - |
      ansible-playbook --limit ${HOSTS_LIMIT} --inventory inventory deploy-api.yml --private-key ${PROJECT_SSH_KEY} \
        --extra-vars project_name=${DEPLOY_PROJECT_NAME}_api --extra-vars config_dir=/etc/app-${DEPLOY_PROJECT_NAME}-api \
        --extra-vars registry_url=${CI_REGISTRY}  --extra-vars registry_username=${CI_REGISTRY_USER} \
        --extra-vars registry_password=${CI_REGISTRY_PASSWORD} --extra-vars api_image=${API_IMAGE_TAG}

deploy-api-201:
  extends: .deploy-api
  variables:
    HOSTS_LIMIT: 172.17.200.101

deploy-api-202:
  extends: .deploy-api
  variables:
    HOSTS_LIMIT: 172.17.200.102

deploy-api-203:
  extends: .deploy-api
  variables:
    HOSTS_LIMIT: 172.17.200.103

deploy-dev-api:
  extends: .deploy-api
  variables:
    HOSTS_LIMIT: 172.17.200.104
  script:
    - |
      ansible-playbook --limit ${HOSTS_LIMIT} --inventory inventory deploy-api.yml --private-key ${PROJECT_SSH_KEY} \
        --extra-vars project_name=${DEPLOY_PROJECT_NAME}_api --extra-vars config_dir=/etc/app-${DEPLOY_PROJECT_NAME}-dev-api \
        --extra-vars registry_url=${CI_REGISTRY}  --extra-vars registry_username=${CI_REGISTRY_USER} \
        --extra-vars registry_password=${CI_REGISTRY_PASSWORD} --extra-vars api_image=${API_IMAGE_TAG}
    
.deploy-web:
  extends: .ansible-template
  image: ${ANSIBLE_IMAGE_TAG}
  stage: deploy_web
  when: manual
  variables:
    HOSTS_LIMIT: all
  script:
    - |
      ansible-playbook --limit ${HOSTS_LIMIT} --inventory inventory deploy-web.yml --private-key ${PROJECT_SSH_KEY} \
        --extra-vars project_name=${DEPLOY_PROJECT_NAME}-web --extra-vars config_dir=/etc/app-${DEPLOY_PROJECT_NAME} \
        --extra-vars registry_url=${CI_REGISTRY}  --extra-vars registry_username=${CI_REGISTRY_USER} \
        --extra-vars registry_password=${CI_REGISTRY_PASSWORD} --extra-vars client_image=${CLIENT_IMAGE_TAG} \
        --extra-vars server_image=${SERVER_IMAGE_TAG} \
        --extra-vars project_domain=${WEB_DOMAIN}

deploy-web-231:
  extends: .deploy-web
  variables:
        HOSTS_LIMIT: 172.17.200.111

deploy-web-232:
  extends: .deploy-web
  variables:
        HOSTS_LIMIT: 172.17.200.112

deploy-web-233:
  extends: .deploy-web
  variables:
        HOSTS_LIMIT: 172.17.200.113

deploy-dev-web:
  extends: .deploy-web
  variables:
        HOSTS_LIMIT: 172.17.200.114
  script:
    - |
      ansible-playbook --limit ${HOSTS_LIMIT} --inventory inventory deploy-web.yml --private-key ${PROJECT_SSH_KEY} \
        --extra-vars project_name=${DEPLOY_PROJECT_NAME}-web --extra-vars config_dir=/etc/app-${DEPLOY_PROJECT_NAME}-dev \
        --extra-vars registry_url=${CI_REGISTRY}  --extra-vars registry_username=${CI_REGISTRY_USER} \
        --extra-vars registry_password=${CI_REGISTRY_PASSWORD} --extra-vars client_image=${CLIENT_IMAGE_TAG} \
        --extra-vars server_image=${SERVER_IMAGE_TAG} \
        --extra-vars project_domain=$dev-{WEB_DOMAIN}



deploy-traefik:
  extends: .ansible-template
  image: ${ANSIBLE_IMAGE_TAG}
  stage: preperations
  when: manual
  script:
    - |
      ansible-playbook --inventory inventory deploy-traefik.yml --private-key ${PROJECT_SSH_KEY} \
       --extra-vars config_dir=/etc/app-traefik

deploy-docker:
  extends: .ansible-template
  image: ${ANSIBLE_IMAGE_TAG}
  stage: preperations
  when: manual
  script:
    - |
      ansible-playbook --inventory inventory deploy-docker-ce.yml --private-key ${PROJECT_SSH_KEY} 

deploy-portainer:
  extends: .ansible-template
  image: ${ANSIBLE_IMAGE_TAG}
  stage: preperations
  when: manual
  script:
    - |
      ansible-playbook --inventory inventory deploy-portainer.yml --private-key ${PROJECT_SSH_KEY} \
       --extra-vars config_dir=/etc/app-portainer


deploy-node-exporter:
  extends: .ansible-template
  image: ${ANSIBLE_IMAGE_TAG}
  variables:
    HOSTS_LIMIT: all
  stage: preperations
  when: manual
  script:
    - |
      ansible-playbook --limit ${HOSTS_LIMIT} --inventory inventory deploy-node-exporter.yml --private-key ${PROJECT_SSH_KEY}
        
