name: Release

on:
  push:
    branches:
      - main

jobs:
  Release:
    name: release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v2
        with:
          always-auth: true
          node-version: 16
          registry-url: 'https://npm.pkg.github.com/'
          scope: '@promotedai'

      - name: Install dependencies
        run: npm i && npm config set access public

      - name: Build
        run: npm run build

      - name: Semantic Release
        uses: cycjimmy/semantic-release-action@v2
        id: semantic
        with:
          branch: main
          extra_plugins: |
            @semantic-release/git
            @semantic-release/changelog
        env:
          GITHUB_TOKEN: ${{ secrets.PROMOTED_PUBLIC_DEPLOYER_GITHUB_ACTIONS }}
          NPM_TOKEN: ${{ secrets.PROMOTED_PUBLIC_DEPLOYER_NPM_TOKEN }}

      - name: Push updates to branch for major version
        if: steps.semantic.outputs.new_release_published == 'true'
        run: git push https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git HEAD:refs/heads/v${{steps.semantic.outputs.new_release_major_version}}
        env:
          GITHUB_TOKEN: ${{ secrets.PROMOTED_PUBLIC_DEPLOYER_GITHUB_ACTIONS }}
