on:
  release:
    types:
      - published
jobs:
  build-artifacts:
    runs-on: docker
    container:
      image: iceshrimp.dev/iceshrimp/ci-env:dotnet9-wasm
      options: |
        --volume /opt/iceshrimp-cache/nuget:/root/.nuget
        --volume /root/.docker:/root/.docker
    steps:
      - name: Clone repository
        run: git clone "$REPO" --branch="$BRANCH" --depth=1 .
        env:
          REPO: ${{ github.event.repository.clone_url }}
          BRANCH: ${{ github.ref_name }}
      - name: Print environment info
        run: dotnet --info
      - name: Build release artifacts
        run: make release-artifacts "ARCHIVE_BASENAME=$REPO" "ARCHIVE_VERSION=$RELEASE_VERSION" VERBOSE=true DEP_VULN_WERROR=true
        env:
          REPO: ${{ github.event.repository.name }}
          RELEASE_VERSION: ${{ github.ref_name }}
      - name: Upload artifacts
        uses: actions/release-action@main
        with:
          files: artifacts/**
          api_key: '${{secrets.RELEASE_TOKEN}}'
      - name: Set environment variables
        shell: bash
        run: |
          # First, we set the docker repository we want to push to
          REPO="iceshrimp.dev/${GITHUB_REPOSITORY@L}"
          
          # We always want to tag :{version} and :pre, but only tag :latest for stable releases, and (temporarily) v2024.1-beta releases
          TAGS="-t $REPO:$GITHUB_REF_NAME -t $REPO:pre"
          
          # The first section below can be safely removed once v2025.1 hits stable
          if [[ "$GITHUB_REF_NAME" == "v2025.1-beta"* ]]; then
            TAGS="$TAGS -t $REPO:latest"
          elif [[ "$GITHUB_REF_NAME" == *"-beta"* ]] || [[ "$GITHUB_REF_NAME" == *"-pre"* ]]; then
            :
          else
            TAGS="$TAGS -t $REPO:latest"
          fi

          # Finally, we pass the computed tags back to the actions environment
          echo "TAGS=$TAGS" >> "${GITHUB_ENV}"
      - name: Build docker image
        run: |
          docker login iceshrimp.dev -u "$USER" -p "$TOKEN"
          docker buildx create --name iceshrimp-ci 2>&1 &>/dev/null || true
          docker buildx build ${{ env.TAGS }} --provenance=false --platform=linux/amd64,linux/arm64 --push --builder iceshrimp-ci -f ./.docker/dotnet-runner-9.0.Dockerfile ./release
          docker buildx prune --keep-storage 20G --builder iceshrimp-ci
        env:
          USER: ${{ github.actor }}
          TOKEN: ${{ secrets.REGISTRY_TOKEN }}
