How to publish beta nuget packages out of AppVeyor - nuget

Here is the behavior I'm trying to achieve in AppVeyor
Build the code (stamp AssemblyInfo with 1.2.3.{build})
Test the code
Create the nuget package if the tests passed
Publish the beta package if the package created successfully (1.2.3-beta-{build})
Also make the package available in artifacts.
Ideally when publishing a nuget package it would be published as prerelease. In NuGet, this is done by adding alpha characters to the end of the package version. It is also considered bad practice to overwrite an existing package (indeed, many nuget implementations do not allow this).
AppVeyor does a good job of building and testing software out of github, but I don't seem to be able to control the nuget package version.
Given:
A package with the next semantic version of 1.2.3
I would expect the AppVeyor {version} variable to equate to 1.2.3.{build}
I would expect the nuget package version to equate to 1.2.3-beta-{build}
The first thing I tried was using variables in the {version} box. Apparently this is not allowed. AppVeyor seems to only do variable substitution for {branch} and {build} as part of the {version}. This means I'll have to maintain a separate variable for the semantic version.
The next challenge I ran into is that there's no way to set the nuget package version through the UI. It wants to default to be the same as the AppVeyor build version.
I decided to try creating the package using Powershell after the tests run. This works, but the Nuget Publish step wants to run before the package is created and there doesn't seem to be a way to control the execution order.
I think I'm on the wrong track. I need a conceptual reset.
Here is my appveyor.yml in its current (incorrect) state:
version: 0.1.0.{build}
configuration: Release
assembly_info:
patch: true
file: '**\AssemblyInfo.*'
assembly_version: '{version}'
assembly_file_version: '{version}'
assembly_informational_version: '{version}'
environment:
packageVersion: 0.1.0
nuget:
account_feed: true
project_feed: true
disable_publish_on_pr: true
before_build:
- ps: nuget restore
build:
verbosity: minimal
artifacts:
- path: '*.nupkg'
name: nuget package
deploy:
- provider: NuGet
api_key:
secure: blahblahblah
artifact: '*.nupkg'
on:
branch: master
on_success:
- ps: >-
$releaseVersion= $env:packageVersion
$buildNumber = $env:APPVEYOR_BUILD_NUMBER
$betaVersion= "$releaseVersion-beta-$buildNumber"
nuget pack Odin.nuspec -version $betaVersion
Get-ChildItem .\*.nupkg | % { Push-AppveyorArtifact $_.FullName -FileName $_.Name }
How do I fix this? Can I get the behavior I want?

You can use PowerShell and AppVeyor API to control version number. I would try composing appveyor.yml as following:
version: 0.1.0.{build}
environment:
packageVersion: 0.1.0
init:
- ps: $env:buildVersion = "$env:packageVersion.$env:appveyor_build_number"
- ps: $env:nugetVersion = "$env:packageVersion-beta-$env:appveyor_build_number"
- ps: Update-AppveyorBuild -Version $env:buildVersion
assembly_info:
patch: true
file: '**\AssemblyInfo.*'
assembly_version: '$(buildVersion)'
assembly_file_version: '$(buildVersion)'
assembly_informational_version: '$(nugetVersion)'
configuration: Release
nuget:
account_feed: true
project_feed: true
disable_publish_on_pr: true
before_build:
- nuget restore
build:
verbosity: minimal
after_build:
- nuget pack Odin.nuspec
artifacts:
- path: '*.nupkg'
deploy:
- provider: NuGet
api_key:
secure: blahblahblah
artifact: '*.nupkg'
on:
branch: master

Related

Create NuGet Package with GitHub Actions

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.

How To Run GitHub Actions - .NET Framework Unit Test

