Create NuGet Package with GitHub Actions - github

I have a solution for an ASP.NET Core API project that contains the main API project as well as two other library projects.
I now want to create NuGet packages for the library projects so that I can use them in other applications.
Do I need to separate my library projects into their own solutions and check them into their own separate repositories on GitHub in order to generate their NuGet packages through GitHub actions?
Currently the API and the library projects are in one solution and I keep them in the same repository. Do I need to split my projects into their own repositories or can I create NuGet packages only for my library projects from a single repository?

As per my experience, I suggest you to put the Nuget Application into another repository and follow the below instructions.
I've done this many times. Let me walk you through it.
1. Create API Key:
Sign in to nuget.org then go to the API Keys management and create a key.
2. Add the API key to GitHub repository
Go to GitHub and desired repository settings, then to Secrets. Create a new secret and paste there API key created on the first step.
3. Add workflow instructions:
Create a file under the root
< YOUR REPO > /.github/workflows/release.yml
name: Release to NuGet
on:
release:
types: [published]
jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Setup .NET SDK
uses: actions/setup-dotnet#v1
- name: Build
run: dotnet build -c Release
- name: Test
run: dotnet test -c Release --no-build
- name: Pack nugets
run: dotnet pack -c Release --no-build --output .
- name: Push to NuGet
run: dotnet nuget push "*.nupkg" --api-key ${{secrets.nuget_api_key}} --source https://api.nuget.org/v3/index.json
It does:
triggers on release publish
runs on ubuntu-latest
setup .NET SDK
runs dotnet build then tests
packs nugets and push it nuget.org using the attached nuget key
4. Create release:
Find the Releases link on the main page of your repo
Then create a new release
Verify the success of workflow.
Helpful Links:
Github Doc

The alternative to Jiya's answer is to use the GitHub action linch90/publish-nuget, which does the same kind of operation, and will do the same dotnet nuget push.
name: publish to nuget
on:
push:
branches:
- master # Default release branch
jobs:
publish:
name: build, pack & publish
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
# - name: Setup dotnet
# uses: actions/setup-dotnet#v1
# with:
# dotnet-version: 6.0.0
# Publish
- name: publish on version change
id: publish_nuget
uses: alirezanet/publish-nuget#v3.0.0
with:
# Filepath of the project to be packaged, relative to root of repository
PROJECT_FILE_PATH: Core/Core.csproj
# NuGet package id, used for version detection & defaults to project name
# PACKAGE_NAME: Core
# Filepath with version info, relative to root of repository & defaults to PROJECT_FILE_PATH
# VERSION_FILE_PATH: Directory.Build.props
# Regex pattern to extract version info in a capturing group
# VERSION_REGEX: ^\s*<Version>(.*)<\/Version>\s*$
# Useful with external providers like Nerdbank.GitVersioning, ignores VERSION_FILE_PATH & VERSION_REGEX
# VERSION_STATIC: 1.0.0
# Flag to toggle git tagging, enabled by default
# TAG_COMMIT: true
# Format of the git tag, [*] gets replaced with actual version
# TAG_FORMAT: v*
# API key to authenticate with NuGet server
# NUGET_KEY: ${{secrets.NUGET_API_KEY}}
# NuGet server uri hosting the packages, defaults to https://api.nuget.org
# NUGET_SOURCE: https://api.nuget.org
# Flag to toggle pushing symbols along with nuget package to the server, disabled by default
# INCLUDE_SYMBOLS: false
# Flag to toggle not building the project and letting pack command handle restoring & building, disabled by default
# NO_BUILD: false
Project gets published only if there's a NUGET_KEY configured in the repository (API key to authenticate with NuGet server)
So you need to register a NUGET_KEY secret in your repository.

Related

Publish NuGet package to private GitHub registry

I have a class library project that I keep in a private GitHub repo. I set up a GitHub action to create a NuGet package and push it to my private NuGet registry on GitHub.
The build process is working fine but getting stuck at NuGet push because it's unable to find the directory where the NuGet package is created. The error I get is:
Run dotnet nuget push "bin/Release/MyPackage.1.2.0.nupkg" --source
"github"
error: Could not find a part of the path
'/home/runner/work/my-package/my-package/bin/Release'.
Here's the yaml file for this job:
name: Publish MyPackage NuGet to GitHub Packages
on:
pull_request:
branches: [ "master" ]
jobs:
build-pack-n-ship:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- name: Setup .NET
uses: actions/setup-dotnet#v3
with:
dotnet-version: 6.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Create NuGet package
run: dotnet pack --configuration Release
- name: Get version
uses: kzrnm/get-net-sdk-project-versions-action#v1
id: get-version
with:
proj-path: MyProject/MyProject.csproj
- name: Add Package Source
run: dotnet nuget add source --username MY_USERNAME --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/MY_ORGANIZATION/index.json"
- name: Publish to GitHub Packages
run: dotnet nuget push "bin/Release/MyProject.${{ steps.get-version.outputs.version }}.nupkg" --source "github"
I think I need to specify the location of the working directory before the
"bin/Release/MyProject.${{ steps.get-version.outputs.version }}.nupkg" --source "github" but I wasn't able to get it right.
BTW, the part where I get the version number for my NuGet package is working fine. So I know that's not where the problem is.
Any idea how I can fix this?

