name: Operator CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
    paths:
      - 'pkg/**'
      - 'cmd/**'
      - 'go.*'

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      
      - name: Set up Go
        uses: actions/setup-go@v4
        with:
          go-version: '1.22'
          
      - name: Clear module cache
        run: |
          go clean -modcache
          rm -rf ~/.cache/go-build
          
      - name: Run linter
        uses: golangci/golangci-lint-action@v3
        with:
          version: latest
          skip-cache: true

  test:
    needs: lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      
      - name: Set up Go
        uses: actions/setup-go@v4
        with:
          go-version: '1.22'
          
      - name: Clear module cache
        run: |
          go clean -modcache
          rm -rf ~/.cache/go-build
          
      - name: Install dependencies
        run: go mod download
        
      - name: Run tests
        run: go test -v ./...
        
  build:
    needs: test
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    
    steps:
      - uses: actions/checkout@v3
      
      - name: Install Podman
        run: |
          sudo apt-get update
          sudo apt-get install -y podman
          
      - name: Login to GitHub Container Registry
        if: github.event_name != 'pull_request'
        run: |
          echo "${{ secrets.GITHUB_TOKEN }}" | podman login ghcr.io -u ${{ github.actor }} --password-stdin
          
      - name: Build and push
        run: |
          podman build -t ghcr.io/${{ github.repository_owner }}/container-mom-operator:${{ github.sha }} -f build/Dockerfile.operator .
          podman tag ghcr.io/${{ github.repository_owner }}/container-mom-operator:${{ github.sha }} ghcr.io/${{ github.repository_owner }}/container-mom-operator:latest
          if [ "${{ github.event_name }}" != "pull_request" ]; then
            podman push ghcr.io/${{ github.repository_owner }}/container-mom-operator:${{ github.sha }}
            podman push ghcr.io/${{ github.repository_owner }}/container-mom-operator:latest
          fi 