# Publish packages
# This workflow affects all our npm packages, so just `layout` for now.
#
#	1. increment the version in package.json
#	2. push the new version to the master branch
#	3. build the project (if applicable)
#	4. publish the project

# It relies on the `NPM_TOKEN` secret being available in this repo.

name: publish-layout

on:
  push:
    branches:
    - master
    paths:
    - 'layout/**'

jobs:
  build-and-deploy:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [10.x]

    steps:
    - uses: actions/checkout@v2

    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}

    - name: Build project
      run: |
        cd layout
        npm install
        npm run build-bundle --if-present

    - name: Increment project version
      run: |
        cd layout
        npm version patch

    - name: Commit changes
      run: |
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"
        git commit -m "increment package version" -a

    - name: Ensure we have latest changes
      run: |
        git rebase origin/master
        git pull
        git fetch

    - name: Push changes
      uses: ad-m/github-push-action@master
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}

    - name: Publish project
      run: |
        cd layout
        echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > .npmrc
        npm publish --access public
        rm .npmrc