|
| 1 | +name: Publish .NET Package to NuGet |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +jobs: |
| 10 | + publish: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + steps: |
| 13 | + - name: Checkout code |
| 14 | + uses: actions/checkout@v4 |
| 15 | + |
| 16 | + - name: Setup .NET SDK |
| 17 | + uses: actions/setup-dotnet@v4 |
| 18 | + with: |
| 19 | + dotnet-version: '9.0' |
| 20 | + |
| 21 | + - name: Get the version from the .csproj file |
| 22 | + id: get_version |
| 23 | + run: | |
| 24 | + VERSION=$(cat SimpleViewModel/SimpleViewModel.csproj | grep -oPm1 "(?<=<Version>)[^<]+") |
| 25 | + echo "VERSION=$VERSION" >> $GITHUB_ENV |
| 26 | +
|
| 27 | + - name: Get the latest published version from NuGet |
| 28 | + id: get_latest_version |
| 29 | + run: | |
| 30 | + LATEST_VERSION=$(curl -s "https://api.nuget.org/v3-flatcontainer/simpleviewmodel/index.json" | jq -r '.versions[-1]') |
| 31 | + echo "LATEST_VERSION=$LATEST_VERSION" >> $GITHUB_ENV |
| 32 | +
|
| 33 | + - name: Compare versions |
| 34 | + id: version_check |
| 35 | + run: | |
| 36 | + if [ "$VERSION" != "$LATEST_VERSION" ]; then |
| 37 | + echo "New version detected: $VERSION" |
| 38 | + echo "run_publish=true" >> $GITHUB_ENV |
| 39 | + else |
| 40 | + echo "No new version detected" |
| 41 | + echo "run_publish=false" >> $GITHUB_ENV |
| 42 | + fi |
| 43 | + |
| 44 | +
|
| 45 | + - name: Restore dependencies |
| 46 | + run: dotnet restore |
| 47 | + |
| 48 | + - name: Build the project and source generator |
| 49 | + run: | |
| 50 | + dotnet build SimpleViewModel/SimpleViewModel.csproj --configuration Release --no-restore |
| 51 | + dotnet build EnumSourceGenerator/EnumSourceGenerator.csproj --configuration Release --no-restore |
| 52 | +
|
| 53 | + - name: Pack the NuGet package |
| 54 | + run: | |
| 55 | + dotnet pack SimpleViewModel/SimpleViewModel.csproj --configuration Release --no-build --output ./nupkg |
| 56 | + mkdir -p ./nupkg/analyzers/dotnet/cs/ |
| 57 | + if [ -f EnumSourceGenerator/bin/Release/netstandard2.0/EnumSourceGenerator.dll ]; then |
| 58 | + echo "Source Generator DLL found, copying..." |
| 59 | + cp EnumSourceGenerator/bin/Release/netstandard2.0/EnumSourceGenerator.dll ./nupkg/analyzers/dotnet/cs/ |
| 60 | + else |
| 61 | + echo "Source Generator DLL not found! Exiting." |
| 62 | + exit 1 |
| 63 | + fi |
| 64 | + ls ./nupkg/analyzers/dotnet/cs/ |
| 65 | +
|
| 66 | + - name: Unzip the NuGet package for inspection |
| 67 | + run: | |
| 68 | + mkdir -p ./package-inspection |
| 69 | + unzip ./nupkg/*.nupkg -d ./package-inspection/ |
| 70 | + ls -R ./package-inspection |
| 71 | + if [ -f ./package-inspection/analyzers/dotnet/cs/EnumSourceGenerator.dll ]; then |
| 72 | + echo "Source Generator DLL is correctly packed!" |
| 73 | + else |
| 74 | + echo "Source Generator DLL is missing from the package!" && exit 1 |
| 75 | + fi |
| 76 | +
|
| 77 | + - name: Publish to NuGet |
| 78 | + if: env.run_publish == 'true' |
| 79 | + run: dotnet nuget push ./nupkg/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json |
| 80 | + continue-on-error: true |
0 commit comments