name: Docs build
on:
  push:
    branches: [main]
  pull_request:
  release:
    types: [published]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

permissions: {}

jobs:
  pre-job:
    runs-on: ubuntu-latest
    permissions:
      contents: read
    outputs:
      should_run: ${{ steps.check.outputs.should_run }}
    steps:
      - id: token
        uses: immich-app/devtools/actions/create-workflow-token@9db058b2e6eec20e07760b0e17a0505c78ec3191 # create-workflow-token-action-v2.0.1
        with:
          client-id: ${{ secrets.PUSH_O_MATIC_APP_CLIENT_ID }}
          private-key: ${{ secrets.PUSH_O_MATIC_APP_KEY }}

      - name: Check what should run
        id: check
        uses: immich-app/devtools/actions/pre-job@91f342bb4477c4bc10c576ae739da875d85aa164 # pre-job-action-v2.0.4
        with:
          github-token: ${{ steps.token.outputs.token }}
          filters: |
            docs:
              - 'docs/**'
            open-api:
              - 'open-api/immich-openapi-specs.json'
          force-filters: |
            - '.github/workflows/docs-build.yml'
          force-events: 'release'
          force-branches: 'main'

  build:
    name: Docs Build
    needs: pre-job
    permissions:
      contents: read
    if: ${{ fromJSON(needs.pre-job.outputs.should_run).docs == true }}
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./docs

    steps:
      - id: token
        uses: immich-app/devtools/actions/create-workflow-token@9db058b2e6eec20e07760b0e17a0505c78ec3191 # create-workflow-token-action-v2.0.1
        with:
          client-id: ${{ secrets.PUSH_O_MATIC_APP_CLIENT_ID }}
          private-key: ${{ secrets.PUSH_O_MATIC_APP_KEY }}

      - name: Checkout code
        uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
        with:
          persist-credentials: false
          token: ${{ steps.token.outputs.token }}

      - name: Setup Mise
        uses: immich-app/devtools/actions/use-mise@3bca63ca3c15020293b36b51737a3ee2c773340b # use-mise-action-v3.1.0
        with:
          github_token: ${{ steps.token.outputs.token }}

      - name: Run install
        run: pnpm install

      - name: Check formatting
        run: pnpm format

      - name: Run build
        run: pnpm build

      - name: Upload build output
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: docs-build-output
          path: docs/build/
          include-hidden-files: true
          retention-days: 1
