.limit_publishing:
    - if: $CI_PIPELINE_SOURCE != "schedule"
      when: on_success

deploy:
  stage: publish
  needs: ['build', 'unit', 'integration', 'e2e', 'audit']
  script:
    - pnpm install
    - pnpm run deploy
  artifacts:
    paths:
      - ./.deploy
      - ./frontend/app/www
  rules: !reference [.limit_publishing]

publish image:
  stage: publish
  inherit:
    default:
      - tags
  needs: ['deploy']
  image:
    name: gcr.io/kaniko-project/executor:v1.12.1-debug
    entrypoint: [""]
  script:
    - >
       /kaniko/executor
       --context "${CI_PROJECT_DIR}/${DEPLOY_DIR}"
       --dockerfile "${CI_PROJECT_DIR}/${DEPLOY_DIR}/Dockerfile"
       --destination "${CI_REGISTRY_IMAGE}/${IMAGE_NAME}:$(grep -o '"version": "[^"]*' "${DEPLOY_DIR}/package.json" | cut -d'"' -f4)"
       --destination "${CI_REGISTRY_IMAGE}/${IMAGE_NAME}:latest"
  rules: !reference [.limit_publishing]
  parallel:
    matrix:
      - IMAGE_NAME: database
        DEPLOY_DIR: backend/database
      - IMAGE_NAME: backend
        DEPLOY_DIR: .deploy/backend
      - IMAGE_NAME: proxy
        DEPLOY_DIR: .deploy/proxy
      - IMAGE_NAME: api
        DEPLOY_DIR: .deploy/api-cli
      - IMAGE_NAME: minimal-connector
        DEPLOY_DIR: .deploy/minimal-connector
      - IMAGE_NAME: minimal-plugin
        DEPLOY_DIR: .deploy/minimal-plugin
      - IMAGE_NAME: app
        DEPLOY_DIR: frontend/app

publish packages:
  stage: publish
  needs: ['deploy']
  variables:
    GIT_STRATEGY: clone
    GIT_DEPTH: 0
  script:
    - pnpm install
    - pnpm build
    - pnpm publish -r --publish-branch ${PUBLISH_BRANCH} --tag ${PUBLISH_TAG} --no-git-checks # TODO: Git checks...
  rules:
    - if: $CI_COMMIT_BRANCH == 'main'
      variables:
        PUBLISH_BRANCH: main
        PUBLISH_TAG: latest
    - variables:
        PUBLISH_BRANCH: $CI_COMMIT_BRANCH
        PUBLISH_TAG: next

publish docs:
  stage: publish
  needs: ['deploy']
  script:
    - pnpm install --prefer-offline
    - pnpm run docs
    - mv docs public
  artifacts:
    paths:
      - public
