name: NPM

# About this workflow:
# It is triggered on push events to branches production and testing. Then it performs a checkout of the current repo
# and sets up a node environment (v12). Following, it will run `npm ci` to build the package. Finally, the `npm
# publish` command will be run, when on branch testing it will run `npm publish --tag beta` to publish it under the
# beta flag on npm. Note: if the package version is not bumped manually the publish step will fail because we can not
# publish to an existing version (see the npm_version_bump_and_publish.yml workflow to solve this).

# Required secrets:
# - NPM_AUTH_TOKEN: required for publishing to npm
# - SLACK_WEBHOOK_URL: required for posting a message in #software

# Optional secrets:
# - SSH_KEY: if `npm ci` needs to install private npm packages

# Note: make sure to commit package-lock.json, this is needed for `npm ci`.

# Note: add `.github` to .npmignore

# Note: when publishing a scoped npm package (e.g. @athombv/node-my-package) append `--access public` to the `npm publish` command, by default scoped packages are published privately.
# For more information see: https://docs.npmjs.com/creating-and-publishing-an-org-scoped-package

# Defines the trigger for this action, in general you want it to run when pushing to production | testing. For more
# information see: https://help.github.com/en/actions/automating-your-workflow-with-github-actions/events-that-trigger-workflows#about-workflow-events)
on:
  workflow_dispatch:
    inputs:
      releaseType:
        description: 'Release type: major, minor or patch.'
        required: true

jobs:
  release:
    name: Build & Deploy to NPM

    # Only run this job if initiator is not the Homey Github Actions Bot to prevent loops
    if: github.actor != 'homey-bot' && github.ref == 'refs/heads/production'

    runs-on: ubuntu-latest
    steps:

      # Checks out the current repository.
      - name: Checkout git repository
        uses: actions/checkout@v2
        with:
          # The token below is only necessary if you want to push the version bump to a protected branch
          token: ${{ secrets.HOMEY_GITHUB_ACTIONS_BOT_PERSONAL_ACCESS_TOKEN }}

      # Set git config to reflect Homey Github Actions Bot user
      - name: Set up HomeyGithubActionsBot git user
        run: |
          git config --local user.email "sysadmin+githubactions@athom.com"
          git config --local user.name "Homey Github Actions Bot"

      # Configures a Node.js environment.
      - name: Set up node 12 environment
        uses: actions/setup-node@v1
        with:
          node-version: '12'
          # Needed for publishing to npm
          registry-url: 'https://registry.npmjs.org'

      # Only use the ssh-agent action below if your repository has private git dependencies (see README.md for
      # instructions on how to configure an ssh key).
      - uses: webfactory/ssh-agent@v0.4.1
        env:
          SSH_KEY: ${{ secrets.SSH_KEY }}
        if: env.SSH_KEY != null
        with:
          ssh-private-key: ${{ env.SSH_KEY }}

      # Run `npm ci` to re-create your local environment (make sure to commit your package-lock.json!).
#       Should be skipped: https://github.com/athombv/node-linux-device/blob/master/package.json#L40
#       - name: Build
#         run: npm ci

      - name: Version bump patch
        if: github.event.inputs.releaseType == 'patch'
        run: |
          npm version patch
          git pull
          git push --follow-tags
      - name: Version bump minor
        if: github.event.inputs.releaseType == 'minor'
        run: |
          npm version minor
          git pull
          git push --follow-tags
      - name: Version bump major
        if: github.event.inputs.releaseType == 'major'
        run: |
          npm version major
          git pull
          git push --follow-tags

      # Publish when this action is running on branch production (when publishing a scoped package add --access public)
      - name: Publish
        if: github.ref == 'refs/heads/production'
        run: |
          npm publish
          VERSION="$(node -p "require('./package.json').version")"
          echo package_version=${VERSION} >> $GITHUB_ENV
          echo $package_version
        env:
          NODE_AUTH_TOKEN: ${{secrets.NPM_AUTH_TOKEN}}

       # Post a Slack notification in #software on success/failure
      - name: Slack notify
        if: always()
        uses: innocarpe/actions-slack@v1
        with:
          status: ${{ job.status }}
          success_text: '${{github.repository}} - published v${{env.package_version}} to npm 🚀'
          failure_text: '${{github.repository}} - failed to publish to npm'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Required
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # Required
