name: ci
on:
  push:
    branches:
      - main

jobs:
  # Build all packages and applications, and creates Docker images.
  build:
    uses: ./.github/workflows/build-and-dockerize.yaml
    with:
      imageTag: ${{ github.sha }}
      publishSourceMaps: true
      publishLatest: false
      targets: build
      uploadJavaScriptArtifacts: true
    secrets: inherit

  # Trigger deployment
  deploy:
    name: trigger staging deployment
    needs:
      - build
    runs-on: ubuntu-22.04
    steps:
      - name: Dispatch Deployment
        run: |
          curl --request POST \
            --url 'https://api.github.com/repos/${{ secrets.PRIVATE_REPO_OWNER }}/${{ secrets.PRIVATE_REPO_NAME }}/dispatches' \
            --header 'Accept: application/vnd.github+json' \
            --header 'Authorization: Bearer ${{ secrets.GH_PAT }}' \
            --header 'X-GitHub-Api-Version: 2022-11-28' \
            --header 'Content-Type: application/json' \
            --data '{
            "event_type": "deploy-staging",
            "client_payload": {
              "actor": "${{ github.actor }}",
              "ref": "${{ github.sha }}",
              "pulumiRefresh": "true",
              "hiveAppUsePersistedDocuments": "true"
            }
          }'

  # ESLint and Prettier
  code-style:
    uses: ./.github/workflows/lint.yaml

  # CodeQL
  static-analysis:
    uses: ./.github/workflows/codeql-analysis.yml

  # TypeScript Typecheck and compiler checks
  typescript:
    uses: ./.github/workflows/typescript-typecheck.yaml

  # Deploy Website to CloudFlare Pages
  website:
    uses: ./.github/workflows/website.yaml
    secrets: inherit

  # Deploy Storybook to CloudFlare Pages
  storybook:
    uses: ./.github/workflows/storybook.yaml
    secrets: inherit

  # Release NPM packages and tarballs
  package:
    uses: ./.github/workflows/release-stable.yaml
    secrets: inherit

  # GraphQL Schema Publish
  graphql-schema:
    uses: ./.github/workflows/graphql-schema-publish.yaml
    secrets:
      hiveToken: ${{ secrets.HIVE_TOKEN }}
