name: Publish AsterNixOS

on:
  workflow_dispatch:
  push:
    branches:
      - main
    paths: 
      - VERSION

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

      - name: Modify configuration.nix to include xfce and podman
        run: |
          config_path="distro/etc_nixos/configuration.nix"
          extra_config_path="tools/github_workflows/config_for_publish_cachix.nix"
          ./test/nixos/common/merge_nixos_config.sh ${config_path} ${extra_config_path} ${config_path}

      - 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:
      kernel_version: ${{ steps.tag.outputs.kernel_version }}
      tag: ${{ steps.tag.outputs.tag }}
    strategy:
      fail-fast: false
    steps:
      - uses: actions/checkout@v4

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

      - name: Add Git tag
        uses: pxpm/github-tag-action@1.0.1
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          tag: ${{ steps.tag.outputs.tag }}

      - 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" --verify-tag
          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: maus007/docker-run-action-fork@v1
        with:
          image: asterinas/asterinas:0.17.2-20260407
          options: --privileged -v /dev:/dev -v ${{ github.workspace }}:/root/asterinas
          run: |
            export ASTER_BUILD_TIMESTAMP=`date '+%a %b %e %H:%M:%S %Z %Y'`
            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: |
          kernel_version="${{ needs.create-github-release.outputs.kernel_version }}"
          tag="${{ needs.create-github-release.outputs.tag }}"
          echo "upload asterinas-nixos-$kernel_version-${{ matrix.arch }}.iso"
          cd ${{ github.workspace }}
          mv ./asterinas-nixos.iso asterinas-nixos-$kernel_version-${{ matrix.arch }}.iso
          gh release upload "$tag" "asterinas-nixos-$kernel_version-${{ 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