name: Release

env:
  CI: "true"

on:
  push:
    branches:
      # https://semantic-release.gitbook.io/semantic-release/usage/configuration#branches
      - master
      - next
      - next-major
      - beta
      - alpha
      # https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#patterns-to-match-branches-and-tags
      - '[0-9]+.x'        # N.x
      - '[0-9]+.x.x'      # N.x.x
      - '[0-9]+.[0-9]+.x' # N.N.x

jobs:
  npm-publish:
    name: npm publish

    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Cache node modules
        uses: actions/cache@v3
        env:
          cache-name: cache-node-modules
        with:
          path: ~/.npm
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-build-${{ env.cache-name }}-
            ${{ runner.os }}-build-
            ${{ runner.os }}-

      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: 16

      - name: Install
        run: npm ci

      - name: Build
        run: npm run build

      - name: Semantic Release
        if: success()
        env:
          GIT_AUTHOR_NAME: Scale Bot
          GIT_AUTHOR_EMAIL: scale-bot@scaleleap.com
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
          SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
        run: npm run semantic-release
