From 7accf08959e768a4d593a93db643f7e8e879b0b9 Mon Sep 17 00:00:00 2001 From: iminet Date: Sun, 27 Sep 2026 11:06:27 +0200 Subject: [PATCH] Adding publishing workflow for GitHub and NuGet --- .github/workflows/publish.yml | 98 +++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 .github/workflows/publish.yml diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..c6021f0 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,98 @@ +name: Publish NuGet package + +on: + push: + tags: + - '*' + workflow_dispatch: + +permissions: + contents: read + packages: write + id-token: write + +jobs: + publish: + runs-on: ubuntu-latest + + steps: + - name: Check out repository + uses: actions/checkout@v4 + + - name: Verify publishing credentials + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') + shell: bash + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NUGET_USER: ${{ secrets.NUGET_USER }} + run: | + if [[ -z "$GITHUB_TOKEN" ]]; then + echo "GitHub Packages token is not available." + exit 1 + fi + + if [[ -z "$NUGET_USER" ]]; then + echo "The NUGET_USER secret is missing or empty." + exit 1 + fi + + - name: Set up .NET SDKs + uses: actions/setup-dotnet@v4 + with: + dotnet-version: | + 5.0.x + 6.0.x + 7.0.x + 8.0.x + 10.0.x + + - name: Restore dependencies + run: dotnet restore ImgBBApi.csproj + + - name: Create NuGet package + shell: bash + run: | + PACKAGE_VERSION_ARGS=() + if [[ "$GITHUB_REF_TYPE" == "tag" ]]; then + PACKAGE_VERSION_ARGS=(-p:PackageVersion="${GITHUB_REF_NAME#v}") + fi + + dotnet pack ImgBBApi.csproj \ + --configuration Release \ + --no-restore \ + --output ./artifacts \ + "${PACKAGE_VERSION_ARGS[@]}" + + - name: Upload package artifact + uses: actions/upload-artifact@v4 + with: + name: ImgBBApi-nuget-package + path: ./artifacts/*.nupkg + if-no-files-found: error + + - name: Publish to GitHub Packages + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') + run: | + for package in ./artifacts/*.nupkg; do + dotnet nuget push "$package" \ + --source "https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json" \ + --api-key "${{ secrets.GITHUB_TOKEN }}" \ + --skip-duplicate + done + + - name: NuGet login + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') + uses: NuGet/login@v1 + id: nuget-login + with: + user: ${{ secrets.NUGET_USER }} + + - name: Publish to NuGet.org + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') + run: | + for package in ./artifacts/*.nupkg; do + dotnet nuget push "$package" \ + --source "https://api.nuget.org/v3/index.json" \ + --api-key "${{ steps.nuget-login.outputs.NUGET_API_KEY }}" \ + --skip-duplicate + done