name: Release Obsidian Plugin
on:
  push:
    # Sequence of patterns matched against refs/tags
    tags:
    - '*' # Push events to matching any tag format, i.e. 1.0, 20.15.10
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      id-token: write
      attestations: write
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 0 # otherwise, you will failed to push refs to dest repo
        submodules: recursive
    - name: Use Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '24.x' # You might need to adjust this value to your own version
    # Get the version number and put it in a variable
    - name: Get Version
      id: version
      run: |
        echo "tag=$(git describe --abbrev=0 --tags)" >> $GITHUB_OUTPUT
    # Build the plugin
    - name: Build
      id: build
      run: |
        npm ci
        npm run build --if-present
    # Attest
    - name: Attest Plugin Artifacts
      uses: actions/attest-build-provenance@v4
      with:
          subject-path: |
              main.js
              manifest.json
              styles.css
    # Package the required files into a zip
    - name: Package
      run: |
        mkdir ${{ github.event.repository.name }}
        cp main.js manifest.json styles.css README.md ${{ github.event.repository.name }}
        zip -r ${{ github.event.repository.name }}.zip ${{ github.event.repository.name }}
    - name: Create Release and Upload Assets
      uses: softprops/action-gh-release@v2
      with:
        files: |
          ${{ github.event.repository.name }}.zip
          main.js
          manifest.json
          styles.css
        name: ${{ steps.version.outputs.tag }}
        tag_name: ${{ steps.version.outputs.tag }}
        draft: true