Skip to content

publish_nuget.yml

publish_nuget.yml #11

Workflow file for this run

# This workflow will build, pack and deploy the solution on Nuget
name: 'publish_nuget.yml'
on:
push:
tags:
- '[1-9]{1,2}.[0-9]+.[0-9]+'
workflow_dispatch: # Allows manual triggering
inputs:
version:
description: 'NuGet package version (SemVer)'
required: true
type: string
default: '1.0.0-beta1'
pattern: '^[1-9]{1,2}.[0-9]+.[0-9]+(-beta\d+)?$'
publish_to_nuget:
description: 'Publish to NuGet.org?'
required: false
type: boolean
default: true
create_release:
description: 'Create draft Release?'
required: false
type: boolean
default: true
permissions:
id-token: write # nuget authentication
contents: read
jobs:
publish:
if: startsWith(github.ref, 'refs/tags/') || (github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/master')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
# Only validate tag is on master for auto-triggered runs
- name: Validate Tag Position (Auto-Trigger Only)
if: github.event_name == 'push'
run: |
TAG_COMMIT=$(git rev-parse ${{ github.ref }})
# Check if tag commit is reachable from origin/master
ORIGIN_MASTER=$(git rev-parse origin/master)
if git merge-base --is-ancestor $TAG_COMMIT $ORIGIN_MASTER; then
echo "Tag validation passed: tag points to valid commit on master branch"
else
echo "ERROR: Tag appears to be on wrong branch or detached HEAD state detected"
exit 1
fi
- name: Setup .NET 10.
uses: actions/setup-dotnet@v5
with:
dotnet-version: latest
- name: Build
run: dotnet build src/Html2OpenXml/HtmlToOpenXml.csproj --configuration Release /p:SourceRevisionId=${{ github.sha }}
- name: Pack NuGet Package
run: |
dotnet pack src/Html2OpenXml/HtmlToOpenXml.csproj \
--configuration Release \
--output ./nupkg \
-- /p:PackageVersion=${{ inputs.version }}
- name: Verify Package Created
run: |
if [ ! -f ./nupkg/*.nupkg ]; then
echo "ERROR: No NuGet packages found after packing"
exit 1
fi
- name: Push to NuGet.org
if: inputs.publish_to_nuget
run: |
dotnet nuget push ./nupkg/*.nupkg \
--source https://api.nuget.org/v3/index.json \
--skip-duplicate
- name: Create GitHub Release
if: inputs.create_release
run: |
TAG_COMMIT=${{ inputs.version != '' && inputs.version || github.ref_name }}
if [ -z "$TAG_COMMIT" ]; then
echo "ERROR: No version specified and no tag detected. Cannot create release."
exit 1
fi
gh release create "$TAG_COMMIT" \
--title "Release $TAG_COMMIT" \
--generate-notes \
--draft
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}