Publish NuGet Package to GitHub Registry #1
Workflow file for this run
This file contains 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: Publish NuGet Package to GitHub Registry | |
on: | |
push: | |
branches: | |
- releases/* | |
create: | |
tags: | |
- releases/* | |
jobs: | |
build-and-publish: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Extract package information | |
run: | | |
BRANCH_NAME="${GITHUB_REF#refs/heads/}" | |
if [[ "$BRANCH_NAME" =~ releases/([^/]+)/(.+) ]]; then | |
echo "Package=${BASH_REMATCH[1]}" >> $GITHUB_ENV | |
echo "Version=${BASH_REMATCH[2]}" >> $GITHUB_ENV | |
else | |
echo "Branch name does not match the required pattern 'releases/{Package}/{Version}'" | |
exit 1 | |
fi | |
- name: Set up .NET | |
uses: actions/setup-dotnet@v4 | |
with: | |
dotnet-version: '9.0.x' | |
- name: Pack the project | |
run: find -type f -name "${PACKAGE}.csproj" | xargs -I {} dotnet pack "{}" --configuration Release --output ./nupkgs | |
- name: Validate package version | |
run: | | |
PACKAGE_FILE=$(find ./nupkgs -name '${PACKAGE}.nupkg' | head -n 1) | |
NUPKG_VERSION=$(dotnet nuget list source --format short | grep -oP 'Version=\K[^;]+') | |
echo "Expected Version: $VERSION" | |
echo "Built Package Version: $NUPKG_VERSION" | |
if [[ "$NUPKG_VERSION" != "$VERSION" ]]; then | |
echo "The built package version does not match the expected version from the branch name." | |
exit 1 | |
fi | |
- name: Publish NuGet package to GitHub Packages | |
run: dotnet nuget push ./nupkgs/${PACKAGE}.nupkg --source https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json -k {{"${{ secrets.TOKEN_GITHUB }}" |