name: Deploy

on:
  push:
    branches:
      - 'master'
    tags:
      - 'v*'

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: 16
          registry-url: https://registry.npmjs.org/
      - run: npm ci
      - run: npm run build:production
      - name: SonarQube Scan 
        uses: sonarsource/sonarqube-scan-action@master
        env:
          SONAR_TOKEN: ${{ secrets.SONARQUBE_TOKEN }}
          SONAR_HOST_URL: ${{ secrets.SONARQUBE_HOST_URL }}
        with:
          args: >
            -Dsonar.projectKey=UserFeedbackForm
            -Dsonar.javascript.lcov.reportPaths=./coverage/lcov.info
            -Dsonar.coverage.exclusions=**/*.config.js,**/coverage/**
      # Only trigger if it's release tag. 
      - name: Create Release
        if: ${{ endsWith(github.ref, 'master') == false }}
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          draft: false
          prerelease: false
      - name: Upload release asset
        if: ${{ endsWith(github.ref, 'master') == false }}
        id: upload-release-asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps
          asset_path: ./dist/user-feedback-form.js
          asset_name: user-feedback-form.min.js
          asset_content_type: text/javascript
      - name: Publish npm package
        if: ${{ endsWith(github.ref, 'master') == false }}
        run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{secrets.NPM_AUTH_TOKEN}}
