image: docker:stable

services:
  - docker:dind

variables:
  AWS_ECS_CLUSTER: example-project
  AWS_S3_STATIC_BASE_URI: example-project-stage/public
  CONTAINER_IMAGE: registry.gitlab.com/$CI_PROJECT_PATH
  DOCKER_HOST: tcp://docker:2375
  DOCKER_DRIVER: overlay2

before_script:
  - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN registry.gitlab.com

stages:
  - build:base
  - 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

.build_tmpl: &build_tmpl
  <<: *job_tmpl
  script:
    - 'git clone http://gitlab+deploy-token-50199:z2WzSQKY9Crzvw98yzTy@gitlab.com/gitw/akverse.git'
    - 'CI=1 PUSH=${PUSH} PUSH_AWS_REGISTRY=${PUSH_AWS_REGISTRY} ./akverse/scripts/build.sh $SERVICE $TARGET_ENV -'

.deploy_tmpl: &deploy_tmpl
  <<: *job_tmpl
  script:
    - 'git clone http://gitlab+deploy-token-50199:z2WzSQKY9Crzvw98yzTy@gitlab.com/gitw/akverse.git'
    - 'LB=${ENABLE_LB} SD=${ENABLE_SD} VPC=${ENABLE_VPC} S3_BUCKET=${S3_BUCKET} S3_KEY=${S3_KEY} STATIC_S3_URI=${STATIC_S3_URI} ./akverse/scripts/deploy.sh $SERVICE $TARGET_ENV - ${ECS_CLUSTER}'

.build_base_tmpl: &build_base_tmpl
  <<: *build_tmpl
  stage: build:base
  tags:
    - stage
  only:
    - master
    - stage
    - /^stage-.*$/
    - prod
    - /^prod-.*$/

.build_stage_tmpl: &build_stage_tmpl
  <<: *build_tmpl
  stage: build:stage
  tags:
    - stage

.build_prod_tmpl: &build_prod_tmpl
  <<: *build_tmpl
  stage: build:prod
  tags:
    - prod

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

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

.migrate_stage_tmpl: &migrate_stage_tmpl
  <<: *build_tmpl
  stage: migrate:stage
  tags:
    - stage
  only:
    - master
    - stage
    - /^stage-.*$/
    - prod
    - /^prod-.*$/

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

datadog-agent:build:stage:
  <<: *build_stage_tmpl
  variables:
    TARGET_ENV: 'stage'
    SERVICE: 'datadog-agent'
    PUSH_AWS_REGISTRY: 1

datadog-agent:build:prod:
  <<: *build_prod_tmpl
  variables:
    TARGET_ENV: 'prod'
    SERVICE: 'datadog-agent'
    PUSH_AWS_REGISTRY: 1
  dependencies:
    - 'datadog-agent:build:stage'

db:migrate:stage:
  <<: *migrate_stage_tmpl
  variables:
    TARGET_ENV: 'stage'
    SERVICE: 'schema'

db:migrate:prod:
  <<: *migrate_prod_tmpl
  variables:
    TARGET_ENV: 'prod'
    SERVICE: 'schema'
  dependencies:
    - 'db:migrate:stage'

webapi:build:stage:
  <<: *build_stage_tmpl
  variables:
    TARGET_ENV: 'stage'
    SERVICE: 'webapi'
  only:
    - master
    - stage
    - stage-webapi
    - prod
    - prod-webapi
webapi:deploy:stage:
  <<: *deploy_stage_tmpl
  variables:
    TARGET_ENV: 'stage'
    SERVICE: 'webapi'
    ECS_CLUSTER: '${ECS_CLUSTER}'
    STATIC_S3_URI: '${AWS_S3_STATIC_BASE_URI}/stage/webapi'
    ENABLE_LB: 0
  dependencies:
    - 'webapi:build:stage'
    - 'db:migrate:stage'
    - 'datadog-agent:build:stage'
  only:
    - master
    - stage
    - stage-webapi
    - prod
    - prod-webapi
webapi:build:prod:
  <<: *build_prod_tmpl
  variables:
    TARGET_ENV: 'prod'
    SERVICE: 'webapi'
  dependencies:
    - 'webapi:deploy:stage'
  only:
    - master
    - prod
    - prod-webapi
webapi:deploy:prod:
  <<: *deploy_prod_tmpl
  variables:
    TARGET_ENV: 'prod'
    SERVICE: 'webapi'
    ECS_CLUSTER: '${ECS_CLUSTER}'
    STATIC_S3_URI: '${AWS_S3_STATIC_BASE_URI}/prod/webapi'
    ENABLE_LB: 0
  dependencies:
    - 'webapi:build:prod'
    - 'db:migrate:prod'
    - 'datadog-agent:build:prod'
  only:
    - master
    - prod
    - prod-webapi

#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'
