|
| 1 | +name: Publish Packages |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - '*-v*' |
| 7 | + |
| 8 | +env: |
| 9 | + DOTNET_VERSION: '9.0.x' |
| 10 | + NUGET_SOURCE: 'https://api.nuget.org/v3/index.json' |
| 11 | + |
| 12 | +jobs: |
| 13 | + detect-project: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + outputs: |
| 16 | + project: ${{ steps.detect.outputs.project }} |
| 17 | + version: ${{ steps.detect.outputs.version }} |
| 18 | + tag: ${{ steps.detect.outputs.tag }} |
| 19 | + steps: |
| 20 | + - name: Checkout code |
| 21 | + uses: actions/checkout@v4 |
| 22 | + with: |
| 23 | + fetch-depth: 0 |
| 24 | + |
| 25 | + - name: Detect project and version from tag |
| 26 | + id: detect |
| 27 | + run: | |
| 28 | + TAG_NAME="${{ github.ref_name }}" |
| 29 | + echo "Processing tag: $TAG_NAME" |
| 30 | + |
| 31 | + # Extract project name and version from tag (any project name) |
| 32 | + if [[ $TAG_NAME =~ ^(.+)-v(.+)$ ]]; then |
| 33 | + PROJECT_NAME="${BASH_REMATCH[1]}" |
| 34 | + VERSION="${BASH_REMATCH[2]}" |
| 35 | + echo "Detected project: $PROJECT_NAME" |
| 36 | + echo "Detected version: $VERSION" |
| 37 | + echo "project=$PROJECT_NAME" >> $GITHUB_OUTPUT |
| 38 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 39 | + echo "tag=$TAG_NAME" >> $GITHUB_OUTPUT |
| 40 | + else |
| 41 | + echo "Invalid tag format: $TAG_NAME" |
| 42 | + echo "Expected format: <projectname>-v<version>" |
| 43 | + exit 1 |
| 44 | + fi |
| 45 | +
|
| 46 | + publish: |
| 47 | + needs: detect-project |
| 48 | + runs-on: ubuntu-latest |
| 49 | + if: needs.detect-project.outputs.project != '' |
| 50 | + |
| 51 | + steps: |
| 52 | + - name: Checkout code |
| 53 | + uses: actions/checkout@v4 |
| 54 | + |
| 55 | + - name: Setup .NET |
| 56 | + uses: actions/setup-dotnet@v4 |
| 57 | + with: |
| 58 | + dotnet-version: ${{ env.DOTNET_VERSION }} |
| 59 | + |
| 60 | + - name: Cache NuGet packages |
| 61 | + uses: actions/cache@v4 |
| 62 | + with: |
| 63 | + path: ~/.nuget/packages |
| 64 | + key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj') }} |
| 65 | + restore-keys: | |
| 66 | + ${{ runner.os }}-nuget- |
| 67 | +
|
| 68 | + - name: Build and publish project |
| 69 | + uses: cake-build/cake-action@v3 |
| 70 | + with: |
| 71 | + target: Publish |
| 72 | + script-path: build.cake |
| 73 | + arguments: --project="${{ needs.detect-project.outputs.project }}" --configuration=Release |
| 74 | + env: |
| 75 | + NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }} |
| 76 | + |
| 77 | + - name: Upload packages as artifacts |
| 78 | + uses: actions/upload-artifact@v4 |
| 79 | + with: |
| 80 | + name: nuget-packages-${{ needs.detect-project.outputs.project }}-${{ needs.detect-project.outputs.version }} |
| 81 | + path: ./output/packages/*.nupkg |
| 82 | + retention-days: 30 |
| 83 | + |
| 84 | + - name: Create GitHub Release |
| 85 | + id: create_release |
| 86 | + uses: softprops/action-gh-release@v2 |
| 87 | + env: |
| 88 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 89 | + with: |
| 90 | + tag_name: ${{ needs.detect-project.outputs.tag }} |
| 91 | + name: ${{ needs.detect-project.outputs.project }} ${{ needs.detect-project.outputs.version }} |
| 92 | + body: | |
| 93 | + ## ${{ needs.detect-project.outputs.project }} ${{ needs.detect-project.outputs.version }} |
| 94 | + |
| 95 | + ### Changes |
| 96 | + - Automated release from tag ${{ needs.detect-project.outputs.tag }} |
| 97 | + |
| 98 | + ### Installation |
| 99 | + |
| 100 | + **NuGet Package Manager:** |
| 101 | + ``` |
| 102 | + Install-Package ${{ needs.detect-project.outputs.project }} -Version ${{ needs.detect-project.outputs.version }} |
| 103 | + ``` |
| 104 | + |
| 105 | + **PackageReference:** |
| 106 | + ```xml |
| 107 | + <PackageReference Include="${{ needs.detect-project.outputs.project }}" Version="${{ needs.detect-project.outputs.version }}" /> |
| 108 | + ``` |
| 109 | + |
| 110 | + **dotnet CLI:** |
| 111 | + ``` |
| 112 | + dotnet add package ${{ needs.detect-project.outputs.project }} --version ${{ needs.detect-project.outputs.version }} |
| 113 | + ``` |
| 114 | + draft: false |
| 115 | + prerelease: false |
| 116 | + files: ./output/packages/${{ needs.detect-project.outputs.project }}.${{ needs.detect-project.outputs.version }}.nupkg |
0 commit comments