name: Build

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

# From: https://alexanderhose.com/how-to-securely-connect-github-actions-to-aws/
permissions: # Permission can be added at job level or workflow level
  id-token: write # This is required for requesting the JWT
  contents: read # This is required for actions/checkout

jobs:
  build:
    name: Build distribution 📦
    runs-on: ubuntu-latest

    env:
      AWS_REGION: us-east-1
      AWS_ROLE_ARN: arn:aws:iam::383473033976:role/PipPackageUploaderRole
      PIP_PACKAGE_BUCKET: prod-ecb-bucketsnestedstackbucke-pipbucket3459f2d7-gmsm8w3nvzgq

    steps:
      - uses: actions/checkout@v4
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.x"
      - name: Install pypa/build
        run: >-
          python3 -m
          pip install
          build
          --user
      - name: Install boto3
        run: |
          python3 -m pip install boto3
      - name: Build a binary wheel and sdist
        run: python -m build --outdir dist/earningscall-base
      - name: Store the distribution packages
        uses: actions/upload-artifact@v4
        with:
          name: python-package-distributions
          path: dist/
          if-no-files-found: error
      - name: List all files in dist
        run: |
          find dist/
      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: ${{ env.AWS_ROLE_ARN }}
          aws-region: ${{ env.AWS_REGION }}
      - name: Sts GetCallerIdentity
        run: |
          aws sts get-caller-identity
      - name: AWS List S3 Files
        run: |
          aws s3 ls s3://${{ env.PIP_PACKAGE_BUCKET }}
      - name: AWS Upload Files
        run: |
          aws s3 cp dist/ s3://${{ env.PIP_PACKAGE_BUCKET }} --recursive
      - name: Generate Index Files
        shell: bash
        run: |
          python generate-index.py --s3-bucket-name ${{ env.PIP_PACKAGE_BUCKET }}
      - name: Upload Index Files
        run: |
          aws s3 cp dist/ s3://${{ env.PIP_PACKAGE_BUCKET }} --recursive --exclude "*" --include "*index.html"
