name: deploy-npm

on:
  push:
    branches:
      - 'npm-*'
    tags:
      - '[0-9]+.[0-9]+.[0-9]+'

jobs:
  tests:
    strategy:
      matrix:
        os: [macos-latest,windows-latest,ubuntu-latest]
        node-version: [15.x, 14.x, 12.x]
    
    runs-on: ${{ matrix.os }}
    
    steps:
    - uses: actions/checkout@v2

    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}

    - run: npm install
    - run: npm test

  merge-to-versions:
    needs: tests

    runs-on: ubuntu-latest

    steps:
    - name: Merge onto version branches
      uses: alagos/regex-merge@v1.1
      with:
        accessToken: ${{ secrets.WORKFLOW_ACCESS_TOKEN }}
        headBranch: ${{ github.sha }}
        branchRegex: '^sdk-.+'

  deploy:
    needs: tests

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Setup .npmrc to publish to NPM
      uses: actions/setup-node@v2
      with:
        node-version: 12.x
        registry-url: https://registry.npmjs.org

    - name: Setup Git user
      run: |
        git config user.name "github-actions[bot]"
        git config user.email "41898282+github-actions[bot]@users.noreply.github.com"

    - name: Bump version to tag
      run: npm version ${GITHUB_REF##*/}

    - name: Publish to NPM
      run: npm publish --access public
      env:
        NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
