code style and fixes #2
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: Release | |
| on: | |
| push: | |
| branches: [ main ] | |
| workflow_dispatch: | |
| env: | |
| DOTNET_VERSION: '10.0.x' | |
| jobs: | |
| build: | |
| name: Build, Test, Pack | |
| runs-on: ubuntu-latest | |
| outputs: | |
| version: ${{ steps.version.outputs.version }} | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v5 | |
| with: | |
| submodules: recursive | |
| - name: Setup .NET | |
| uses: actions/setup-dotnet@v4 | |
| with: | |
| dotnet-version: ${{ env.DOTNET_VERSION }} | |
| - name: Extract version | |
| id: version | |
| run: | | |
| VERSION=$(dotnet msbuild src/CodexSharpSDK.csproj -nologo -getProperty:Version) | |
| if [ -z "$VERSION" ]; then | |
| echo "Failed to resolve package version from project file." | |
| exit 1 | |
| fi | |
| echo "version=$VERSION" >> "$GITHUB_OUTPUT" | |
| echo "Version: $VERSION" | |
| - name: Validate semantic version | |
| run: | | |
| VERSION="${{ steps.version.outputs.version }}" | |
| if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then | |
| echo "Version '$VERSION' is not a valid semantic version." | |
| exit 1 | |
| fi | |
| - name: Restore | |
| run: dotnet restore ManagedCode.CodexSharpSDK.slnx | |
| - name: Build | |
| run: dotnet build ManagedCode.CodexSharpSDK.slnx -c Release -warnaserror --no-restore | |
| - name: Test | |
| run: dotnet test --solution ManagedCode.CodexSharpSDK.slnx -c Release --no-build | |
| - name: Pack | |
| run: | | |
| mkdir -p artifacts | |
| dotnet pack src/CodexSharpSDK.csproj \ | |
| -c Release \ | |
| --no-build \ | |
| -o artifacts | |
| - name: Upload package artifacts | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: nuget-packages | |
| path: artifacts/*.*nupkg | |
| retention-days: 5 | |
| publish: | |
| name: Publish NuGet | |
| needs: build | |
| runs-on: ubuntu-latest | |
| if: github.ref == 'refs/heads/main' | |
| outputs: | |
| published: ${{ steps.publish.outputs.published }} | |
| version: ${{ needs.build.outputs.version }} | |
| steps: | |
| - name: Download artifacts | |
| uses: actions/download-artifact@v5 | |
| with: | |
| name: nuget-packages | |
| path: artifacts | |
| - name: Setup .NET | |
| uses: actions/setup-dotnet@v4 | |
| with: | |
| dotnet-version: ${{ env.DOTNET_VERSION }} | |
| - name: Publish to NuGet | |
| id: publish | |
| continue-on-error: true | |
| run: | | |
| set +e | |
| PUBLISHED=false | |
| for package in artifacts/*.nupkg; do | |
| if [[ "$package" == *.snupkg ]]; then | |
| continue | |
| fi | |
| RESULT=$(dotnet nuget push "$package" \ | |
| --api-key "${{ secrets.NUGET_API_KEY }}" \ | |
| --source https://api.nuget.org/v3/index.json \ | |
| --skip-duplicate 2>&1) | |
| EXIT_CODE=$? | |
| echo "$RESULT" | |
| if [ $EXIT_CODE -eq 0 ]; then | |
| PUBLISHED=true | |
| elif echo "$RESULT" | grep -qi "already exists"; then | |
| echo "Package already exists, skipping" | |
| else | |
| echo "NuGet publish failed" | |
| exit 1 | |
| fi | |
| done | |
| echo "published=$PUBLISHED" >> "$GITHUB_OUTPUT" | |
| create-release: | |
| name: Create GitHub Release | |
| needs: publish | |
| runs-on: ubuntu-latest | |
| if: needs.publish.outputs.published == 'true' | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v5 | |
| with: | |
| fetch-depth: 0 | |
| - name: Download artifacts | |
| uses: actions/download-artifact@v5 | |
| with: | |
| name: nuget-packages | |
| path: artifacts | |
| - name: Create and push tag | |
| run: | | |
| VERSION="${{ needs.publish.outputs.version }}" | |
| TAG="v$VERSION" | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| if git rev-parse "$TAG" >/dev/null 2>&1; then | |
| echo "Tag already exists: $TAG" | |
| else | |
| git tag -a "$TAG" -m "Release $VERSION" | |
| git push origin "$TAG" | |
| fi | |
| - name: Create GitHub release | |
| uses: softprops/action-gh-release@v2 | |
| with: | |
| tag_name: v${{ needs.publish.outputs.version }} | |
| name: v${{ needs.publish.outputs.version }} | |
| generate_release_notes: true | |
| files: artifacts/*.*nupkg | |
| draft: false | |
| prerelease: false |