name: Build and Release Docker Image
on: [push]


jobs:
  build:
    runs-on: ubuntu-latest
    name: Build image and upload artifact
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

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

      # Build a single-architecture image (using linux/amd64) so we can load it locally.
      - name: Build Docker image for artifact
        run: |
          docker buildx build --platform linux/amd64 --load -t ghcr.io/dj2ls/freedata:latest .

      # Save the built image as a tarball.
      - name: Save Docker image to tar file
        run: |
          docker save ghcr.io/dj2ls/freedata:latest -o freedata.tar

      # Upload the image tarball as an artifact.
      - name: Upload Docker image artifact
        uses: actions/upload-artifact@v4
        with:
          name: freedata-image
          path: freedata.tar

  release:
    if: startsWith(github.ref, 'refs/tags/')
    needs: build
    runs-on: ubuntu-latest
    name: Build and Publish Release
    permissions:
      packages: write
      contents: read
    steps:
      - name: Checkout Repo
        uses: actions/checkout@v4

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3

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

      - name: Log in to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push multi-arch Docker images
        uses: docker/build-push-action@v6
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: |
            ghcr.io/dj2ls/freedata:latest
            ghcr.io/dj2ls/freedata:${{ github.ref_name }}
          labels: |
            org.opencontainers.image.title=FreeDATA
            org.opencontainers.image.description=Docker image for FreeDATA
            org.opencontainers.image.url=https://github.com/dj2ls/freedata/pkgs/container/freedata/
