|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + version: |
| 7 | + description: Package version, for example 1.0.1 |
| 8 | + required: true |
| 9 | + type: string |
| 10 | + prerelease: |
| 11 | + description: Mark GitHub Release as prerelease |
| 12 | + required: false |
| 13 | + default: false |
| 14 | + type: boolean |
| 15 | + release_notes: |
| 16 | + description: GitHub Release notes |
| 17 | + required: false |
| 18 | + default: "" |
| 19 | + type: string |
| 20 | + |
| 21 | +concurrency: |
| 22 | + group: release-${{ inputs.version }} |
| 23 | + cancel-in-progress: false |
| 24 | + |
| 25 | +jobs: |
| 26 | + build-pack: |
| 27 | + name: Build, test and pack |
| 28 | + runs-on: ubuntu-latest |
| 29 | + |
| 30 | + steps: |
| 31 | + - name: Checkout |
| 32 | + uses: actions/checkout@v6 |
| 33 | + |
| 34 | + - name: Setup .NET |
| 35 | + uses: actions/setup-dotnet@v5 |
| 36 | + with: |
| 37 | + dotnet-version: 10.0.x |
| 38 | + |
| 39 | + - name: Restore |
| 40 | + run: dotnet restore OpenPort.Net.sln |
| 41 | + |
| 42 | + - name: Build |
| 43 | + env: |
| 44 | + VERSION: ${{ inputs.version }} |
| 45 | + run: | |
| 46 | + package_version="${VERSION#v}" |
| 47 | + dotnet build OpenPort.Net.sln \ |
| 48 | + --configuration Release \ |
| 49 | + --no-restore \ |
| 50 | + /p:Version="$package_version" \ |
| 51 | + /p:PackageVersion="$package_version" |
| 52 | +
|
| 53 | + - name: Test |
| 54 | + run: dotnet test OpenPort.Net.sln --configuration Release --no-build --verbosity normal |
| 55 | + |
| 56 | + - name: Pack |
| 57 | + env: |
| 58 | + VERSION: ${{ inputs.version }} |
| 59 | + run: | |
| 60 | + package_version="${VERSION#v}" |
| 61 | + dotnet pack OpenPort.Net/OpenPort.Net.csproj \ |
| 62 | + --configuration Release \ |
| 63 | + --no-build \ |
| 64 | + --output artifacts \ |
| 65 | + /p:Version="$package_version" \ |
| 66 | + /p:PackageVersion="$package_version" |
| 67 | +
|
| 68 | + - name: Upload package artifact |
| 69 | + uses: actions/upload-artifact@v7 |
| 70 | + with: |
| 71 | + name: nuget-package |
| 72 | + path: artifacts/*.nupkg |
| 73 | + if-no-files-found: error |
| 74 | + |
| 75 | + publish-nuget: |
| 76 | + name: Publish to NuGet |
| 77 | + needs: build-pack |
| 78 | + runs-on: ubuntu-latest |
| 79 | + |
| 80 | + steps: |
| 81 | + - name: Setup .NET |
| 82 | + uses: actions/setup-dotnet@v5 |
| 83 | + with: |
| 84 | + dotnet-version: 10.0.x |
| 85 | + |
| 86 | + - name: Download package artifact |
| 87 | + uses: actions/download-artifact@v7 |
| 88 | + with: |
| 89 | + name: nuget-package |
| 90 | + path: artifacts |
| 91 | + |
| 92 | + - name: Push package |
| 93 | + env: |
| 94 | + NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }} |
| 95 | + run: | |
| 96 | + if [ -z "$NUGET_API_KEY" ]; then |
| 97 | + echo "::error::Repository secret NUGET_API_KEY is not set." |
| 98 | + exit 1 |
| 99 | + fi |
| 100 | +
|
| 101 | + dotnet nuget push artifacts/*.nupkg \ |
| 102 | + --api-key "$NUGET_API_KEY" \ |
| 103 | + --source https://api.nuget.org/v3/index.json \ |
| 104 | + --skip-duplicate |
| 105 | +
|
| 106 | + github-release: |
| 107 | + name: Create GitHub Release |
| 108 | + needs: build-pack |
| 109 | + runs-on: ubuntu-latest |
| 110 | + permissions: |
| 111 | + contents: write |
| 112 | + |
| 113 | + steps: |
| 114 | + - name: Checkout |
| 115 | + uses: actions/checkout@v6 |
| 116 | + with: |
| 117 | + fetch-depth: 0 |
| 118 | + |
| 119 | + - name: Download package artifact |
| 120 | + uses: actions/download-artifact@v7 |
| 121 | + with: |
| 122 | + name: nuget-package |
| 123 | + path: artifacts |
| 124 | + |
| 125 | + - name: Create release |
| 126 | + env: |
| 127 | + GH_TOKEN: ${{ github.token }} |
| 128 | + VERSION: ${{ inputs.version }} |
| 129 | + PRERELEASE: ${{ inputs.prerelease }} |
| 130 | + RELEASE_NOTES: ${{ inputs.release_notes }} |
| 131 | + run: | |
| 132 | + package_version="${VERSION#v}" |
| 133 | + tag="v$package_version" |
| 134 | +
|
| 135 | + if ! git ls-remote --exit-code --tags origin "refs/tags/$tag" >/dev/null 2>&1; then |
| 136 | + git config user.name "github-actions[bot]" |
| 137 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 138 | + git tag "$tag" "$GITHUB_SHA" |
| 139 | + git push origin "$tag" |
| 140 | + fi |
| 141 | +
|
| 142 | + notes="$RELEASE_NOTES" |
| 143 | + if [ -z "$notes" ]; then |
| 144 | + notes="OpenPort.Net $package_version" |
| 145 | + fi |
| 146 | + printf '%s\n' "$notes" > release-notes.md |
| 147 | +
|
| 148 | + prerelease_flag="" |
| 149 | + if [ "$PRERELEASE" = "true" ]; then |
| 150 | + prerelease_flag="--prerelease" |
| 151 | + fi |
| 152 | +
|
| 153 | + if gh release view "$tag" >/dev/null 2>&1; then |
| 154 | + gh release upload "$tag" artifacts/*.nupkg --clobber |
| 155 | + else |
| 156 | + gh release create "$tag" artifacts/*.nupkg \ |
| 157 | + --title "OpenPort.Net $package_version" \ |
| 158 | + --notes-file release-notes.md \ |
| 159 | + $prerelease_flag |
| 160 | + fi |
0 commit comments