I have a Sample Solution with a simple .NET Framework 4.8 Library Project.
this Solution has also a Unit Test Project for this Library. This Test Project has a Test which will succeed and one Test which will fail.
Now i want to upload this to github and it should Run the Test Project. But i cant figure out, how i can run the Test Project. All Tutorials are for .NET Core 5+
my actual workflow file looks like this:
name: .NET Framework Desktop
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
strategy:
matrix:
configuration: [Release]
runs-on: self-hosted # For a list of available runner types, refer to
# https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on
env:
Solution_Name: SelfHostedPDMTest.sln # Replace with your solution name, i.e. MyWpfApp.sln.
Test_Project_Path: TestProjectTest.csproj # Replace with the path to your test project, i.e. MyWpfApp.Tests\MyWpfApp.Tests.csproj.
Wap_Project_Directory: your-wap-project-directory-name # Replace with the Wap project directory relative to the solution, i.e. MyWpfApp.Package.
Wap_Project_Path: your-wap-project-path # Replace with the path to your Wap project, i.e. MyWpf.App.Package\MyWpfApp.Package.wapproj.
steps:
- name: Checkout
uses: actions/checkout#v3
with:
fetch-depth: 0
# Install the .NET Core workload
- name: Install .NET
uses: actions/setup-dotnet#v2
with:
dotnet-version: 5.0.x
# Add MSBuild to the PATH: https://github.com/microsoft/setup-msbuild
- name: Setup MSBuild.exe
uses: microsoft/setup-msbuild#v1.0.2
# Execute all unit tests in the solution
- name: Execute unit tests
run: dotnet test
# Restore the application to populate the obj folder with RuntimeIdentifiers
- name: Restore the application
run: msbuild $env:Solution_Name /t:Restore /p:Configuration=$env:Configuration
env:
Configuration: ${{ matrix.configuration }}
Appx_Bundle_Platforms: x86|x64
Appx_Package_Build_Mode: StoreUpload
This is a sample Workflow from github.
i know the selected .net version is 5.0.x but 4.8.x is not possible.
and also dotnet test would run a .NET Core Test and not a .NET Framework Test.
Maybe someone has a good workflow file or can help me to start?
Try the below as documented
- name: Run vstests
uses: microsoft/vstest-action#v1.0.0
with:
testAssembly: TestProject.dll
searchFolder: .\TestProject\bin\Debug\
runInParallel: true

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

Two projects in one repository

I use Azure DevOps Pipelines with GitVersion.
I have one git repository with two projects in folders:
ProjectA/ProjectA.csproj
ProjectA/azure-pipelines.yml
ProjectA/gitversion.yml
ProjectB/ProjectB.csproj
ProjectB/azure-pipelines.yml
ProjectB/gitversion.yml
The beginning of my pipeline.yml looks like this:
trigger:
branches:
include:
- '*'
paths:
include:
- 'ProjectA' <-- ProjectB in the other file
# Setup everything
steps:
- task: NuGetCommand#2
displayName: 'Install GitVersion CLI Tool'
inputs:
command: custom
arguments: install GitVersion.CommandLine -Version 5.0.0 -OutputDirectory $(Build.BinariesDirectory)/tools -ExcludeVersion
- task: UseDotNet#2
displayName: 'Install .NET Core SDK 3.0'
inputs:
packageType: 'sdk'
version: '3.x'
includePreviewVersions: true
# Execute the GitVersion Tool
- script: mono $(Build.BinariesDirectory)/tools/GitVersion.CommandLine/tools/GitVersion.exe /output buildserver /nofetch
displayName: 'Execute GitVersion CLI Tool'
followed by dotnet restore, build and so on.
My issue is let's say ProjectA get's the version "-alpha12" through commits.
And then I commit to ProjectB it get's the version "-alpha13".
But these two projects are independent from each other and so should the version.
What I mentioned is, there is nowhere a path to the gitversion.yml and I think the build process is just working with defaults.
But I don't know how to specify the path to the gitversion.yml and don't know if that would solve my issue.
Can someone help me?
Simply put, this isn't going to work.
GitVersion is intended to assert a single version number for the entire repository it is being run against. It doesn't know anything about the different projects that exist within that repository, and therefore it can't assert a different version number for them.
You will only ever get a single version number from GitVersion.
The recommendation would be to split Project A and B into separate repositories.

Travis NUnit Run fails

i have a github project which i'm trying to intergrate in to Travis CI. i'm really new to this. so on my travis.yml i have added like this
language: csharp
solution: MigrationRunner.sln
install:
- nuget restore MigrationRunner.sln
- nuget install NUnit.Runners -Version 3.4.1 -OutputDirectory $TRAVIS_BUILD_DIR/libs
script:
- xbuild /p:Configuration=Release MigrationRunner.sln
- mono $TRAVIS_BUILD_DIR/libs/NUnit.Runners.3.4.1/tools/nunit-console.exe $TRAVIS_BUILD_DIR/libs/MigrationRunner.Test/bin/Debug/MigrationRunner.Test.dll
deploy:
provider: releases
api_key: "79bd0a7b6294145bde09c390b2f5c03680c589b1"
file: "MigrationRunner"
skip_cleanup: true
on:
tags: true
and then tried to run travis but it gets error saying
Cannot open assembly
'/home/travis/build/gayan85/MigrationRunner/libs/NUnit.Runners.3.4.1/tools/nunit-console.exe':
No such file or directory.
what was the wrong?