timoth-y/kicksware-api

View on GitHub
.gitlab-ci.yml

Summary

Maintainability
Test Coverage
include: /.gitlab/.gitlab-ci.compose.yml

image: dtzar/helm-kubectl:3.3.0

stages:
  - build
  - test
  - deploy
  - check

before_script: &common_before
  - kubectl create secret docker-registry registry-token -n api
    --docker-server=$CI_REGISTRY --docker-username=$CI_REGISTRY_USER --docker-password=$CI_REGISTRY_TOKEN --dry-run -o yaml | kubectl apply -f -
  - kubectl create secret generic mongo-auth -n api
    --from-literal=MONGO_USER=$MONGO_USER --from-literal=MONGO_PASSWORD=$MONGO_PASSWORD --dry-run -o yaml | kubectl apply -f -
  - apk add gettext


# build stage steps:

api.build:
  image: docker/compose:1.25.0
  services:
    - docker:18.09-dind
  stage: build
  tags:
    - k8s
    - api
  variables:
    DOCKER_HOST: tcp://localhost:2375
  allow_failure: true
  except:
    changes:
      - "*.md"
      - assets/**/*
  before_script:
    - docker version
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_TOKEN $CI_REGISTRY
  script:
    - docker-compose build
  after_script:
    - docker-compose push

# test stage steps:

api.test:
  stage: test
  tags:
    - k8s
    - api
  except:
    changes:
      - "*.md"
      - assets/**/*
  before_script:
    - printf "\e[32mTODO"
  script:
    - echo -e "\e[33mYou should implement test... \e[91mI am serisous, dude - \e[1mTESTS ARE IMPORTANT!"

# deploy stage steps:

users.deploy.k8s:
  stage: deploy
  tags:
    - k8s
    - api
  only:
    changes:
      - user-service/**/*
    kubernetes: active
  except:
    changes:
      - "*.md"
      - assets/**/*
  before_script:
    - *common_before
    - kubectl create secret generic mail-auth -n api
      --from-literal=MAIL_SERVER=$MAIL_SERVER --from-literal=MAIL_ADDRESS=$MAIL_ADDRESS --from-literal=MAIL_PASSWORD=$MAIL_PASSWORD --dry-run -o yaml | kubectl apply -f -
    - envsubst < user-service/env/config.$ENV.yaml > config.yaml && kubectl create configmap users-service.config -n api
      --from-file=config.yaml=config.yaml --dry-run -o yaml | kubectl apply -f -
    - kubectl create configmap mail-service.templates -n api
      --from-file=user-service/template/verify.template.html
      --from-file=user-service/template/notify.template.html
      --from-file=user-service/template/reset.template.html --dry-run -o yaml | kubectl apply -f -
  script:
    - helm upgrade --install -n api users-service user-service/users-chart

references.deploy.k8s:
  stage: deploy
  tags:
    - k8s
    - api
  only:
    changes:
      - reference-service/**/*
    kubernetes: active
  except:
    changes:
      - "*.md"
      - assets/**/*
  before_script:
    - *common_before
    - envsubst < reference-service/env/config.$ENV.yaml > config.yaml && kubectl create configmap references-service.config -n api
      --from-file=config.yaml=config.yaml --dry-run -o yaml | kubectl apply -f -
  script:
    - helm upgrade --install -n api references-service reference-service/references-chart

products.deploy.k8s:
  stage: deploy
  tags:
    - k8s
    - api
  only:
    changes:
      - product-service/**/*
    kubernetes: active
  except:
    changes:
      - "*.md"
      - assets/**/*
  before_script:
    - *common_before
    - envsubst < product-service/env/config.$ENV.yaml > config.yaml && kubectl create configmap products-service.config -n api
      --from-file=config.yaml=config.yaml --dry-run -o yaml | kubectl apply -f -
  script:
    - helm upgrade --install -n api products-service product-service/products-chart

search.deploy.k8s:
  stage: deploy
  tags:
    - k8s
    - api
  only:
    changes:
      - search-service/**/*
    kubernetes: active
  except:
    changes:
      - "*.md"
      - assets/**/*
  before_script:
    - *common_before
    - envsubst < search-service/env/config.$ENV.yaml > config.yaml && kubectl create configmap search-service.config -n api
      --from-file=config.yaml=config.yaml --dry-run -o yaml | kubectl apply -f -
  script:
    - helm upgrade --install -n api search-service search-service/search-chart

orders.deploy.k8s:
  stage: deploy
  tags:
    - k8s
    - api
  only:
    changes:
      - order-service/**/*
    kubernetes: active
  except:
    changes:
      - "*.md"
      - assets/**/*
  before_script:
    - *common_before
    - envsubst < order-service/env/config.$ENV.yaml > config.yaml && kubectl create configmap orders-service.config -n api
      --from-file=config.yaml=config.yaml --dry-run -o yaml | kubectl apply -f -
  script:
    - helm upgrade --install -n api orders-service order-service/orders-chart

