name: Build, Test and Deploy
run-name: workflow-${{ github.ref_name }}-${{ github.run_number }}
on:
  push:
  workflow_dispatch:
    inputs:
      manual_debug:
        description: 'Stuck the workflow for debugging'
        required: false
        default: false
        type: boolean

# If you update the node version, set it in test and publish jobs!
jobs:
  build:
    uses: test-IO/cicd/.github/workflows/docker_build.yaml@main
    concurrency:
      group: ci-${{ github.ref }}-build-${{ github.workflow }}
      cancel-in-progress: true
    with:
      image_name: eu.gcr.io/oi-tset/app-designsystem

  test:
    uses: test-IO/cicd/.github/workflows/designsystem_test.yaml@main
    concurrency:
      group: ci-${{ github.ref }}-test-${{ github.workflow }}
      cancel-in-progress: true
    with:
      node_version: 24

  publish:
    uses: test-IO/cicd/.github/workflows/designsystem_publish.yaml@main
    concurrency:
      group: ci-${{ github.ref }}-${{ github.workflow }}
      cancel-in-progress: true
    needs: [build, test]
    permissions:
      id-token: write
      contents: read
    if: |-
      contains(fromJSON('["main", "master"]'), github.ref_name)
    with:
      node_version: 24

  deploy:
    uses: test-IO/cicd/.github/workflows/deploy_gcp.yaml@main
    concurrency:
      group: deploy-${{ github.ref }}-${{ github.workflow }}
      cancel-in-progress: false # never cancel a running deploy; queue instead
    needs: [publish]
    if: |-
      contains(fromJSON('["main", "master"]'), github.ref_name)
    with:
      helm_chart_name: app-designsystem
      namespace: designsystems

  # Re-index Algolia after deploy. The live site is VPN-only (403 for GitHub runners), so the job
  # builds this commit, serves dist/ on localhost and crawls that with the scraper (private repo).
  # Hit URLs point to localhost; the site's DocSearch transformItems makes them relative.
  algolia_reindex:
    runs-on: ubuntu-latest
    needs: [deploy]
    if: |-
      contains(fromJSON('["main", "master"]'), github.ref_name)
    steps:
      - name: Checkout site
        uses: actions/checkout@v7
        with:
          path: site
      - name: Checkout scraper
        uses: actions/checkout@v7
        with:
          repository: test-IO/cirro-guides-scraper
          ref: master
          ssh-key: ${{ secrets.SCRAPER_REPO_CLONE_KEY }}
          path: scraper
      - name: Setup Node
        uses: actions/setup-node@v7
        with:
          node-version-file: site/.nvmrc
      - name: Build site
        working-directory: site
        run: npm ci && npm run build
      - name: Setup Python
        uses: actions/setup-python@v7
        with:
          python-version-file: scraper/Pipfile
      - name: Install scraper dependencies
        working-directory: scraper
        run: pip install pipenv && PIPENV_VENV_IN_PROJECT=1 pipenv sync
      - name: Index testio_designsystem from the local build
        working-directory: scraper
        env:
          APPLICATION_ID: P5U2WJ4FQV
          API_KEY: ${{ secrets.ALGOLIA_WRITE_API_KEY }}
        run: |
          python3 -m http.server 8080 --directory ../site/dist > /dev/null 2>&1 &
          timeout 30 sh -c 'until curl -sf http://localhost:8080/ > /dev/null; do sleep 1; done'
          jq '.start_urls = ["http://localhost:8080"] | .stop_urls = ["http://localhost:8080/pages/"]' \
            configs/testio_designsystem.json > configs/ci_local.json
          # empty .env: without it the CLI prompts for credentials and hangs
          touch .env && ./docsearch run configs/ci_local.json

  tag-image:
    uses: test-IO/cicd/.github/workflows/docker_tag.yaml@main
    concurrency:
      group: deploy-${{ github.ref }}-${{ github.workflow }}
      cancel-in-progress: false
    needs: [deploy]
    if: |-
      contains(fromJSON('["main", "master"]'), github.ref_name) ||
      startsWith(github.ref_name, 'stage-') ||
      endsWith(github.ref_name, '-testio')
    with:
      image_name: eu.gcr.io/oi-tset/app-designsystem
