stages:
  - build
  - deploy-dev
  - deploy-prod
  - push-runner

build:
  stage: build
  tags:
    - dev
  before_script:
    - docker login -u mathwave -p $DOCKERHUB_PASSWORD
  script:
    - docker build -t mathwave/sprint-repo:sprint .
    - docker push mathwave/sprint-repo:sprint
    - docker build -t mathwave/sprint-repo:sprint-nginx nginx
    - docker push mathwave/sprint-repo:sprint-nginx

.deploy:
  before_script:
    - docker login -u mathwave -p $DOCKERHUB_PASSWORD

deploy-dev:
  extends:
    - .deploy
  stage: deploy-dev
  tags:
    - dev
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"'
      when: on_success
    - when: manual
  script:
    - docker stack deploy -c ./.deploy/deploy-dev.yaml sprint

deploy-prod:
  extends:
    - .deploy
  stage: deploy-prod
  tags:
    - prod
  only:
    - master
  when: manual
  script:
    - docker stack deploy -c ./.deploy/deploy-prod.yaml sprint

push-runner:
  extends:
    - .deploy
  stage: push-runner
  tags:
    - prod
  only:
    - master
  when: manual
  script:
    - docker build -t mathwave/sprint-runner .
    - docker push mathwave/sprint-runner