How can I prevent my unit test project from being deployed to my Azure Web App along with my ASP.NET Core website project?

I have an ASP .NET Core website whose source code is stored in GitHub. This is automatically deployed to an Azure App Service (running on Linux) when I do a check-in to GitHub. (I set this up a while back using the Azure "wizard", but now I can't find any way to tinker with it).
Recently I added a unit test project to my solution, and when I checked that in the unit test project was deployed along with the web project - which I don't want. (Especially since the runtime host gets confused about which DLL it should run, and decides to run neither - so the website does not start!).
Is there a way to prevent the unit test project from being built or deployed? (I'm happy just to run these tests locally). I'm not even sure where to start - would I do this in GitHub, or in the Azure portal? I can't find any likely-looking knobs or levers either in GitHub or in the Azure portal.
Update: I've found a file that has been created in my repo in a .github/workflows folder. Here's the content:
name: Build and deploy ASP.Net Core app to Azure Web App - MyApp
on:
push:
branches:
- master
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Set up .NET Core
uses: actions/setup-dotnet#v1
with:
dotnet-version: '5.0.x'
include-prerelease: true
- name: Build with dotnet
run: dotnet build --configuration Release
- name: dotnet publish
run: dotnet publish -c Release -o ${{env.DOTNET_ROOT}}/myapp
- name: Upload artifact for deployment job
uses: actions/upload-artifact#v2
with:
name: .net-app
path: ${{env.DOTNET_ROOT}}/myapp
deploy:
runs-on: ubuntu-latest
needs: build
environment:
name: 'Production'
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
steps:
- name: Download artifact from build job
uses: actions/download-artifact#v2
with:
name: .net-app
- name: Deploy to Azure Web App
id: deploy-to-webapp
uses: azure/webapps-deploy#v2
with:
app-name: 'My-App'
slot-name: 'Production'
publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_0C864EC866B247EABE271A962BE84378 }}
package: .
While that's progress, I still can't see where I could modify it to prevent it from including the unit test project, because it doesn't actually reference anything directly.
Here's what worked for me:
I changed:
run: dotnet build --configuration Release
...to:
run: dotnet build MyProject --configuration Release
...where MyProject is the name of the folder containing the website project. (dotnet build is run from the solution folder, and the default behavior is that it will build all projects in the solution; specifying a sub-folder name makes it build only the project(s) it finds in that folder - see the documentation here).
Similarly, I changed:
run: dotnet publish -c Release -o ${{env.DOTNET_ROOT}}/myapp
to:
run: dotnet publish MyProject -c Release -o ${{env.DOTNET_ROOT}}/myapp
Thanks to #LexLi for pointing me in the right direction.

GitHub Actions dotnet build Unable to find package

I have a .NET CI ins GitHub Actions. Before added Dapper.FluentMap to the project, the GitHub Action was working without any errors.
GitHub Actions fails on:
- name: Build with dotnet
run: dotnet build --configuration Release
How can I build on GitHub actions using a NuGet Dapper.FluentMap dependency ?
I am now getting an error in GitBub Actions:
D:\a\esta-uploadmanagement\esta-uploadmanagement\BLL\BLL.csproj : error NU1101: Unable to find package Dapper.FluentMap. No packages exist with this id in source(s): Microsoft Visual Studio Offline Packages [D:\a\esta-uploadmanagement\esta-uploadmanagement\esta-uploadmanagement.sln]
D:\a\esta-uploadmanagement\esta-uploadmanagement\WebApp\WebApp.csproj : error NU1101: Unable to find package Dapper.FluentMap. No packages exist with this id in source(s): Microsoft Visual Studio Offline Packages [D:\a\esta-uploadmanagement\esta-uploadmanagement\esta-uploadmanagement.sln]
Failed to restore D:\a\esta-uploadmanagement\esta-uploadmanagement\WebApp\WebApp.csproj (in 316 ms).
Failed to restore D:\a\esta-uploadmanagement\esta-uploadmanagement\BLL\BLL.csproj (in 313 ms).
D:\a\esta-uploadmanagement\esta-uploadmanagement\DAL\DAL.csproj : error NU1101: Unable to find package Dapper.FluentMap. No packages exist with this id in source(s): Microsoft Visual Studio Offline Packages [D:\a\esta-uploadmanagement\esta-uploadmanagement\esta-uploadmanagement.sln]
Failed to restore D:\a\esta-uploadmanagement\esta-uploadmanagement\DAL\DAL.csproj (in 1 ms).
My Yaml file
on:
push:
branches:
- main
workflow_dispatch:
jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout#v2
- name: Set up .NET Core
uses: actions/setup-dotnet#v1
with:
dotnet-version: '5.0.x'
- name: Build with dotnet
run: dotnet build --configuration Release
- name: dotnet publish
run: dotnet publish -c Release -o ${{env.DOTNET_ROOT}}/myapp
This seems to be a recent bug / issue, as reported here and here on GitHub.
You can work around this issue by manually adding the nuget.org as source in a step:
[...]
- name: Set up .NET Core
uses: actions/setup-dotnet#v1
with:
dotnet-version: '5.0.x'
# new step
- name: Add nuget.org as nuget package source
run: dotnet nuget add source https://api.nuget.org/v3/index.json --name nuget.org
- name: Build with dotnet
run: dotnet build --configuration Release
[...]

