include:
  - project: "devops/pena-continuous-integration"
    file: "/templates/docker/build-template.gitlab-ci.yml"
  - project: "devops/pena-continuous-integration"
    file: "/templates/docker/deploy-template.gitlab-ci.yml"

stages:
  - build
  - migrate
  - deploy

build-app:
  stage: build
  extends: .build_template
  rules:
    - if: "$CI_COMMIT_BRANCH == $STAGING_BRANCH || $CI_COMMIT_BRANCH == $PRODUCTION_BRANCH"
  script:
    - docker build -t $CI_REGISTRY_IMAGE/$CI_COMMIT_BRANCH-worker:$CI_COMMIT_REF_SLUG.$CI_PIPELINE_ID --build-arg  GITLAB_TOKEN=$GITLAB_TOKEN $CI_PROJECT_DIR
    - docker push $CI_REGISTRY_IMAGE/$CI_COMMIT_BRANCH-worker:$CI_COMMIT_REF_SLUG.$CI_PIPELINE_ID


migrate-staging:
  stage: migrate
  variables:
    STAGING_BRANCH: staging
  tags:
    - staging
  rules:
    - if: "$CI_COMMIT_BRANCH == $STAGING_BRANCH"
  script:
    - apk add git
    - git clone https://buildToken:glpat-axA8ttckx3aPf_xd2Dym@penahub.gitlab.yandexcloud.net/backend/quiz/common.git
    - ls
    - ./tools/migrate -source file://common/dal/schema -database postgres://squiz:Redalert2@10.8.0.5:5433/squiz?sslmode=disable up

migrate-prod:
  stage: migrate
  variables:
    STAGING_BRANCH: main
  tags:
    - prod
  rules:
    - if: "$CI_COMMIT_BRANCH == $STAGING_BRANCH"
  script:
    - apk add git
    - git clone https://buildToken:glpat-axA8ttckx3aPf_xd2Dym@penahub.gitlab.yandexcloud.net/backend/quiz/common.git
    - ls
    - ./tools/migrate -source file://common/dal/schema -database postgres://squiz:Redalert2@10.8.0.9:5433/squiz?sslmode=disable up
deploy-staging:
  stage: deploy
  tags:
    - staging
  extends: .deploy_template
  rules:
    - if: "$CI_COMMIT_BRANCH == $STAGING_BRANCH"

deploy-prod:
  stage: deploy
  tags:
    - prod
  extends: .deploy_template
  rules:
    - if: "$CI_COMMIT_BRANCH == $PRODUCTION_BRANCH"
