name: ci
on:
  push:
    branches:
      - main

concurrency:
  group: ci-main
  cancel-in-progress: false

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

  # Build all packages and applications, and creates Docker images.
  build:
    needs:
      - package
    uses: ./.github/workflows/build-and-dockerize.yaml
    with:
      imageTag:
        # prettier-ignore
        ${{ needs.package.outputs.hivePublish == 'true' && needs.package.outputs.hiveVersion || github.sha }}
      publishSourceMaps: true
      publishLatest: ${{ needs.package.outputs.hivePublish == 'true' }}
      targets: build
      uploadJavaScriptArtifacts: true
    secrets: inherit

  # Dockerize the CLI in case a CLI version was released
  publish_docker_cli:
    needs:
      - package
    uses: ./.github/workflows/dockerize-cli.yaml
    if: ${{ needs.package.outputs.cliPublish == 'true' }}
    with:
      cliVersion: ${{ needs.package.outputs.cliVersion }}
      publishLatest: true
    secrets: inherit

  # Trigger deployment for staging
  deploy:
    name: trigger staging deployment
    needs:
      - build
    runs-on: ubuntu-22.04
    if: ${{ vars.ENABLE_STAGING_DEPLOYMENT == '1' }}
    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

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

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