name: Publish AsterNixOS

on:
  workflow_dispatch:
  push:
    tags:
      - 'v*'

jobs:
  push-cachix:
    runs-on: ubuntu-4-cores-150GB-ssd
    container:
      image: asterinas/asterinas:0.17.0-20251213
      options: -v /dev:/dev --privileged
    timeout-minutes: 60
    steps:
      - uses: actions/checkout@v4

      - name: Modify configuration.nix to include xfce and podman
        run: |
          sed -i \
            -e 's/^[[:space:]]*# services\.xserver\.enable = true;/  services.xserver.enable = true;/' \
            -e 's/^[[:space:]]*# services\.xserver\.desktopManager\.xfce\.enable = true;/  services.xserver.desktopManager.xfce.enable = true;/' \
            -e 's/^[[:space:]]*# virtualisation\.podman\.enable = true;/  virtualisation.podman.enable = true;/' \
            distro/etc_nixos/configuration.nix

      - name: Build Asterinas NixOS patched packages and push to release cache
        run: |
          make kernel BOOT_PROTOCOL=linux-efi-handover64
          export CACHIX_AUTH_TOKEN=${{ secrets.CACHIX_AUTH_TOKEN_FOR_RELEASE_CACHE }}
          make push_cachix USE_RELEASE_CACHE=1
          echo "Push cachix succeeds!"

  create-github-release:
    runs-on: ubuntu-latest
    needs: [ push-cachix ]
    outputs:
      tag: ${{ steps.tag.outputs.tag }}
    strategy:
      fail-fast: false
    steps:
      - uses: actions/checkout@v4

      - name: Get Asterinas version
        id: tag
        run: |
          ASTER_VERSION=$(cat ./VERSION)
          echo "ASTER_VERSION=${ASTER_VERSION}"
          echo "tag=${ASTER_VERSION}" >> $GITHUB_OUTPUT

      - name: Create release
        env:
          GH_TOKEN: ${{ github.token }}
          GH_REPO: ${{ github.repository }}
        run: |
          tag="${{ steps.tag.outputs.tag }}"
          body="TODO: Write the release notes for version $tag"
          echo "tag=$tag"
          echo "body=$body"
          if gh release view "$tag" > /dev/null 2>&1; then
            echo "Release with tag $tag already exists. Skipping creation."
          else
            gh release create --draft "$tag" --title "$tag" --notes "$body"
          fi

  build-iso:
    runs-on: ubuntu-latest
    needs: [ create-github-release ]
    strategy:
      fail-fast: false
      matrix:
        arch: [ 'x86_64' ]
    timeout-minutes: 60
    steps:
      - uses: endersonmenezes/free-disk-space@v3
        with:
          rm_cmd: "rmz"
          remove_android: true
          remove_dotnet: true
          remove_haskell: true
      - uses: actions/checkout@v4

      - name: Build ISO
        uses: addnab/docker-run-action@v3
        with:
          image: asterinas/asterinas:0.17.0-20251213
          options: --privileged -v /dev:/dev -v ${{ github.workspace }}:/root/asterinas
          run: |
            make iso RELEASE=1 AUTO_INSTALL=false ARCH=${{ matrix.arch }}
            iso_path=$(realpath ./target/nixos/iso_image/iso/*.iso)
            echo "iso_path=$iso_path"
            cp $iso_path ./asterinas-nixos.iso
            echo "Building ISO succeeds"
      - name: Upload ISO
        env:
          GH_TOKEN: ${{ github.token }}
          GH_REPO: ${{ github.repository }}
        run: |
          tag="${{ needs.create-github-release.outputs.tag }}"
          echo "upload asterinas-nixos-$tag-${{ matrix.arch }}.iso"
          cd ${{ github.workspace }}
          mv ./asterinas-nixos.iso asterinas-nixos-$tag-${{ matrix.arch }}.iso
          gh release upload "$tag" "asterinas-nixos-$tag-${{ matrix.arch }}.iso"

  publish-github-release:
    runs-on: ubuntu-latest
    needs: [ create-github-release, build-iso ]
    steps:
      - name: Publish Release
        env:
          GH_TOKEN: ${{ github.token }}
          GH_REPO: ${{ github.repository }}
        run: gh release edit "${{ needs.create-github-release.outputs.tag }}" --draft=false