name: Build for Non-Main Branches

on:
  push:
    branches-ignore:
      - main

jobs:
  setup:
    runs-on: ubuntu-latest
    outputs:
      build-unix-time: ${{ steps.variable.outputs.build_unix_time }}
      build-date: ${{ steps.variable.outputs.build_date }}
      docker-tags: ${{ steps.meta.outputs.tags }}
      docker-labels: ${{ steps.meta.outputs.labels }}
      ezbookkeeping-docker-bake-meta-file-path: ${{ steps.variable.outputs.ezbookkeeping_docker_bake_meta_file_path }}
      ezbookkeeping-docker-bake-meta-artifact-name: ${{ steps.variable.outputs.ezbookkeeping_docker_bake_meta_artifact_name }}
      ezbookkeeping-docker-digests-file-path: ${{ steps.variable.outputs.ezbookkeeping_docker_digests_file_path }}
      ezbookkeeping-docker-digests-artifact-name-prefix: ${{ steps.variable.outputs.ezbookkeeping_docker_digests_artifact_name_prefix }}
      ezbookkeeping-package-file-name-prefix: ${{ steps.variable.outputs.ezbookkeeping_package_file_name_prefix }}
      ezbookkeeping-backend-artifact-name-prefix: ${{ steps.variable.outputs.ezbookkeeping_backend_artifact_name_prefix }}
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v6
        with:
          images: |
            ${{ vars.DOCKER_IMAGE_NAME }}
          tags: |
            type=raw,value=dev-${{ github.run_id }}

      - name: Set up variables
        id: variable
        run: |
          echo "build_unix_time=$(date '+%s')" >> "$GITHUB_OUTPUT"
          echo "build_date=$(date '+%Y%m%d')" >> "$GITHUB_OUTPUT"
          echo "ezbookkeeping_docker_bake_meta_file_path=${{ runner.temp }}/bake-meta.json" >> "$GITHUB_OUTPUT"
          echo "ezbookkeeping_docker_bake_meta_artifact_name=ezbookkeeping-build-dev-docker-bake-meta-${{ github.run_id }}" >> "$GITHUB_OUTPUT"
          echo "ezbookkeeping_docker_digests_file_path=${{ runner.temp }}/digests" >> "$GITHUB_OUTPUT"
          echo "ezbookkeeping_docker_digests_artifact_name_prefix=ezbookkeeping-build-dev-digests-${{ github.run_id }}" >> "$GITHUB_OUTPUT"
          echo "ezbookkeeping_package_file_name_prefix=ezbookkeeping-dev-package-${{ github.run_id }}" >> "$GITHUB_OUTPUT"
          echo "ezbookkeeping_backend_artifact_name_prefix=ezbookkeeping-build-dev-backend-${{ github.run_id }}" >> "$GITHUB_OUTPUT"

      - name: Rename docker bake meta file
        run: |
          mv "${{ steps.meta.outputs.bake-file }}" "${{ steps.variable.outputs.ezbookkeeping_docker_bake_meta_file_path }}"

      - name: Upload docker bake meta artifact
        uses: actions/upload-artifact@v7
        with:
          name: ${{ steps.variable.outputs.ezbookkeeping_docker_bake_meta_artifact_name }}
          path: ${{ steps.variable.outputs.ezbookkeeping_docker_bake_meta_file_path }}
          if-no-files-found: error

  build-linux-docker-and-package-x86:
    runs-on: ubuntu-24.04
    needs:
      - setup
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - uses: ./.github/actions/build-linux-docker-and-package
        with:
          build-unix-time: ${{ needs.setup.outputs.build-unix-time }}
          build-date: ${{ needs.setup.outputs.build-date }}
          check-3rd-api: ${{ vars.CHECK_3RD_API }}
          skip-tests: ${{ vars.SKIP_TESTS }}
          platform: linux/amd64
          platform-name: linux-amd64
          docker-push: false
          docker-image-name: ${{ vars.DOCKER_IMAGE_NAME }}
          docker-bake-meta-file-path: ${{ needs.setup.outputs.ezbookkeeping-docker-bake-meta-file-path }}
          docker-bake-meta-artifact-name: ${{ needs.setup.outputs.ezbookkeeping-docker-bake-meta-artifact-name }}
          docker-bake-digests-file-path: ${{ needs.setup.outputs.ezbookkeeping-docker-digests-file-path }}
          docker-bake-digests-artifact-name-prefix: ${{ needs.setup.outputs.ezbookkeeping-docker-digests-artifact-name-prefix }}
          package-file-name-prefix: ${{ needs.setup.outputs.ezbookkeeping-package-file-name-prefix }}

  build-linux-docker-and-package-arm:
    runs-on: ubuntu-24.04-arm
    needs:
      - setup
    strategy:
      matrix:
        include:
          - platform: linux/arm64/v8
            platform-name: linux-arm64
          - platform: linux/arm/v7
            platform-name: linux-armv7
          - platform: linux/arm/v6
            platform-name: linux-armv6
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - uses: ./.github/actions/build-linux-docker-and-package
        with:
          build-unix-time: ${{ needs.setup.outputs.build-unix-time }}
          build-date: ${{ needs.setup.outputs.build-date }}
          check-3rd-api: ${{ vars.CHECK_3RD_API }}
          skip-tests: ${{ vars.SKIP_TESTS }}
          platform: ${{ matrix.platform }}
          platform-name: ${{ matrix.platform-name }}
          docker-push: false
          docker-image-name: ${{ vars.DOCKER_IMAGE_NAME }}
          docker-bake-meta-file-path: ${{ needs.setup.outputs.ezbookkeeping-docker-bake-meta-file-path }}
          docker-bake-meta-artifact-name: ${{ needs.setup.outputs.ezbookkeeping-docker-bake-meta-artifact-name }}
          docker-bake-digests-file-path: ${{ needs.setup.outputs.ezbookkeeping-docker-digests-file-path }}
          docker-bake-digests-artifact-name-prefix: ${{ needs.setup.outputs.ezbookkeeping-docker-digests-artifact-name-prefix }}
          package-file-name-prefix: ${{ needs.setup.outputs.ezbookkeeping-package-file-name-prefix }}

  build-windows-backend:
    needs:
      - setup
    runs-on: windows-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - uses: ./.github/actions/build-windows-backend
        with:
          build-unix-time: ${{ needs.setup.outputs.build-unix-time }}
          build-date: ${{ needs.setup.outputs.build-date }}
          check-3rd-api: ${{ vars.CHECK_3RD_API }}
          skip-tests: ${{ vars.SKIP_TESTS }}
          backend-artifact-name-prefix: ${{ needs.setup.outputs.ezbookkeeping-backend-artifact-name-prefix }}

  build-macos-backend:
    needs:
      - setup
    runs-on: macos-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - uses: ./.github/actions/build-macos-backend
        with:
          architecture: arm64
          build-unix-time: ${{ needs.setup.outputs.build-unix-time }}
          build-date: ${{ needs.setup.outputs.build-date }}
          check-3rd-api: ${{ vars.CHECK_3RD_API }}
          skip-tests: ${{ vars.SKIP_TESTS }}
          backend-artifact-name-prefix: ${{ needs.setup.outputs.ezbookkeeping-backend-artifact-name-prefix }}

  build-windows-package:
    needs:
      - setup
      - build-windows-backend
      - build-linux-docker-and-package-x86
    runs-on: windows-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - uses: ./.github/actions/build-windows-package
        with:
          package-file-name-prefix: ${{ needs.setup.outputs.ezbookkeeping-package-file-name-prefix }}
          backend-artifact-name-prefix: ${{ needs.setup.outputs.ezbookkeeping-backend-artifact-name-prefix }}

  build-macos-package:
    needs:
      - setup
      - build-macos-backend
      - build-linux-docker-and-package-x86
    runs-on: macos-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - uses: ./.github/actions/build-macos-package
        with:
          architecture: arm64
          package-file-name-prefix: ${{ needs.setup.outputs.ezbookkeeping-package-file-name-prefix }}
          backend-artifact-name-prefix: ${{ needs.setup.outputs.ezbookkeeping-backend-artifact-name-prefix }}
