name: Release

on:
  push:
    branches:
      - master
      - next

jobs:
  semantic-release:
    name: Build & Publish
    if: "!contains(github.event.head_commit.message, 'SKIP CI')"
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        node-version: [10.x]
    steps:

      # Setup Git Repository & Node
      #
      - name: Checkout branch (${{ github.ref }})
        uses: actions/checkout@v2
      - name: Configure git
        run: |
          git config user.name "Rafe Goldberg"
          git config user.email "rafegoldberg@gmail.com"
      - name: Setup node (v${{ matrix.node-version }})
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
      - name: Install & compile.
        run: |
          npm ci
          npm run build --if-present
        env:
          CI: true

      # Version, build, tag, and publish the new release;
      #
      - name: Build new release
        run:  npm run release # see: .releaserc for config
        env:
          GH_TOKEN: ${{ secrets.GITHUB_ADMIN || secrets.GITHUB_TOKEN }} # auth to push release to remote repo
          NPM_TOKEN: ${{ secrets.NPM_TOKEN || secrets.GITHUB_TOKEN }}   # auth to publish package to registry

      # Commit and push the new release.
      #
      - name: Sync to remote
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}
        continue-on-error: true
      - name: Sync to next
        if: "github.ref == 'refs/heads/master'" # merge changes in to @next remote when releasing on @latest (master)
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: next
        continue-on-error: true

      # Build and publish the styleguide to GH pages.
      #
      - name: Publish Styleguide
        if: "github.ref == 'refs/heads/master'" # only update from master
        run: |
          npm run docs:build --if-present
          git add docs/
          git commit -m "BUILD STYLEGUIDE"
          git push origin `git subtree split --prefix docs HEAD`:gh-pages --force
          # see: https://stackoverflow.com/a/13403588/1341949
        env:
          CI: true

      # Save the bundle as a GitHub workflow artifact.
      #
      - name: Save workflow artifacts.
        uses: actions/upload-artifact@v1
        with:
          name: UI Library
          path: .bundles
        continue-on-error: true