Incorrect Path and No API errors when trying to publish to GitHub packages via GitHubActions

been stuck on this for a long time now, seems to be one problem after the other, just trying to get GitHub Actions to publish a nuGet package to GitHub packages, documentations is really hard to find and doesnt seem to give any clear examples
This was my previous question (just for context incase it helps)
I cant get gitHub actions to publish my package build to nuget.pkg.github
but now, I am getting the following:
Run dotnet nuget push "bin/Release/Project.1.0.0.nupkg" --source "github"
warn : No API Key was provided and no API Key could be found for 'https://nuget.pkg.github.com/name'. To save an API Key for a source use the 'setApiKey' command.
error: Could not find a part of the path '/home/runner/work/project/project/bin/Release'.
Here is my full yml
name: .NET Core
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Setup .NET Core
uses: actions/setup-dotnet#v1
with:
dotnet-version: 3.1.200
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --no-restore --verbosity normal
- name: nuGet publish
run: dotnet nuget add source https://nuget.pkg.github.com/name/index.json -n github -u uname -p password123 --store-password-in-clear-text
- name: nuGet pack
run: dotnet pack --configuration Release
- name: publish
run: dotnet nuget push "bin/Release/EurekaShared.1.0.0.nupkg" --source "github"
this is the build result:
dotnet nuget sadly doesn't always work as expected, and also has a habit of returning misleading error messages. It's infuriating.
Try adding a nuget/setup-nuget#v1 step to your workflow and use nuget.exe push instead of dotnet nuget push. That might either fix the problem or at least give you a more helpful error message.
I think the error is related with the part of the path to nupkg file, or file name. It could be issue of case, since Linux is case-sensitive.
Moreover, this workflow file is to publish just one package, i.e., EurekaShared.1.0.0.nupkg it won't work if your package is EurekaShared.1.0.2.nupkg
Perhaps you can give it a try by updating the last line in your YML file by using wildcard ** as follows:
run: dotnet nuget push /**/*.nupkg --source "github"
I also suggest that you secure your username and password in workflow file with GitHub encrypted secrets https://docs.github.com/en/free-pro-team#latest/actions/reference/encrypted-secrets

I cant get gitHub actions to publish my package build to nuget.pkg.github

I have been trying for a few months now, just to get GitHub actions to push a build to the gitHub packages, everytime i come back, try something else, nothing seems to work and it isnt making any sense.
I have just seen a new link with more details added for gitActions here:
I got excited, clicked, and see more code than previous had:
// Step 1: Authenticate (if this is the first time) Note you must also pass --store-password-in-clear-text on non-Windows systems.
$ dotnet nuget add source https://nuget.pkg.github.com/xxxxx/index.json -n github -u xxxxx -p GH_TOKEN [--store-password-in-clear-text]
// Step 2: Pack
$ dotnet pack --configuration Release
// Step 3: Publish
$ dotnet nuget push "bin/Release/myproject.1.0.0.nupkg" --source "github"
so, i went to my yml page (all within GitHub) and noticed the format is a little different... they are missing the "- name: xxx" and "run: xxx"
so, i updated... here is my full yml
name: .NET Core
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Setup .NET Core
uses: actions/setup-dotnet#v1
with:
dotnet-version: 3.1.101
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --no-restore --verbosity normal
- name: nuGet publish
run: dotnet nuget add source https://nuget.pkg.github.com/xxxx/index.json -n github -u xxxx-p GH_TOKEN [--store-password-in-clear-text]
- name: nuGet pack
run: dotnet pack --configuration Release
- name: publish
run: dotnet nuget push "bin/Release/projectname.1.0.0.nupkg" --source "github"
now, i thought maybe it would complain about password or something, but instead, im just getting this error: (i.e. "error: Unrecognized command or argument 'add'")
I am at a total loss and have no idea what and how to do this.... it builds fine all in gitHub, my package location is in GitHub, what am i doing so wrong?
EDIT:
thank you #ColinM after adjusting the yml above and just changin the version to 3.1.200, it now gets further, however, getting the following
Password encryption is not supported on .NET Core for this platform. The following feed try to use an encrypted password: 'github'. You can use a clear text password as a workaround
when running this line
Run dotnet nuget add source nuget.pkg.github.com/myname/index.json -n github -u myname -p abc123"
As per the MSDN documentation for dotnet nuget source add, this is available only from SDK 3.1.200 onwards; whereas you're currently using 3.1.101.
Update your YAML file to install an SDK version equal to or greater than 3.1.200