name: Deploy storybook

on:
  workflow_dispatch:

  workflow_run:
    workflows: ["Release"]
    types: [completed]

permissions:
  pages: write
  id-token: write

concurrency:
  group: pages
  cancel-in-progress: false

jobs:
  build:
    if: github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success'
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.workflow_run.head_sha || github.sha }}

      - name: Setup Node
        uses: actions/setup-node@v6
        with:
          node-version: 26.5.0

      - name: Setup Pages
        id: pages
        uses: actions/configure-pages@v5

      - name: "Install JS dependencies"
        run: "npm ci"

      - name: Build storybook
        run: npm run storybook-build

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: "storybook-static"

  deploy:
    needs: build
    # required by actions/deploy-pages: ties this job to the Pages deployment
    # history/protection rules and provides the OIDC context it authenticates with
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    steps:
      - name: Deploy to GitHub pages
        id: deployment
        uses: actions/deploy-pages@v4
