# Copyright 2024 Ronan LE MEILLAT
# Licensed under the Affero GPL v3
name: Build and Deploy

on:
  release:
    types: [created]
  workflow_dispatch:

permissions:
  contents: write
  pages: write
  id-token: write
  packages: write
  attestations: write

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Set up Node.js
        uses: actions/setup-node@v6
        with:
          node-version: "20"
          registry-url: https://registry.npmjs.org/

      - name: Install dependencies
        run: npm ci

      - name: Build the project
        run: npm run build

      - name: Run tests
        run: npm test

      - name: Deploy to GitHub Pages
        continue-on-error: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          git config --global user.name 'github-actions[bot]'
          git config --global user.email 'github-actions[bot]@users.noreply.github.com'
          git add dist
          git commit -m 'Update dist' dist/*
          git push origin main

      - name: Publish to npm
        env:
          NODE_AUTH_TOKEN: ${{secrets.NPMJS_TOKEN}}
        run: npm publish
        continue-on-error: true
