C
C#2y ago
Alexicon

✅ Simple way to push a C# project as a NuGet package within a GitHub workflow yml

I am not sure if there is a better place to ask this question as its really only C# adjacent, but maybe someone here knows the answer. Does anyone have a simple and up to date example of how to push a C# project as a NuGet package for GitHub actions? There are a lot of examples online but all of the ones I can find seem to be out of date or use a third-party workflow which I would like to avoid. If I was using Azure DevOps I would simply add the following to my yml file:
- task: DotNetCoreCLI@2
displayName: Pack myproject
inputs:
command: pack
packagesToPack: '**/myproject.csproj'
configuration: release
versioningScheme: byBuildNumber

- task: DotNetCoreCLI@2
displayName: Push NuGet packages
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
inputs:
command: push
versioningScheme: byPrereleaseNumber
- task: DotNetCoreCLI@2
displayName: Pack myproject
inputs:
command: pack
packagesToPack: '**/myproject.csproj'
configuration: release
versioningScheme: byBuildNumber

- task: DotNetCoreCLI@2
displayName: Push NuGet packages
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
inputs:
command: push
versioningScheme: byPrereleaseNumber
however, the GitHub actions equivalent seems to elude me.
5 Replies
Angius
Angius2y ago
run: dotnet pack and run: dotnet nuget push would prolly be the easiest I use
jobs:
build:
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Build
run: dotnet build --configuration Release
- name: Pack
run: dotnet pack --configuration Release --no-build --output .
- name: Get version
uses: kzrnm/get-net-sdk-project-versions-action@v1
id: get-version
with:
proj-path: AnyBaseConverter/AnyBaseConverter.csproj
- name: Push
run: dotnet nuget push Atulin.AnyBaseConverter.${{steps.get-version.outputs.package-version}}.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.NUGET_KEY }}
jobs:
build:
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Build
run: dotnet build --configuration Release
- name: Pack
run: dotnet pack --configuration Release --no-build --output .
- name: Get version
uses: kzrnm/get-net-sdk-project-versions-action@v1
id: get-version
with:
proj-path: AnyBaseConverter/AnyBaseConverter.csproj
- name: Push
run: dotnet nuget push Atulin.AnyBaseConverter.${{steps.get-version.outputs.package-version}}.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.NUGET_KEY }}
for example
Alexicon
Alexicon2y ago
Awesome thanks @Angius , In case anyone else wants another example here is what I ended up doing
name: Build and Publish Nuget Packages

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: windows-latest

steps:
- uses: actions/checkout@v2

- name: Install .NET 7
uses: actions/setup-dotnet@v1
with:
dotnet-version: 7.x

- name: Restore
run: dotnet restore

- name: Build
run: dotnet build --configuration Release --no-restore

- name: Pack myproject
run: dotnet pack myproject/myproject.csproj --configuration Release --no-build /p:Version=7.0.$env:GITHUB_RUN_NUMBER

- name: Push
run: dotnet nuget push */**/*.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.NUGET_KEY }}
name: Build and Publish Nuget Packages

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: windows-latest

steps:
- uses: actions/checkout@v2

- name: Install .NET 7
uses: actions/setup-dotnet@v1
with:
dotnet-version: 7.x

- name: Restore
run: dotnet restore

- name: Build
run: dotnet build --configuration Release --no-restore

- name: Pack myproject
run: dotnet pack myproject/myproject.csproj --configuration Release --no-build /p:Version=7.0.$env:GITHUB_RUN_NUMBER

- name: Push
run: dotnet nuget push */**/*.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.NUGET_KEY }}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Alexicon
Alexicon2y ago
@TeBeConsulting Hey thanks for the refactored implementation. I changed my pipeline to reflect those changes you recommended. However the path part ''./build/packages/*.nupkg" doesnt work. Something to due with running on windows instead of Linux I think, but thats fine honestly.