# This is a basic workflow to help you get started with Actions

name: Release

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  push:
    tags:
      - 'v*'
    branches-ignore:
      - "dev"
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  release:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    - uses: actions/checkout@v2
    
    # Setup NodeJs
    - name: Setup Node.js environment
      uses: actions/setup-node@v1.4.3

    # Pull yarn cache if available
    - uses: actions/cache@v2
      with:
        path: '**/node_modules'
        key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}

    # Install packages
    - name: Install
      run: yarn

    # Run tests
    - name: Test
      run: yarn test
      
    # Try building
    - name: Build
      run: yarn build

    - name: Generate latest changelog
      run: yarn changelog:latest

    - name: Publish to NPM
      run: |
          npm config set '//registry.npmjs.org/:_authToken' "${NPM_AUTH_TOKEN}" --dry-run
          npm publish --tag ${{ github.ref }}
      env:
        NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}

    - name: Create Release
      id: create_release
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
      with:
        tag_name: ${{ github.ref }}
        release_name: ${{ github.ref }}
        body_path: CHANGELOG_LATEST.md
        draft: true
        prerelease: true
