image: geeksaccelerator/docker-library:golang1.12-docker

services:
  - docker:dind

before_script:
  - 'cd ./tools/devops && make install && cd ../../'
  - 'cd ./tools/schema && make install && cd ../../'

stages:
  - build:dev
  - migrate:dev
  - deploy:dev
  - build:stage
  - migrate:stage
  - deploy:stage
  - build:prod
  - migrate:prod
  - deploy:prod

cache:
  key: ${CI_COMMIT_REF_SLUG}

# Everything should get this, whether through subtemplates or explicitly
# embedded in a job.
.job_tmpl: &job_tmpl
  only:
    - master

.deploy_tmpl: &deploy_tmpl
  <<: *job_tmpl
  script:
    - 'devops deploy -service=${SERVICE} -project=${PROJECT_NAME} -env=${TARGET_ENV} -enable_https=${ENABLE_HTTPS} -enable_elb=${ENABLE_ELB} -primary_host=${PRIMARY_HOST} -host_names=${HOST_NAMES} -private_bucket=${S3_BUCKET_PRIVATE} -public_bucket=${S3_BUCKET_PUBLIC} -no_build=${NO_BUILD} -no_deploy=${NO_DEPLOY}'

.migrate_tmpl: &migrate_tmpl
  <<: *job_tmpl
  script:
    - './schema ???'

.deploy_dev_tmpl: &deploy_dev_tmpl
  <<: *deploy_tmpl
  tags:
    - dev
  environment:
    name: 'dev/${SERVICE}-dev'

.deploy_stage_tmpl: &deploy_stage_tmpl
  <<: *deploy_tmpl
  tags:
    - stage
  environment:
    name: 'stage/${SERVICE}-stage'

.deploy_prod_tmpl: &deploy_prod_tmpl
  <<: *deploy_tmpl
  tags:
    - prod
  environment:
    name: 'production/${SERVICE}'
  when: manual

.migrate_dev_tmpl: &migrate_dev_tmpl
  <<: *migrate_tmpl
  stage: migrate:dev
  tags:
    - dev
  only:
    - master
    - dev
    - /^dev-.*$/

.migrate_stage_tmpl: &migrate_stage_tmpl
  <<: *migrate_tmpl
  stage: migrate:stage
  tags:
    - stage
  only:
    - stage
    - /^stage-.*$/

.migrate_prod_tmpl: &migrate_prod_tmpl
  <<: *migrate_tmpl
  stage: migrate:prod
  tags:
    - prod
  when: manual
  only:
    - prod
    - /^prod-.*$/

#db:migrate:dev:
#  <<: *migrate_dev_tmpl
#  variables:
#    TARGET_ENV: 'stage'
#    SERVICE: 'schema'

.deploy_dev_webapi_tmpl: &deploy_dev_webapi_tmpl
  <<: *deploy_dev_tmpl
  tags:
    - dev
  environment:
    name: 'dev/${SERVICE}-dev'
  variables:
    TARGET_ENV: 'dev'
    SERVICE: 'web-api'
    ENABLE_HTTPS: 1
    ENABLE_ELB: 0
    PRIMARY_HOST: 'eproc.tech'
    HOST_NAMES: 'www.eproc.tech, api.eproc.tech'
    S3_BUCKET_PRIVATE: 'saas-starter-kit-private'
    S3_BUCKET_PUBLIC: 'saas-starter-kit-public'
    NO_DEPLOY: 1
    AWS_USE_ROLE: 1
webapi:build:dev:
  <<: *deploy_dev_webapi_tmpl
  stage: build:dev
  only:
    - master
    - dev
    - dev-web-api
webapi:deploy:dev:
  <<: *deploy_dev_webapi_tmpl
  stage: deploy:dev
  dependencies:
    - 'webapi:build:dev'
#    - 'db:migrate:dev'
  only:
    - master
    - dev
    - dev-web-api


#ddlogscollector:deploy:stage:
#  <<: *deploy_stage_tmpl
#  variables:
#    TARGET_ENV: 'stage'
#    ECS_CLUSTER: '${ECS_CLUSTER}'
#    SERVICE: 'ddlogscollector'
#    S3_BUCKET: 'keenispace-services-stage'
#    S3_KEY: 'aws/lambda/ddlogscollector/src/ddlogscollector-stage.zip'
#    ENABLE_VPC: 0
#  only:
#    - master
#    - stage
#ddlogscollector:deploy:prod:
#  <<: *deploy_prod_tmpl
#  variables:
#    TARGET_ENV: 'prod'
#    ECS_CLUSTER: '${ECS_CLUSTER}'
#    SERVICE: 'ddlogscollector'
#    S3_BUCKET: 'keenispace-services-prod'
#    S3_KEY: 'aws/lambda/ddlogscollector/src/ddlogscollector-prod.zip'
#    ENABLE_VPC: 0
#  only:
#    - master
#    - prod
#  #dependencies:
#  #  - 'ddlogscollector:deploy:stage'
