variables:
  MAIN_BRANCH: &main-branch main
  BASE_IMAGE_NAME: $CI_REGISTRY_IMAGE
  BASE_IMAGE_TAG: $CI_COMMIT_SHA
  DOCKER_AUTH_CONFIG: $DOCKER_AUTH_RO
  KANIKO_REGISTRY_MIRROR: docker-proxy.binary.picodata.io


workflow:
  # See https://docs.gitlab.com/ee/ci/jobs/job_control.html#avoid-duplicate-pipelines
  rules:
    # To avoid duplicate pipelines we disable merge request events,
    # leaving only pushes and manual triggering.
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: never
    - if: $CI_PIPELINE_SOURCE == "push"
    - if: $CI_PIPELINE_SOURCE == "web"

default:
  tags:
    - docker

stages:
  - build-base-image
  - lint
  - pack-doc
  - upload
  - deploy

build-base-image:
  stage: build-base-image
  variables:
    DOCKERFILE: "Dockerfile"
    PUSH_DOCKER: ""
    GIT_USERNAME: $CI_REGISTRY_USER
    GIT_PASSWORD: $CI_REGISTRY_PASSWORD
  image:
    name: docker-public.binary.picodata.io/kaniko-project/executor:v1.20.1-debug
    entrypoint: ['']
    pull_policy: [if-not-present]
  script:
    - echo "Build picodata doc..."
    - cd docker/static
    - >
      /kaniko/executor --context $CI_PROJECT_DIR --dockerfile ${DOCKERFILE}
      --build-arg IMAGE_DIR=$CI_PROJECT_DIR
      --build-arg COMMIT_HASH=${CI_COMMIT_SHA} ${PUSH_DOCKER}
      --cache=false --cache-run-layers=true --single-snapshot --compressed-caching=false --use-new-run --snapshot-mode=redo --cleanup
      --destination ${BASE_IMAGE_NAME}:${BASE_IMAGE_TAG}

lint:
  image: ${BASE_IMAGE_NAME}:${BASE_IMAGE_TAG}
  stage: lint
  script:
    - echo "Checking picodata doc..."
    - pipenv run lint
    - echo "Picodata doc successfully checked"

pack-doc:
  image: ${BASE_IMAGE_NAME}:${BASE_IMAGE_TAG}
  stage: pack-doc
  variables:
    SED_SLUG: 's/[^a-zA-Z0-9.]+/_/g'
  before_script:
    - |
      if [ "$CI_COMMIT_BRANCH" == "main" ]; then
        PICODATA_DOC_VER="devel"
      elif [ "$CI_COMMIT_BRANCH" == "$MAIN_BRANCH" ]; then
        PICODATA_DOC_VER="$MAIN_BRANCH"
      else
        PICODATA_DOC_VER="$(echo $CI_COMMIT_REF_NAME | sed -E "$SED_SLUG")"
      fi
    - |
      if [ "$CI_COMMIT_BRANCH" == "$MAIN_BRANCH" ]; then
        SITE_URL="https://docs.picodata.io/picodata/$PICODATA_DOC_VER"
      else
        SITE_URL="https://docs.binary.picodata.io/picodata/$PICODATA_DOC_VER"
      fi
  script:
    # Automatic Loading of .env — https://pipenv.pypa.io/en/stable/shell.html
    - echo "Pack picodata doc..."
    - echo "PICODATA_DOC_VER=$PICODATA_DOC_VER" | tee .env
    - echo "SITE_URL=$SITE_URL" | tee -a .env
    - DATE=$(date +%Y%m%d%H%M%S)
    - FNAME="picodata-doc-${DATE}-${PICODATA_DOC_VER}-${CI_COMMIT_SHA}.tgz"
    - echo "FNAME=$FNAME" | tee -a .env
    - echo "GIT_DESCRIBE=$(git describe --always)" | tee -a .env
    - pipenv run mkdocs build -sd build
    - pushd build
    - tar -cvzf ../$FNAME .
    - popd
    - echo "Picodata doc successfully packed."
  artifacts:
    name: "artifacts-vars-${CI_PIPELINE_ID}"
    paths:
      - picodata-doc-*.tgz
    reports:
      dotenv: .env

upload-doc-to-binary:
  image: docker.binary.picodata.io/curlimages/curl
  stage: upload
  script:
    - echo "Upload picodata doc to binary..."
    - echo "FNAME=$FNAME"
    - 'curl --fail -H "Authorization: Basic ${WWW_RAW_RW}" --upload-file $FNAME https://binary.picodata.io/repository/www-raw/$FNAME'
    - echo "Picodata doc successfully uploaded to binary."
  needs:
    - job: pack-doc
      artifacts: true

.deploy:
  stage: deploy
  variables:
    STAGE: TEST
    PICODATA_DOC: $FNAME
    PICODATA_DOC_SUBDIR: $PICODATA_DOC_VER
  trigger:
    project: 'picodata/web-site/infra'
    strategy: depend
  needs:
    - job: pack-doc
      artifacts: true
    - job: upload-doc-to-binary

deploy-staging:
  extends:
    - .deploy

deploy-prod:
  extends:
    - .deploy
  rules:
    - if: $CI_COMMIT_BRANCH == $MAIN_BRANCH
  variables:
    STAGE: PROD
