name: Deploy Docker Image to Github Container Registry

on:
    push:
        branches: ["main"]
        paths:
            - "Cargo.toml"

env:
    CARGO_TERM_COLOR: always

jobs:
    build:
        runs-on: ubuntu-latest
        permissions:
            packages: write
            contents: read
            attestations: write
            id-token: write

        steps:
            - uses: actions/checkout@v4
            - uses: hustcer/setup-nu@v3
            - uses: cachix/install-nix-action@v31
              with:
                  nix_path: nixpkgs=channel:nixos-unstable

            - run: nix build .#docker

            - run: |
                  export CARGO_VERSION=$(nu -c "cargo metadata --format-version=1 --no-deps | from json | get packages | first | get version")
                    echo "CARGO_VERSION=$CARGO_VERSION" >> $GITHUB_ENV

            - run: docker load -i result
            - name: Log in to registry
              run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
            - run: docker tag docki:latest "ghcr.io/quirinecker/docki:$CARGO_VERSION"
            - run: docker tag docki:latest "ghcr.io/quirinecker/docki:latest"
            - run: docker push ghcr.io/quirinecker/docki:$CARGO_VERSION
            - run: docker push ghcr.io/quirinecker/docki:latest
