name: Master
on:
  push:
    paths:
      - '.github/workflows/push.yml'
      - '.github/workflows/master.yml'
      - 'packages/**'
      - '.eslintrc.js'
      - '.prettierrc'
      - 'lerna.json'
      - 'package.json'
      - 'rollup.config.js'
      - 'yarn.lock'
      - 'rust/cube/**'
      - 'rust/cubesql/**'
    branches:
      - master
jobs:
  latest-tag-sha:
    runs-on: ubuntu-24.04
    outputs:
      sha: ${{ steps.get-tag.outputs.sha }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - id: git-log
        run: git log HEAD~30..HEAD
      - id: get-tag-test
        run: echo "$SHA $(git rev-list -n 1 "$(git tag --contains "$SHA")")"
        env:
          SHA: ${{ github.sha }}
      - id: get-tag
        run: echo "sha=$(git rev-list -n 1 "$(git tag --contains "$SHA")")" >> "$GITHUB_OUTPUT"
        env:
          SHA: ${{ github.sha }}
      - id: get-tag-out
        run: echo "$OUT"
        env:
          OUT: ${{ steps.get-tag.outputs.sha }}

  build_native_linux:
    # Please use minimal possible version of ubuntu, because it produces constraint on glibc
    runs-on: ubuntu-22.04
    timeout-minutes: 60
    name: Build Linux Native backend for Dev image
    container:
      image: cubejs/rust-cross:x86_64-unknown-linux-gnu-31072025-python-3.13

    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Install Rust
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: 1.90.0
          # override: true # this is by default on
          rustflags: ""
          components: rustfmt
          target: x86_64-unknown-linux-gnu
          cache: false
      - uses: Swatinem/rust-cache@v2
        with:
          workspaces: ./rust/cubesql -> target
          key: cubesql-x86_64-unknown-linux-gnu
          shared-key: cubesql-x86_64-unknown-linux-gnu
      - name: Install Node.js 24
        uses: actions/setup-node@v4
        with:
          node-version: 24
      - name: Install Yarn
        run: npm install -g yarn
      - name: Set Yarn version
        run: yarn policies set-version v1.22.22
        # We don't need to install all yarn deps to build native
      - name: Install cargo-cp-artifact
        run: npm install -g cargo-cp-artifact@0.1
      - name: Build native (with Python)
        env:
          PYO3_PYTHON: python3.13
          CARGO_BUILD_TARGET: x86_64-unknown-linux-gnu
        working-directory: ./packages/cubejs-backend-native
        run: yarn run native:build-debug-python
      - name: Store build artifact for dev image
        uses: actions/upload-artifact@v4
        with:
          name: "native-linux-x64-glibc-3.13.node"    # this name is referenced below in docker-image-dev
          path: ./packages/cubejs-backend-native/index.node
          overwrite: true

  docker-image-dev:
    name: Release :dev image
    needs: [latest-tag-sha, build_native_linux]
    runs-on: ubuntu-24.04
    if: (needs['latest-tag-sha'].outputs.sha != github.sha)
    steps:
      - name: Check out the repo
        uses: actions/checkout@v4
      - name: Download backend-native artifact
        uses: actions/download-artifact@v4
        with:
          name: "native-linux-x64-glibc-3.13.node"    # this name is referenced in above in native_linux
          path: ./packages/cubejs-backend-native/
      - name: Login to DockerHub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      # current .dockerignore prevents use of native build
      - name: Unignore native from .dockerignore
        run: |
          grep -v -E "packages/cubejs-backend-native/((native)|(index.node))" .dockerignore > .dockerignore.tmp
          mv .dockerignore.tmp .dockerignore
      - name: Push to Docker Hub
        uses: docker/build-push-action@v6
        with:
          context: ./
          file: ./packages/cubejs-docker/dev.Dockerfile
          platforms: linux/amd64
          push: true
          tags: cubejs/cube:dev
      - name: Update repo description
        uses: peter-evans/dockerhub-description@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
          repository: cubejs/cube
          readme-filepath: ./packages/cubejs-docker/README.md

  trigger-test-suites:
    name: Trigger test suites run
    runs-on: ubuntu-24.04
    needs: [docker-image-dev]
    timeout-minutes: 60
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Dispatch event
        uses: actions/github-script@v7
        with:
          github-token: ${{ secrets.GH_TRIGGER_TOKEN }}
          script: |
            const prUrl = context.payload.pull_request ? context.payload.pull_request.html_url : '';
            const commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${context.sha}`;

            await github.rest.actions.createWorkflowDispatch({
              owner: 'cubedevinc',
              repo: 'sql-api-test-suite',
              workflow_id: 'test_and_run_test_suites.yml',
              ref: 'main',
              inputs: {
                'cube-image': 'cubejs/cube:dev',
                'source-repo': `${context.repo.owner}/${context.repo.repo}`,
                'source-pr-url': prUrl,
                'source-commit-url': commitUrl,
                'initiator': '${{ github.actor }}'
              }
            })
