name: Publish NuGet Packages on: pull_request: branches: - main workflow_dispatch: jobs: publish: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v3 - name: Setup .NET uses: actions/setup-dotnet@v3 with: dotnet-version: 10.0.x - name: Restore dependencies run: dotnet restore - name: Run tests # Run all test projects in the repository. If any test fails, this step will exit non-zero # and subsequent packaging/publish steps will not run. run: | echo "Running all tests..." dotnet test --configuration Release --no-restore --blame-hang-timeout 60s --logger "console;verbosity=detailed" - name: Check, Build, and Pack Projects env: NUGET_API_KEY: ${{ secrets.NUGET_TOKEN }} SOURCE_URL: "https://git.personalnas.com/api/packages/Misaki/nuget" run: | mkdir -p ./artifacts for projfile in $(find . -name "*.csproj"); do # 1. Check if the project generates a package GENERATE_PKG=$(dotnet build "$projfile" --getProperty:GeneratePackageOnBuild) if [[ "$GENERATE_PKG" != "true" && "$GENERATE_PKG" != "True" ]]; then continue fi # 2. Get Package ID and Version PACKAGE_ID=$(dotnet build "$projfile" --getProperty:PackageId) VERSION=$(dotnet build "$projfile" --getProperty:Version) if [ -z "$PACKAGE_ID" ]; then PACKAGE_ID=$(basename "$projfile" .csproj) fi echo "Checking $PACKAGE_ID version $VERSION..." # 3. Check Gitea Registry LOWER_ID=$(echo "$PACKAGE_ID" | tr '[:upper:]' '[:lower:]') HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" \ -H "Authorization: token $NUGET_API_KEY" \ "${SOURCE_URL}/registration/${LOWER_ID}/${VERSION}.json") if [ "$HTTP_CODE" -eq 200 ]; then echo "✅ Version $VERSION of $PACKAGE_ID already exists. Skipping..." else echo "🆕 Version $VERSION of $PACKAGE_ID is new." # 4. EXPLICIT BUILD (Creates the DLLs) echo "Building $projfile..." if ! dotnet build "$projfile" -c Release; then echo "❌ Failed to build $projfile" exit 1 fi # 5. PACK (Packages the DLLs created above) echo "Packing $projfile..." # We use --no-build because we just built it in step 4 if ! dotnet pack "$projfile" -c Release -o ./artifacts --no-build; then echo "❌ Failed to pack $projfile" exit 1 fi fi done - name: Publish new packages env: GITEA_API_KEY: ${{ secrets.NUGET_TOKEN }} NUGET_ORG_API_KEY: ${{ secrets.NUGET_ORG_TOKEN }} run: | if [ ! -d "./artifacts" ] || [ -z "$(ls -A ./artifacts/*.nupkg 2>/dev/null)" ]; then echo "No new packages to publish." exit 0 fi for pkg in ./artifacts/*.nupkg; do if [[ "$pkg" == *".symbols.nupkg" ]]; then continue fi echo "Pushing $pkg..." dotnet nuget push "$pkg" \ --source "https://git.personalnas.com/api/packages/Misaki/nuget/index.json" \ --api-key "$GITEA_API_KEY" \ --skip-duplicate echo "Pushing $pkg to NuGet.org..." dotnet nuget push "$pkg" \ --source "https://api.nuget.org/v3/index.json" \ --api-key "$NUGET_ORG_API_KEY" \ --skip-duplicate done