Publish NuGet Package #5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Publish NuGet Package | |
| on: | |
| push: | |
| tags: | |
| - 'v*.*.*' | |
| - '*.*.*' | |
| workflow_dispatch: | |
| inputs: | |
| version: | |
| description: '版本号 (例如: 1.0.0)' | |
| required: true | |
| type: string | |
| jobs: | |
| publish: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Set version | |
| id: set_version | |
| run: | | |
| if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then | |
| # 手动触发时使用输入的版本号 | |
| VERSION="${{ github.event.inputs.version }}" | |
| echo "Using manual version: $VERSION" | |
| else | |
| # 标签触发时从标签提取版本号 | |
| REF_NAME="${{ github.ref_name }}" | |
| echo "Tag name: $REF_NAME" | |
| if [[ "$REF_NAME" == v* ]]; then | |
| VERSION=${REF_NAME#v} | |
| else | |
| VERSION=$REF_NAME | |
| fi | |
| echo "Extracted version from tag: $VERSION" | |
| fi | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| echo "Final version: $VERSION" | |
| - name: Setup .NET | |
| uses: actions/setup-dotnet@v4 | |
| with: | |
| dotnet-version: | | |
| 8.0.x | |
| 9.0.x | |
| 10.0.x | |
| - name: Restore dependencies | |
| run: dotnet restore | |
| - name: Build | |
| run: dotnet build --configuration Release --no-restore | |
| - name: Test | |
| run: dotnet test --configuration Release --no-build --verbosity normal | |
| - name: Pack | |
| run: dotnet pack --configuration Release --no-build --output ./artifacts | |
| - name: Publish to NuGet | |
| run: dotnet nuget push ./artifacts/*.nupkg --api-key ${{ secrets.NUGET }} --source https://api.nuget.org/v3/index.json --skip-duplicate |