stages:
  - build
  - upload
  - release

variables:
  PACKAGE_REGISTRY_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/devlifefrommodernway/${CI_COMMIT_TAG}"

upload:
  stage: upload
  image: curlimages/curl:latest
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - 'curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file target/book.pdf ${PACKAGE_REGISTRY_URL}/book-${CI_COMMIT_TAG}.pdf'
    - 'curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file target/book.epub ${PACKAGE_REGISTRY_URL}/book-${CI_COMMIT_TAG}.epub'
    - 'curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file target/book.mobi ${PACKAGE_REGISTRY_URL}/book-${CI_COMMIT_TAG}.mobi'

release:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - release-cli create --name "book-$CI_COMMIT_TAG" --tag-name $CI_COMMIT_TAG
      --assets-link "{\"name\":\"book.pdf\", \"url\":\"${PACKAGE_REGISTRY_URL}/book-${CI_COMMIT_TAG}.pdf\"}"
      --assets-link "{\"name\":\"book.epub\", \"url\":\"${PACKAGE_REGISTRY_URL}/book-${CI_COMMIT_TAG}.epub\"}"
      --assets-link "{\"name\":\"book.mobi\", \"url\":\"${PACKAGE_REGISTRY_URL}/book-${CI_COMMIT_TAG}.mobi\"}"

image: node:lts-bullseye
pages:
  stage: build
  cache:
    key:
      files:
        - pnpm-lock.yaml
    paths:
      - node_modules/
  script:
    - apt-get update && apt-get install -y calibre --no-install-recommends

    - apt-get update && apt-get install -y fontconfig unzip sudo
    - unzip "./styles/fonts/*.zip" -d /usr/share/fonts/
      && fc-cache -fv

    - mkdir -p /etc/fonts/conf.d &&
      cp -a ./styles/fonts/fonts.conf /etc/fonts/conf.d/99-firgenerd.conf &&
      fc-cache -fv

    - sudo corepack enable
    - sudo -u node pnpm install

    - sudo -u node env LANG=ja_JP.UTF-8 pnpm run build

    - cp -a _book/. public/
  artifacts:
    paths:
      - public/
      - target/
  rules:
    - if: $CI_COMMIT_TAG
