name: Create release

on:
  push:
    tags:
      - "v*"

defaults:
    run:
        working-directory: avm-runner-background

jobs:
  npm-publish:
    name: "Publish"
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Set env
        run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV

     ### Set version
      - name: Set version to ${{ env.RELEASE_VERSION }}
        run: npm version --new-version --allow-same-version ${{ env.RELEASE_VERSION }} --no-git-tag-version

      ### Publish to NPM registry
      - uses: actions/setup-node@v1
        with:
          node-version: '16'
          registry-url: 'https://registry.npmjs.org'

      ### release AVM runner
      - name: publish AVM runner
        env: 
            NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: |
          npm install
          ./build_runner.sh
          npm run build
          npm publish --access public

      ### Create a release
      - name: Build Changelog
        id: changelog
        uses: mikepenz/release-changelog-builder-action@v1
        with:
          configuration: ".github/workflows/changelog_config.json"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          
      - name: Release
        id: release
        uses: softprops/action-gh-release@v1
        with:
          name: Background AVM runner ${{ env.RELEASE_VERSION }}
          tag_name: ${{ env.RELEASE_VERSION }}
          body: ${{steps.changelog.outputs.changelog}}
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}