name: Build, Test and Deploy

on: push

env:
  NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
  CDN_BUCKET: mira-public-assets

jobs:
  build-test-deploy:
    name: Build, Test and Deploy
    runs-on: ubuntu-latest
    # These permissions are needed to get a token from the Github OIDC provider.
    permissions:
      id-token: write
      contents: read
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          role-to-assume: arn:aws:iam::761633760558:role/RaydiantGithubActionsRaydiantSDK
          role-session-name: github-actions-raydiant-sdk-deploy
          aws-region: us-west-2

      # Setup node is supposed to generate this file for us but for some reason
      # publish fails with "No token found" unless we create our own at the root repo.
      # Setup node won't generate an npmrc file if one already exists.
      - name: Generate .npmrc
        run: |
          cat << EOF > ${GITHUB_WORKSPACE}/.npmrc
          //registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}
          registry=https://registry.npmjs.org/
          always-auth=true
          EOF

      - name: Setup node
        uses: actions/setup-node@v3
        with:
          cache: 'yarn'
          node-version: 18

      - name: Install dependencies
        run: yarn install

      - name: Build and run test suite
        run: yarn test

      - name: Publish to NPM and CDN
        run: yarn deploy
        if: github.ref == 'refs/heads/main'
