name: Github Container Registry (GHCR)
on:
  push:
    branches: [ "main" ]
    tags: ['*']

env:
  REGISTRY: ghcr.io
  PLATFORMS: linux/amd64,linux/arm64
  BASE_IMAGE: ${{ github.repository }}
  ARCHIVE_IMAGE: performancecopilot/archive-analysis

jobs:
  build-and-push-image:
    if: github.repository == 'performancecopilot/pcp'
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v7

      - name: Log in to the container registry
        uses: docker/login-action@v4
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v4

      - name: Extract tags and labels for base image
        id: meta_base
        uses: docker/metadata-action@v6
        with:
          images: ${{ env.REGISTRY }}/${{ env.BASE_IMAGE }}
          tags: |
            type=raw,value=latest,enable={{is_default_branch}}
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=semver,pattern={{major}}
            type=ref,event=tag
            type=sha,prefix={{branch}}-,enable=${{ github.ref_type != 'tag' }}
            type=raw,value={{branch}},enable=${{ github.ref_type != 'tag' }}

      - name: Extract tags and labels for archive image
        id: meta_archive
        uses: docker/metadata-action@v6
        with:
          images: ${{ env.REGISTRY }}/${{ env.ARCHIVE_IMAGE }}
          tags: |
            type=raw,value=latest,enable={{is_default_branch}}
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=semver,pattern={{major}}
            type=ref,event=tag
            type=sha,prefix={{branch}}-,enable=${{ github.ref_type != 'tag' }}
            type=raw,value={{branch}},enable=${{ github.ref_type != 'tag' }}

      - name: Build and push base image
        uses: docker/build-push-action@v7
        with:
          context: .
          file: ./build/containers/pcp/Containerfile
          push: true
          tags: ${{ steps.meta_base.outputs.tags }}
          labels: ${{ steps.meta_base.outputs.labels }}
          platforms: ${{ env.PLATFORMS }}
          cache-from: type=gha
          cache-to: type=gha,mode=max

      - name: Build and push archive-analysis image
        uses: docker/build-push-action@v7
        with:
          context: .
          file: ./build/containers/archive-analysis/Containerfile
          push: true
          tags: ${{ steps.meta_archive.outputs.tags }}
          labels: ${{ steps.meta_archive.outputs.labels }}
          platforms: ${{ env.PLATFORMS }}
          cache-from: type=gha
          cache-to: type=gha,mode=max

      - name: Image digest
        run: |
          echo "Base image pushed with tags: ${{ steps.meta_base.outputs.tags }}"
          echo "Archive image pushed with tags: ${{ steps.meta_archive.outputs.tags }}"