XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension

【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等

公开
关注 0 Fork 0 Star 0
UTF-8
name: Publish Packages

on:
  push:
    branches-ignore:
      - "**"
    tags:
      - "v*"
  workflow_dispatch:
    inputs:
      version:
        description: Package version to publish, for example 1.0.0
        required: true
        type: string
      skip_nuget_org:
        description: Skip publishing to NuGet.org
        required: false
        default: false
        type: boolean

permissions:
  contents: write
  packages: write

env:
  DOTNET_VERSION: 10.0.x
  SOLUTION_PATH: XFEExtension.slnx
  EXCLUDED_PROJECT_PATH: XFEExtension/XFEExtension.csproj
  PROJECT_PATH: XFEExtension.NetCore/XFEExtension.NetCore.csproj
  PACKAGE_OUTPUT_DIR: ${{ github.workspace }}/artifacts/packages

jobs:
  publish:
    runs-on: windows-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: ${{ env.DOTNET_VERSION }}

      - name: Resolve version
        id: version
        shell: bash
        run: |
          if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
            version="${{ inputs.version }}"
          else
            version="${GITHUB_REF_NAME#v}"
          fi

          if [[ ! "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?([-.][0-9A-Za-z.-]+)?$ ]]; then
            echo "Invalid package version: $version"
            exit 1
          fi

          echo "value=$version" >> "$GITHUB_OUTPUT"
          echo "PACKAGE_VERSION=$version" >> "$GITHUB_ENV"

      - name: Prepare build solution
        shell: pwsh
        run: |
          [xml]$solution = Get-Content "${{ env.SOLUTION_PATH }}" -Raw
          $excludedProjectPath = "${{ env.EXCLUDED_PROJECT_PATH }}"
          $projects = @($solution.Solution.Project)

          foreach ($project in $projects) {
            if ($project.Path -eq $excludedProjectPath) {
              [void]$project.ParentNode.RemoveChild($project)
            }
          }

          $buildSolutionPath = Join-Path "${{ github.workspace }}" "build.filtered.slnx"
          $solution.Save($buildSolutionPath)
          "BUILD_SOLUTION_PATH=$buildSolutionPath" >> $env:GITHUB_ENV

      - name: Restore
        run: dotnet restore ${{ env.BUILD_SOLUTION_PATH }}

      - name: Build
        run: dotnet build ${{ env.BUILD_SOLUTION_PATH }} --configuration Release --no-restore -p:GeneratePackageOnBuild=false -p:ContinuousIntegrationBuild=true -p:Version=${{ env.PACKAGE_VERSION }} -p:PackageVersion=${{ env.PACKAGE_VERSION }}

      - name: Pack
        run: dotnet pack ${{ env.PROJECT_PATH }} --configuration Release --no-build --no-restore --output ${{ env.PACKAGE_OUTPUT_DIR }} -p:GeneratePackageOnBuild=false -p:ContinuousIntegrationBuild=true -p:Version=${{ env.PACKAGE_VERSION }} -p:PackageVersion=${{ env.PACKAGE_VERSION }}

      - name: Upload package artifacts
        uses: actions/upload-artifact@v4
        with:
          name: packages-${{ env.PACKAGE_VERSION }}
          path: ${{ env.PACKAGE_OUTPUT_DIR }}/*.nupkg
          if-no-files-found: error

      - name: Publish to GitHub Packages
        shell: pwsh
        env:
          GH_PACKAGES_TOKEN: ${{ secrets.GH_PACKAGES_TOKEN }}
        run: |
          $packages = Get-ChildItem -Path "${{ env.PACKAGE_OUTPUT_DIR }}" -Filter "*.nupkg" -File
          if ($packages.Count -eq 0) {
            throw "No .nupkg files found in ${{ env.PACKAGE_OUTPUT_DIR }}"
          }

          foreach ($package in $packages) {
            dotnet nuget push $package.FullName --source "https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json" --api-key "$env:GH_PACKAGES_TOKEN" --skip-duplicate
          }

      - name: Publish to NuGet.org
        if: ${{ github.event_name != 'workflow_dispatch' || !inputs.skip_nuget_org }}
        shell: pwsh
        run: |
          $packages = Get-ChildItem -Path "${{ env.PACKAGE_OUTPUT_DIR }}" -Filter "*.nupkg" -File
          if ($packages.Count -eq 0) {
            throw "No .nupkg files found in ${{ env.PACKAGE_OUTPUT_DIR }}"
          }

          foreach ($package in $packages) {
            dotnet nuget push $package.FullName --source "https://api.nuget.org/v3/index.json" --api-key "${{ secrets.NUGET_API_KEY }}" --skip-duplicate
          }

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ env.PACKAGE_VERSION }}
          name: V${{ env.PACKAGE_VERSION }}
          generate_release_notes: true
          append_body: true
          body: |
            ## Package feeds

            - NuGet.org
            - GitHub Packages
          files: ${{ env.PACKAGE_OUTPUT_DIR }}/*.nupkg
          fail_on_unmatched_files: true
          prerelease: ${{ contains(env.PACKAGE_VERSION, '-') }}

      - name: Publish summary
        shell: bash
        run: |
          {
            echo "## Published packages"
            echo ""
            echo "- Version: \`${PACKAGE_VERSION}\`"
            echo "- GitHub Packages: published"
            if [[ "${{ github.event_name }}" == "workflow_dispatch" && "${{ inputs.skip_nuget_org }}" == "true" ]]; then
              echo "- NuGet.org: skipped"
            else
              echo "- NuGet.org: published"
            fi
            if [[ "${{ github.event_name }}" == "push" && "${GITHUB_REF_NAME}" == v* ]]; then
              echo "- GitHub Release: created"
            else
              echo "- GitHub Release: skipped"
            fi
          } >> "$GITHUB_STEP_SUMMARY"