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

services:
  - docker:dind

variables:
  DOCKER_TLS_CERTDIR: ""
  GIT_DEPTH: "10"

stages:
  - setup:dev
  - build:dev
  - migrate:dev
  - deploy:dev
  - setup:stage
  - build:stage
  - migrate:stage
  - deploy:stage
  - setup:prod
  - 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

.setup_tmpl: &setup_tmpl
  <<: *job_tmpl
  script:
    - 'go build -o cicd ./build/cicd'
    - './cicd --env=${TARGET_ENV} deploy infrastructure'
  artifacts:
    paths:
      - cicd
    expire_in: 1 day

.migrate_tmpl: &migrate_tmpl
  <<: *job_tmpl
  script:
    - './cicd --env=${TARGET_ENV} schema migrate'

.build_tmpl: &build_tmpl
  <<: *job_tmpl
  script:
    - './cicd --env=${TARGET_ENV} build ${TARGET_TYPE} --name=${TARGET_REF}'

.deploy_tmpl: &deploy_tmpl
  <<: *job_tmpl
  script:
    - './cicd --env=${TARGET_ENV} deploy ${TARGET_TYPE} --name=${TARGET_REF}'

infra:setup:prod:
  <<: *setup_tmpl
  stage: setup:prod
  tags:
    - prod
  only:
    - master
    - prod
    - /^prod-.*$/
  variables:
    TARGET_ENV: 'prod'
    AWS_USE_ROLE: 'true'

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

webapp:build:prod:
  <<: *build_tmpl
  stage: build:prod
  tags:
    - prod
  only:
    - master
    - prod
    - prod-web-app
  dependencies:
    - 'infra:setup:prod'
  variables:
    TARGET_ENV: 'prod'
    TARGET_TYPE: 'service'
    TARGET_REF: 'web-app'
    AWS_USE_ROLE: 'true'
webapp:deploy:prod:
  <<: *deploy_tmpl
  stage: deploy:prod
  tags:
    - prod
  only:
    - master
    - prod
    - prod-web-app
  dependencies:
    - 'infra:setup:prod'
    - 'webapp:build:prod'
    - 'db:migrate:prod'
  variables:
    TARGET_ENV: 'prod'
    TARGET_TYPE: 'service'
    TARGET_REF: 'web-app'
    AWS_USE_ROLE: 'true'
  #when: manual

webapi:build:prod:
  <<: *build_tmpl
  stage: build:prod
  tags:
    - prod
  only:
    - master
    - prod
    - prod-web-api
  dependencies:
    - 'infra:setup:prod'
  variables:
    TARGET_ENV: 'prod'
    TARGET_TYPE: 'service'
    TARGET_REF: 'web-api'
    AWS_USE_ROLE: 'true'
webapi:deploy:prod:
  <<: *deploy_tmpl
  stage: deploy:prod
  tags:
    - prod
  only:
    - master
    - prod
    - prod-web-api
  dependencies:
    - 'infra:setup:prod'
    - 'webapi:build:prod'
    - 'db:migrate:prod'
  variables:
    TARGET_ENV: 'prod'
    TARGET_TYPE: 'service'
    TARGET_REF: 'web-api'
    AWS_USE_ROLE: 'true'
  #when: manual

ddlogscollector:build:prod:
  <<: *build_tmpl
  stage: build:prod
  tags:
    - prod
  only:
    - master
    - prod
    - prod-ddlogs
  dependencies:
    - 'infra:setup:prod'
  variables:
    TARGET_ENV: 'prod'
    TARGET_TYPE: 'function'
    TARGET_REF: 'ddlogscollector'
    AWS_USE_ROLE: 'true'
ddlogscollector:deploy:prod:
  <<: *deploy_tmpl
  stage: deploy:prod
  tags:
    - prod
  only:
    - master
    - prod
    - prod-ddlogs
  dependencies:
    - 'infra:setup:prod'
    - 'ddlogscollector:build:prod'
    - 'db:migrate:prod'
  variables:
    TARGET_ENV: 'prod'
    TARGET_TYPE: 'function'
    TARGET_REF: 'ddlogscollector'
    AWS_USE_ROLE: 'true'
  #when: manual

