name: DEUNA Vault Deploy

on:
  push:
    branches:
      - develop
      - staging
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [16.x]

    steps:
      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-2

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

      - name: Set NPM_TAG
        run: |
          if [ ${{ github.ref }} == 'refs/heads/main' ]; then
            echo "MAIN"
            echo "NPM_TAG=latest" >> $GITHUB_ENV
          elif [[ ${{ github.ref }} == 'refs/heads/develop' || ${{ github.ref }} == 'refs/heads/staging' ]]; then
            echo "DEVELOP"
            echo "NPM_TAG=beta" >> $GITHUB_ENV
          fi

      - name: Publish vault widget
        run: npm run login
      - run: echo "NPMCR" && cat ~/.npmrc
      - run: npm install
      - run: npm run build --if-present
      - run: npm run post-build
      - uses: JS-DevTools/npm-publish@v1
        with:
          token: ${{ secrets.NPM_TOKEN }}
          access: public
          tag: ${{ env.NPM_TAG }}
