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

services:
  - docker:dind

variables:
  DOCKER_TLS_CERTDIR: ""

before_script:
  - './tools/devops/scripts/build.sh'

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

.build_tmpl: &build_tmpl
  <<: *job_tmpl
  script:
    - './devops build -service=${SERVICE} -project=${PROJECT_NAME} -env=${TARGET_ENV}'

.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} -public_bucket_cloudfront=${S3_BUCKET_PUBLIC_CLOUDFRONT}  -static_files_s3=${STATIC_FILES_S3} -static_files_img_resize=${STATIC_FILES_IMG_RESIZE}'

.migrate_tmpl: &migrate_tmpl
  <<: *job_tmpl
  script:
    - './devops migrate -project=${PROJECT_NAME} -env=${TARGET_ENV}'

db:migrate:dev:
  <<: *migrate_tmpl
  stage: migrate:dev
  tags:
    - dev
  only:
    - master
    - dev
    - /^dev-.*$/
  variables:
    TARGET_ENV: 'dev'
    AWS_USE_ROLE: 'true'

webapp:build:dev:
  <<: *build_tmpl
  stage: build:dev
  tags:
    - dev
  only:
    - master
    - dev
    - dev-web-app
  variables:
    TARGET_ENV: 'dev'
    SERVICE: 'web-app'
    AWS_USE_ROLE: 'true'
webapp:deploy:dev:
  <<: *deploy_tmpl
  stage: deploy:dev
  tags:
    - dev
  only:
    - master
    - dev
    - dev-web-app
  dependencies:
    - 'webapp:build:dev'
    - 'db:migrate:dev'
  variables:
    TARGET_ENV: 'dev'
    SERVICE: 'web-app'
    ENABLE_HTTPS: 1
    ENABLE_ELB: 0
    PRIMARY_HOST: 'example.saasstartupkit.com'
    HOST_NAMES: 'example.saasstartupkit.com,dev.example.saasstartupkit.com'
    S3_BUCKET_PRIVATE: 'saas-starter-kit-private'
    S3_BUCKET_PUBLIC: 'saas-starter-kit-public'
    S3_BUCKET_PUBLIC_CLOUDFRONT: 'true'
    STATIC_FILES_S3: 'true'
    STATIC_FILES_IMG_RESIZE: 'true'
    AWS_USE_ROLE: 'true'
    EMAIL_SENDER: 'lee+saas-starter-kit@geeksinthewoods.com'
    WEB_API_BASE_URL: https://api.example.saasstartupkit.com

webapi:build:dev:
  <<: *build_tmpl
  stage: build:dev
  tags:
    - dev
  only:
    - master
    - dev
    - dev-web-api
  variables:
    TARGET_ENV: 'dev'
    SERVICE: 'web-api'
    AWS_USE_ROLE: 'true'
webapi:deploy:dev:
  <<: *deploy_tmpl
  stage: deploy:dev
  tags:
    - dev
  only:
    - master
    - dev
    - dev-web-api
  dependencies:
    - 'webapi:build:dev'
    - 'db:migrate:dev'
  variables:
    TARGET_ENV: 'dev'
    SERVICE: 'web-api'
    ENABLE_HTTPS: 1
    ENABLE_ELB: 0
    PRIMARY_HOST: 'api.example.saasstartupkit.com'
    HOST_NAMES: 'api.dev.example.saasstartupkit.com'
    S3_BUCKET_PRIVATE: 'saas-starter-kit-private'
    S3_BUCKET_PUBLIC: 'saas-starter-kit-public'
    S3_BUCKET_PUBLIC_CLOUDFRONT: 'false'
    STATIC_FILES_S3: 'false'
    STATIC_FILES_IMG_RESIZE: 'false'
    AWS_USE_ROLE: 'true'
    EMAIL_SENDER: 'lee+saas-starter-kit@geeksinthewoods.com'
    WEB_APP_BASE_URL: https://example.saasstartupkit.com

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