diff --git a/.github/workflows/dotnet-publish.yml b/.github/workflows/dotnet-publish.yml index f04fdaf..1ff0f4d 100644 --- a/.github/workflows/dotnet-publish.yml +++ b/.github/workflows/dotnet-publish.yml @@ -15,16 +15,112 @@ concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true +# Inlined from baoduy/ShareWorkflows .github/workflows/dotnet-nuget-release.yml +# NuGet Trusted Publishing (OIDC) requires the token to be minted in this +# workflow directly; it cannot be referenced via a reusable workflow. +env: + Project_Path: 'src/DKNet.FW.sln' + Enable_Release: 'false' + Enable_Nuget_Release: ${{ github.ref == 'refs/heads/main' }} + Dotnet_Version: 10.x + jobs: - ## Only build nuget package and release to Nuget.org when running on main branch - dotnet_pack_release_job: - uses: baoduy/ShareWorkflows/.github/workflows/dotnet-nuget-release.yml@main - with: - Project_Path: 'src/DKNet.FW.sln' - Enable_Release: false ## ${{ github.ref == 'refs/heads/main'}} - Enable_Nuget_Release: ${{ github.ref == 'refs/heads/main'}} - Dotnet_Version: 10.x - secrets: - NUGET_USER: ${{ secrets.NUGET_USER }} - PACKAGE_TOKEN: ${{ secrets.PACKAGE_TOKEN }} - #NUGET_PACKAGE_TOKEN: ${{ secrets.NUGET_PACKAGE_TOKEN }} + build: + runs-on: ubuntu-latest + permissions: + contents: write + packages: write + id-token: write + + steps: + - uses: actions/checkout@v7.0.0 + with: + fetch-depth: 0 + fetch-tags: true + #https://github.com/actions/setup-dotnet + + - name: Setup .NET + uses: actions/setup-dotnet@v5.3.0 + with: + dotnet-version: ${{ env.Dotnet_Version }} + #global-json-file: global.json + + # Build + - name: Ensure GitHub NuGet Source + run: | + dotnet nuget add source 'https://nuget.pkg.github.com/baoduy/index.json' \ + -n github -u baoduy -p ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text + + - name: Restore dependencies + run: dotnet restore ${{ env.Project_Path }} + - name: Build + run: dotnet build ${{ env.Project_Path }} --no-restore --configuration Release + - name: Test + continue-on-error: true + if: ${{ env.Enable_Release != 'true' && env.Enable_Nuget_Release != 'true' }} + run: dotnet test ${{ env.Project_Path }} --collect "Code Coverage" --verbosity normal + + # Versioning + - name: Calculate version + id: version + uses: paulhatch/semantic-version@v6.0.2 + with: + tag_prefix: "v" + major_pattern: "(MAJOR)" + minor_pattern: "(MINOR)" + version_format: "${major}.${minor}.${patch}" + bump_each_commit: false + search_commit_body: false + + - name: Set NEXT_VERSION + run: echo "NEXT_VERSION=${{ steps.version.outputs.version }}" >> $GITHUB_ENV + + - name: Print the version + run: echo ${{ steps.version.outputs.version }} + + # Ensure the project able to Package + - name: Package + run: dotnet pack ${{ env.Project_Path }} --no-build --no-restore --configuration Release --output ${{ runner.WORKSPACE }}/nupkgs -p:PackageVersion=${{ env.NEXT_VERSION }} + - name: List Package + run: ls ${{ runner.WORKSPACE }}/nupkgs + + - name: Zip Release + if: ${{ env.Enable_Release == 'true' || env.Enable_Nuget_Release == 'true'}} + uses: thedoctor0/zip-release@0.7.6 + with: + # Filename for archive + filename: release.zip + directory: ${{ runner.WORKSPACE }}/nupkgs + + # Push nuget packages to Github Repository + - name: Publish + if: ${{ env.Enable_Release == 'true' }} + continue-on-error: true + run: dotnet nuget push ${{ runner.WORKSPACE }}/nupkgs/*.nupkg --skip-duplicate --source github --api-key ${{ secrets.PACKAGE_TOKEN }} + + # Create Release + - name: Release + uses: softprops/action-gh-release@v3.0.1 + if: ${{ env.Enable_Release == 'true' || env.Enable_Nuget_Release == 'true' }} + continue-on-error: true + with: + generate_release_notes: true + make_latest: true + files: ${{ runner.WORKSPACE }}/nupkgs/release.zip + tag_name: v${{ env.NEXT_VERSION }} + env: + GITHUB_TOKEN: ${{ github.token }} + + # Publish to NUGET.ORG via Trusted Publishing (OIDC) + # See: https://learn.microsoft.com/en-us/nuget/nuget-org/trusted-publishing + - name: NuGet login (Trusted Publishing) + id: login + if: ${{ env.Enable_Nuget_Release == 'true' }} + uses: NuGet/login@v1 + with: + user: ${{ secrets.NUGET_USER }} + + - name: Publish Nuget + if: ${{ env.Enable_Nuget_Release == 'true' }} + continue-on-error: true + run: dotnet nuget push ${{ runner.WORKSPACE }}/nupkgs/*.nupkg --skip-duplicate --source https://api.nuget.org/v3/index.json --api-key ${{ steps.login.outputs.NUGET_API_KEY }}