name: Release
on:
  push:
    branches:
      - main
  pull_request:
    types:
      - closed
    branches:
      - main

permissions:
  contents: read

jobs:
  release:
    if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.pull_request.merged == true)
    name: Release
    runs-on: ubuntu-latest
    environment: Release
    permissions:
      id-token: write
      contents: write
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Setup node
        uses: actions/setup-node@v3
        with:
          node-version-file: ".node-version"
      - name: Install dependencies
        env:
          npm_config_ignore_scripts: true
        run: npm ci
      - name: Build
        run: npm run build
      - name: Install semantic-release
        run: npm i semantic-release @semantic-release/commit-analyzer @semantic-release/github @semantic-release/npm @semantic-release/release-notes-generator
      - name: Publish and commit version changes
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: bash scripts/release.sh
