name: Release

on:
  push:
    tags:
      - "*"

permissions:
  contents: write
  packages: write

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Nix
        uses: cachix/install-nix-action@v31
        with:
          nix_path: nixpkgs=channel:nixos-unstable

      - name: Use Cachix
        uses: cachix/cachix-action@v16
        with:
          name: trevstack
          authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"

      - name: Check
        run: nix flake check

  # release:
  #   runs-on: ubuntu-latest
  #   needs: check
  #   steps:
  #     - name: Checkout
  #       uses: actions/checkout@v4

  #     - name: Install Nix
  #       uses: cachix/install-nix-action@v31
  #       with:
  #         nix_path: nixpkgs=channel:nixos-unstable

  #     - name: Use Cachix
  #       uses: cachix/cachix-action@v16
  #       with:
  #         name: trevstack
  #         authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"

  #     - name: Build
  #       run: >
  #         nix build
  #         .#trevstack-linux-amd64
  #         .#trevstack-linux-arm64
  #         .#trevstack-linux-arm
  #         .#trevstack-windows-amd64
  #         .#trevstack-darwin-amd64
  #         .#trevstack-darwin-arm64

  #     - name: Release
  #       uses: softprops/action-gh-release@v2
  #       with:
  #         generate_release_notes: true
  #         files: |-
  #           result*/bin/*

  # https://docs.docker.com/build/ci/github-actions/manage-tags-labels/
  package:
    runs-on: ubuntu-latest
    needs: release # Wait for binary cache to propagate
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Nix
        uses: cachix/install-nix-action@v31
        with:
          nix_path: nixpkgs=channel:nixos-unstable

      - name: Use Cachix
        uses: cachix/cachix-action@v16
        with:
          name: trevstack
          authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"

      - name: Set env
        run: |
          TAG=${{ github.ref_name }}
          VERSION=${TAG#v}

          NAME="trevstack:${VERSION}"

          REPOSITORY=${{ github.repository }}
          REGISTRY="ghcr.io/${REPOSITORY}"

          echo "VERSION=${VERSION}" >> $GITHUB_ENV
          echo "NAME=${NAME}" >> $GITHUB_ENV
          echo "REGISTRY=${REGISTRY}" >> $GITHUB_ENV

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

      - name: Build & Load Images
        run: |
          nix build .#trevstack-linux-amd64-image && ./result | docker load
          nix build .#trevstack-linux-arm64-image && ./result | docker load
          nix build .#trevstack-linux-arm-image && ./result | docker load

      - name: Push Images
        run: |
          docker image tag ${NAME}-amd64 ${REGISTRY}/${NAME}-amd64
          docker push ${REGISTRY}/${NAME}-amd64

          docker image tag ${NAME}-arm64 ${REGISTRY}/${NAME}-arm64
          docker push ${REGISTRY}/${NAME}-arm64

          docker image tag ${NAME}-arm ${REGISTRY}/${NAME}-arm
          docker push ${REGISTRY}/${NAME}-arm

      - name: Push Manifest
        run: |
          docker manifest create ${REGISTRY}/${NAME} \
            ${REGISTRY}/${NAME}-amd64 \
            ${REGISTRY}/${NAME}-arm64 \
            ${REGISTRY}/${NAME}-arm

          docker manifest annotate ${REGISTRY}/${NAME} ${REGISTRY}/${NAME}-amd64 --arch amd64
          docker manifest annotate ${REGISTRY}/${NAME} ${REGISTRY}/${NAME}-arm64 --arch arm64
          docker manifest annotate ${REGISTRY}/${NAME} ${REGISTRY}/${NAME}-arm --arch arm

          docker manifest create ${REGISTRY}/trevstack:latest \
            ${REGISTRY}/${NAME}-amd64 \
            ${REGISTRY}/${NAME}-arm64 \
            ${REGISTRY}/${NAME}-arm

          docker manifest annotate ${REGISTRY}/trevstack:latest ${REGISTRY}/${NAME}-amd64 --arch amd64
          docker manifest annotate ${REGISTRY}/trevstack:latest ${REGISTRY}/${NAME}-arm64 --arch arm64
          docker manifest annotate ${REGISTRY}/trevstack:latest ${REGISTRY}/${NAME}-arm --arch arm

          docker manifest push ${REGISTRY}/${NAME}
          docker manifest push ${REGISTRY}/trevstack:latest