cdn.deploy.k8s:
  stage: deploy
  tags:
    - k8s
    - api
  only:
    changes:
      - cdn-service/**/*
    kubernetes: active
  except:
    changes:
      - "*.md"
      - assets/**/*
  before_script:
    - *common_before
    - kubectl create secret docker-registry registry-token -n api
        --docker-server=$CI_REGISTRY --docker-username=$CI_REGISTRY_USER --docker-password=$CI_REGISTRY_TOKEN --dry-run -o yaml | kubectl apply -f -
    - kubectl create secret generic mongo-auth -n api
        --from-literal=MONGO_USER=$MONGO_USER --from-literal=MONGO_PASSWORD=$MONGO_PASSWORD --dry-run -o yaml | kubectl apply -f -
    - envsubst < cdn-service/env/config.$ENV.yaml > config.yaml && kubectl create configmap cdn-service.config -n api
      --from-file=config.yaml=config.yaml --dry-run -o yaml | kubectl apply -f -
  script:
    - helm upgrade --install -n api cdn-service cdn-service/cdn-chart

# check stage steps:

users.check.k8s:
  image: curlimages/curl
  stage: check
  tags:
    - k8s
    - api
  before_script:
    - echo Waiting half a minute for service to properly start or fail... && sleep 30s
  script:
    - curl "http://$API_ENDPOINT_URL/health/users/live" -f -o /dev/null || exit 1 | echo -e "\e[91mService dead! Triggering check job failure"
    - echo -e "\e[36mService live. Checking service readiness:\n"
    - curl "http://$API_ENDPOINT_URL/health/users/ready" -f -o /dev/null || exit 1 | echo -e "\e[91mService not ready! Triggering check job failure"
    - echo -e "\e[36mService ready. Check job succeeded!"
  retry: 2

references.check.k8s:
  image: curlimages/curl
  stage: check
  tags:
    - k8s
    - api
  before_script:
    - echo Waiting half a minute for service to properly start or fail... && sleep 30s
  script:
    - curl "http://$API_ENDPOINT_URL/health/references/live" -f -o /dev/null || exit 1 | echo -e "\e[91mService dead! Triggering check job failure"
    - echo -e "\e[36mService live. Checking service readiness:\n"
    - curl "http://$API_ENDPOINT_URL/health/references/ready" -f -o /dev/null || exit 1 | echo -e "\e[91mService not ready! Triggering check job failure"
    - echo -e "\e[36mService ready. Check job succeeded!"
  retry: 2

products.check.k8s:
  image: curlimages/curl
  stage: check
  tags:
    - k8s
    - api
  before_script:
    - echo Waiting half a minute for service to properly start or fail... && sleep 30s
  script:
    - curl "http://$API_ENDPOINT_URL/health/products/live" -f -o /dev/null || exit 1 | echo -e "\e[91mService dead! Triggering check job failure"
    - echo -e "\e[36mService live. Checking service readiness:\n"
    - curl "http://$API_ENDPOINT_URL/health/products/ready" -f -o /dev/null || exit 1 | echo -e "\e[91mService not ready! Triggering check job failure"
    - echo -e "\e[36mService ready. Check job succeeded!"
  retry: 2

search.check.k8s:
  image: curlimages/curl
  stage: check
  tags:
    - k8s
    - api
  before_script:
    - echo Waiting half a minute for service to properly start or fail... && sleep 30s
  script:
    - curl "http://$API_ENDPOINT_URL/health/search/live" -f -o /dev/null || exit 1 | echo -e "\e[91mService dead! Triggering check job failure"
    - echo -e "\e[36mService live. Checking service readiness:\n"
    - curl "http://$API_ENDPOINT_URL/health/search/ready" -f -o /dev/null || exit 1 | echo -e "\e[91mService not ready! Triggering check job failure"
    - echo -e "\e[36mService ready. Check job succeeded!"
  retry: 2

orders.check.k8s:
  image: curlimages/curl
  stage: check
  tags:
    - k8s
    - api
  before_script:
    - echo Waiting half a minute for service to properly start or fail... && sleep 30s
  script:
    - curl "http://$API_ENDPOINT_URL/health/orders/live" -f -o /dev/null || exit 1 | echo -e "\e[91mService dead! Triggering check job failure"
    - echo -e "\e[36mService live. Checking service readiness:\n"
    - curl "http://$API_ENDPOINT_URL/health/orders/ready" -f -o /dev/null || exit 1 | echo -e "\e[91mService not ready! Triggering check job failure"
    - echo -e "\e[36mService ready. Check job succeeded!"
  retry: 2

cdn.check.k8s:
  image: curlimages/curl
  stage: check
  tags:
    - k8s
    - api
  before_script:
    - echo Waiting half a minute for service to properly start or fail... && sleep 30s
  script:
    - curl "http://$CDN_ENDPOINT_URL/health/live" -f -o /dev/null || exit 1 | echo -e "\e[91mService dead! Triggering check job failure"
    - echo -e "\e[36mService live. Checking service readiness:\n"
    - curl "http://$CDN_ENDPOINT_URL/health/ready" -f -o /dev/null || exit 1 | echo -e "\e[91mService not ready! Triggering check job failure"
    - echo -e "\e[36mService ready. Check job succeeded!"
  retry: 2