name: CI/CD

on:
  push: # Runs whenever a commit is pushed to the repository
  workflow_call: # Allows another workflow to call this one
  workflow_dispatch: # Allows you to run this workflow manually from the Actions tab

concurrency:
  group: "${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}"
  cancel-in-progress: true

permissions:
  contents: write # publish a GitHub release
  pages: write # deploy to GitHub Pages
  issues: write # comment on released issues
  pull-requests: write # comment on released pull requests

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    env:
      TRIGGER_DEPLOY: ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/develop' }}
    steps:
      - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4

      - uses: actions/setup-node@1a4442cacd436585916779262731d5b162bc6ec7 # v3
        with:
          cache: "npm"
          node-version-file: ".nvmrc"

      - name: Info
        run: |
          cat <<EOF
          Node version: $(node --version)
          NPM version: $(npm --version)
          GitHub ref: ${{ github.ref }}
          GitHub head ref: ${{ github.head_ref }}
          EOF

      - run: npm ci

      - run: npm test
        env:
          GOOGLE_CLIENT_EMAIL: ${{ secrets.GOOGLE_CLIENT_EMAIL }}
          GOOGLE_PRIVATE_KEY: ${{ secrets.GOOGLE_PRIVATE_KEY }}

      - name: Run semantic-release
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: npx --no -- semantic-release
