name: Docker Build and Deploy

on:
  push:
    branches: [ main ]
    paths:
      - 'containers/**'
      - 'Dockerfile*'
      - '.gitea/workflows/docker.yml'

jobs:
  build:
    runs-on: ubuntu-latest
    name: Build Podman Images
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up Podman
        run: |
          sudo apt-get update
          sudo apt-get install -y podman
          podman --version

      - name: Login to Container Registry
        run: |
          echo ${{ secrets.REGISTRY_PASSWORD }} | podman login ${{ secrets.REGISTRY_URL }} --username ${{ secrets.REGISTRY_USERNAME }} --password-stdin

      - name: Build and push images
        run: |
          # 构建应用镜像
          for dockerfile in containers/applications/*/Dockerfile; do
            if [ -f "$dockerfile" ]; then
              app_name=$(basename $(dirname "$dockerfile"))
              echo "Building $app_name"
              podman build -t "${{ secrets.REGISTRY_URL }}/$app_name:${{ github.sha }}" -f "$dockerfile" .
              podman push "${{ secrets.REGISTRY_URL }}/$app_name:${{ github.sha }}"
            fi
          done

  deploy-nomad:
    runs-on: ubuntu-latest
    name: Deploy to Nomad Cluster
    needs: build
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Deploy to Nomad
        run: |
          # 这里可以通过 SSH 连接到 Nomad 管理节点进行部署
          echo "Deploy to Nomad placeholder"
          # 示例命令: nomad job run -var "image_tag=${{ github.sha }}" jobs/app.nomad
