# Build a macos release from the luxd repo

name: build-macos-release

# Controls when the action will run.
on:
  workflow_dispatch:
    inputs:
      tag:
        description: 'Tag to include in artifact name'
        required: true
  workflow_call:
    inputs:
      tag:
        description: 'Tag to include in artifact name'
        required: true
        type: string
  push:
    tags:
      - "*"

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # Cross-compile both darwin/amd64 and darwin/arm64 from Linux. The build
  # uses CGO_ENABLED=0 + GOOS=darwin GOARCH=$arch — pure Go, no Darwin
  # toolchain required. We run on the self-hosted `lux-build` ARC pool
  # rather than `macos-13` (which has long GitHub-hosted queues) to keep
  # the release pipeline unblocked.
  build-mac:
    strategy:
      fail-fast: false
      matrix:
        goarch: [amd64, arm64]
    runs-on: [self-hosted, linux, amd64]
    permissions:
      id-token: write
      contents: read

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v4
      - uses: ./.github/actions/setup-go-for-project
      - run: go version

      # Runs a single command using the runners shell
      - name: Build the luxd binary (cross-compile via GOOS/GOARCH)
        run: CGO_ENABLED=0 GOOS=darwin GOARCH=${{ matrix.goarch }} ./scripts/run_task.sh build

      - name: Try to get tag from git
        if: "${{ github.event.inputs.tag == '' }}"
        id: get_tag_from_git
        run: |
          echo "TAG=${GITHUB_REF/refs\/tags\//}" >> "$GITHUB_ENV"
        shell: bash

      - name: Try to get tag from workflow dispatch
        if: "${{ github.event.inputs.tag != '' }}"
        id: get_tag_from_workflow
        run: |
          echo "TAG=${{ github.event.inputs.tag }}" >> "$GITHUB_ENV"
        shell: bash

      - name: Create zip file with CLI-compatible naming
        run: |
          # CLI expects: node-macos-{arch}-{version}.zip containing build/luxd
          mkdir -p build
          if ! command -v zip >/dev/null 2>&1; then
            sudo apt-get update && sudo apt-get install -y zip
          fi
          zip "node-macos-${{ matrix.goarch }}-${TAG}.zip" build/luxd
        env:
          TAG: ${{ env.TAG }}

      - name: Save as Github artifact
        uses: actions/upload-artifact@v7
        with:
          name: build-darwin-${{ matrix.goarch }}
          path: node-macos-${{ matrix.goarch }}-${{ env.TAG }}.zip

      - name: Cleanup
        run: |
          rm -rf ./build
