name: Build

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  build:
    name: Build Binaries
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - goos: linux
            goarch: amd64
          - goos: linux
            goarch: arm64
          - goos: darwin
            goarch: amd64
          - goos: darwin
            goarch: arm64
          - goos: windows
            goarch: amd64
          - goos: windows
            goarch: arm64
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.26.4'

      - name: Get version info
        id: version
        run: |
          VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo "dev")
          COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
          DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "commit=$COMMIT" >> $GITHUB_OUTPUT
          echo "date=$DATE" >> $GITHUB_OUTPUT

      - name: Build binary
        env:
          GOOS: ${{ matrix.goos }}
          GOARCH: ${{ matrix.goarch }}
          CGO_ENABLED: 0
        run: |
          BINARY_NAME="dtctl-${{ matrix.goos }}-${{ matrix.goarch }}"
          if [ "${{ matrix.goos }}" = "windows" ]; then
            BINARY_NAME="${BINARY_NAME}.exe"
          fi
          
          go build \
            -ldflags "-s -w -X github.com/dynatrace-oss/dtctl/pkg/version.Version=${{ steps.version.outputs.version }} -X github.com/dynatrace-oss/dtctl/pkg/version.Commit=${{ steps.version.outputs.commit }} -X github.com/dynatrace-oss/dtctl/pkg/version.Date=${{ steps.version.outputs.date }}" \
            -o "bin/${BINARY_NAME}" \
            .

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: dtctl-${{ matrix.goos }}-${{ matrix.goarch }}
          path: bin/*
          if-no-files-found: error
          retention-days: 30
