.pre-deploy-uber-space-setup: &pre-deploy-uber-space-setup
  - apk add rsync openssh-client
  - eval $(ssh-agent -s)
  - echo "$CI_DEPLOYMENT_PRIVATE_KEY" | tr -d '\r' | ssh-add -
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - echo "$UBERSPACE_KNOWN_HOST" >> ~/.ssh/known_hosts

.doc-changelist: &doc-changelist
  changes:
    - docs/*
    - versioned_docs/*
    - versioned_sidebars/*
    - src/*
    - static/*
    - docusaurus.config.js
    - sidebars.js
    - versions.json
    - .gitlab-ci.yml

stages:
  - lint
  - build
  - upload
  - release

lint-job:
  stage: lint
  image:
    name: $CI_REGISTRY/stoplight/spectral:latest
    entrypoint: [ "" ]
  script:
    - spectral lint spec/*.yml

build:spec:
  stage: build
  image: $CI_REGISTRY/node:current-alpine
  before_script:
    - npm install -g @apidevtools/swagger-cli
    - mkdir dist
  script:
    - cp metadata-schema/antragsmetadaten.schema.json dist/
    - swagger-cli bundle -t yaml -o dist/zustelldienst.yml spec/zustelldienst.yml
  artifacts:
    expose_as: 'Built API spec'
    paths:
      - dist/
    expire_in: 30 days

build:doc:
  stage: build
  image: $CI_REGISTRY/node:current-alpine
  variables:
    GIT_BRANCH: $CI_COMMIT_REF_NAME
  before_script:
    - apk add git
    - yarn
  script:
    - export DOCUSAURUS_BASE_URL="/fit-connect/" && [[ "$CI_COMMIT_REF_NAME" != "main" ]] && export DOCUSAURUS_BASE_URL="/preview/fit-connect/$CI_COMMIT_REF_SLUG/"
    - yarn build
  artifacts:
    expose_as: 'Built Documentation'
    paths:
      - build/
    expire_in: 30 days
  rules:
    - *doc-changelist

upload:doc:
  stage: upload
  image: $CI_REGISTRY/alpine:latest
  needs: [ 'build:doc' ]
  rules:
    - *doc-changelist
  before_script:
    - *pre-deploy-uber-space-setup
  script:
    - DEPLOY_PATH="fit-connect" && [[ "$CI_COMMIT_REF_NAME" != "main" ]] && DEPLOY_PATH="preview/fit-connect/$CI_COMMIT_REF_SLUG"
    - rsync -rLvz --size-only --checksum -e "ssh -o CheckHostIP=no" --ipv4 --progress ./build/. fitko@dorado.uberspace.de:html/$DEPLOY_PATH

upload:spec:
  stage: upload
  image: $CI_REGISTRY/alpine:latest
  needs: [ 'build:spec' ]
  only:
    refs:
      - tags
    variables:
      - $CI_COMMIT_TAG
  before_script:
    - *pre-deploy-uber-space-setup
  script:
    - sed -i "s,API_VERSION,$CI_COMMIT_TAG,g" dist/zustelldienst.yml
    - sed -i "s,API_VERSION,$CI_COMMIT_TAG,g" dist/antragsmetadaten.schema.json
    - rsync -rLvz --size-only --checksum -e "ssh -o CheckHostIP=no" --ipv4 --progress ./assets/. fitko@dorado.uberspace.de:html/$CI_COMMIT_TAG/
    - rsync -rLvz --size-only --checksum -e "ssh -o CheckHostIP=no" --ipv4 --progress ./dist/. fitko@dorado.uberspace.de:html/$CI_COMMIT_TAG/

#release-new-api-version:
#  stage: release
#  image: registry.gitlab.com/gitlab-org/release-cli:latest
#  needs:
#    - job: build
#      artifacts: true
#  only:
#    refs:
#      - main
#      - tags
#    variables:
#      - $CI_COMMIT_TAG
#  script:
#    - release-cli -v
#    - echo "Releasing new API version"
#    - release-cli create --name "$CI_COMMIT_TAG" --description './CHANGELOG.md'
#      --tag-name $CI_COMMIT_TAG
#      --assets-link '{"name":"combined-$CI_COMMIT_TAG","url":"https://fitko.uber.space/$CI_COMMIT_TAG/zustelldienst.yml"}'

