stages:
  - build
  - deploy-dev
  - deploy-prod

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

build:
  stage: build
  tags:
    - dev
  extends:
    - .deploy
  script:
    - docker build -t mathwave/sprint-repo:platform .
    - docker build -t mathwave/sprint-repo:platform-nginx nginx
    - docker push mathwave/sprint-repo:platform
    - docker push mathwave/sprint-repo:platform-nginx

deploy-dev:
  extends:
    - .deploy
  stage: deploy-dev
  tags:
    - dev
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'
      when: on_success
    - if: '$CI_PIPELINE_SOURCE != "merge_request_event"'
      when: manual
  script:
    - docker stack deploy --with-registry-auth -c ./.deploy/deploy-dev.yaml platform

deploy-prod:
  extends:
    - .deploy
  stage: deploy-prod
  tags:
    - prod
  only:
    - main
  when: manual
  script:
    - docker stack deploy --with-registry-auth -c ./.deploy/deploy-prod.yaml platform
