How do I deploy nuget packages in Travis CI? - deployment

I have a nuget package that runs Travis CI for its builds. Here is my yml:
language: csharp
solution: TreasureGen.sln
install:
- nuget restore TreasureGen.sln
- nuget install NUnit.Runners -OutputDirectory testrunner
script:
- xbuild TreasureGen.sln /p:TargetFrameworkVersion="v4.5" /p:Configuration=Stress
- mono ./testrunner/NUnit.ConsoleRunner.*/tools/nunit3-console.exe ./TreasureGen.Tests.Unit/bin/Stress/TreasureGen.Tests.Unit.dll
- mono ./testrunner/NUnit.ConsoleRunner.*/tools/nunit3-console.exe ./TreasureGen.Tests.Integration.IoC/bin/Stress/TreasureGen.Tests.Integration.IoC.dll
- mono ./testrunner/NUnit.ConsoleRunner.*/tools/nunit3-console.exe ./TreasureGen.Tests.Integration.Tables/bin/Stress/TreasureGen.Tests.Integration.Tables.dll
- mono ./testrunner/NUnit.ConsoleRunner.*/tools/nunit3-console.exe ./TreasureGen.Tests.Integration.Stress/bin/Stress/TreasureGen.Tests.Integration.Stress.dll
Ideally, when this runs on the master branch, if it is successful, it would then deploy the nuget packages as needed. There are already Nuget projects in the solution, which contain Package.nuspec and NuGet.config files for each package. I have tried getting it to deploy myself and have not had much success - typically I run into problems with the authentication, but not exclusively. I was wondering if anyone here has deployed nuget packages like this in Travis and how they did it.

After much fiddling and experimentation, I finally found a solution.
.travis.yml
language: csharp
solution: TreasureGen.sln
install:
- nuget restore TreasureGen.sln
- nuget install NUnit.Runners -OutputDirectory testrunner
script:
- xbuild TreasureGen.sln /p:TargetFrameworkVersion="v4.5" /p:Configuration=Stress
- mono ./testrunner/NUnit.ConsoleRunner.*/tools/nunit3-console.exe ./TreasureGen.Tests.Unit/bin/Stress/TreasureGen.Tests.Unit.dll
- mono ./testrunner/NUnit.ConsoleRunner.*/tools/nunit3-console.exe ./TreasureGen.Tests.Integration.IoC/bin/Stress/TreasureGen.Tests.Integration.IoC.dll
- mono ./testrunner/NUnit.ConsoleRunner.*/tools/nunit3-console.exe ./TreasureGen.Tests.Integration.Tables/bin/Stress/TreasureGen.Tests.Integration.Tables.dll
- mono ./testrunner/NUnit.ConsoleRunner.*/tools/nunit3-console.exe ./TreasureGen.Tests.Integration.Stress/bin/Stress/TreasureGen.Tests.Integration.Stress.dll
deploy:
skip_cleanup: true
provider: script
script: chmod +x ./deploy/deploy.sh && ./deploy/deploy.sh $NUGET_API_KEY $NUGET_SOURCE
on:
branch: master
deploy.sh
ApiKey=$1
Source=$2
nuget pack ./TreasureGen/TreasureGen.nuspec -Verbosity detailed
nuget pack ./TreasureGen.Domain/TreasureGen.Domain.nuspec -Verbosity detailed
nuget push ./DnDGen.TreasureGen.*.nupkg -Verbosity detailed -ApiKey $ApiKey -Source $Source
nuget push ./DnDGen.TreasureGen.Domain.*.nupkg -Verbosity detailed -ApiKey $ApiKey -Source $Source
Here are some of the key things to remember:
Do not forget the skip_cleanup: true - this allows you to reuse
your previous build command results for your nuget package
The chmod +x ./deploy/deploy.sh allows for the script to be executable
Place your API Key and Source as Travis environment variables. Especially for the API Key, make sure they are marked to not show in the output
Your build may differ (not using nunit for tests, only 1 package to publish, etc.), but the deployment process should be similar.

My .travis.yml is as below, it deploys correctly to nuget and only does the deploy from master branch. The NUGET_API environment variable is setup to only apply to master branch and not be visible in the build.
language: csharp
mono: none
dotnet: 3.1
before_install:
- sudo apt-get -y install libpam0g-dev
install:
- dotnet restore
script:
- dotnet build -c Release
after_success:
- ./test/setup_test_account.sh
- sudo dotnet test test/Npam.Tests/Npam.Tests.csproj
before_deploy:
- dotnet pack -c Release
deploy:
skip_cleanup: true
provider: script
script: dotnet nuget push ./src/Npam/bin/Release/Npam.*.nupkg -k $NUGET_API -s https://api.nuget.org/v3/index.json
on:
branch: master
https://github.com/CamW/npam/blob/master/.travis.yml

Accepted answer didn't work for me (I don't know why). Following is what worked.
language: csharp
solution: [SolutionName].sln
install:
- curl -L -o nuget.exe https://dist.nuget.org/win-x86-commandline/latest/nuget.exe
- mono nuget.exe restore [SolutionName].sln
script:
- xbuild /p:Configuration=Release [SolutionName].sln
- mono nuget.exe pack ./[NuspecName].nuspec
- mono nuget.exe setApiKey $NUGET_API_KEY -Source $NUGET_SOURCE -Verbosity quiet
- mono nuget.exe push [SolutionName].*.nupkg -Source $NUGET_SOURCE
$NUGET_SOURCE, $NUGET_API_KEY are environment variables defined in Travis.

I was trying to solve the .travis.yml problem with an aspnetcore web site. The dotnet CLI now supports commands for nuget push and executing unit testing. I also determined that the approach of having .travis.yml just call an external bash script makes things a like lot easier:
.travis.yml
language: csharp
solution: ./SolutionName.sln
mono: none
dotnet: 3.1
script:
- chmod +x ./deploy.sh
- ./deploy.sh
deploy.sh
#!/bin/bash
set -ev
dotnet build -c $BUILD_CONFIG ./SolutionName.sln
if [ $? -eq 0 ]
then
if [ "$BUILD_CONFIG" = "debug" ];
then
dotnet test ./TestProject/TestProject.csproj -v normal --no-build
else
echo Skip testing on non-debug build
fi
fi
if [ $? -eq 0 ]
then
if [ "${TRAVIS_PULL_REQUEST}" = "false" ] && [ "${TRAVIS_BRANCH}" = "master" ];
then
dotnet nuget push ./NugetProject/bin/$BUILD_CONFIG/NugetProject.*.nupkg --api-key $NUGET_API_KEY --source $NUGET_SOURCE --skip-duplicate
else
echo Skip nuget for non-master build
fi
fi
Hope this helps someone using .NET Core 2.2 and higher.

Related

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

How do I publish NuGet packages to GitHub Package Registry using dotnet on Mac?

The documentation is not complete and currently I cannot consume or publish package to GPR using dotnet on Mac. Any help would be useful
Far from ideal, and I haven't tested on Mac yet, but it worked on Linux
Publish
Enable GPR 'feature' in your GitBHub settings
Go to github and add a TOKEN with GPR read/write access
Add source locally
nuget source Add -Name "GitHub" \
-Source "https://nuget.pkg.github.com/MY_ACCOUNT/index.json"
Set api key
nuget setApiKey $TOKEN \
-Source "https://nuget.pkg.github.com/MY_ACCOUNT/index.json"
Push package
nuget push "my.lib.nupkg" -Source "GitHub"
Install package
nuget install my.lib -pre # '-pre' because of alpha, if alpha
Note:
'nuget install' downloads the nuget package
It doesn't add it to the project.
'dotnet' can't find it
Do it anyway so it gets cached in `~/.nuget/packages`
The `./project` relative downloaded package can be deleted
Reference manually:
Add 'new Source' to nuget.config.
<add key="GitHub" value="https://nuget.pkg.github.com/MY_ACCOUNT/index.json" />
nuget config can be
'./nuget.config'
'~/.nuget/NuGet/nuget.config'
Add to project:
dotnet add package my.lib \
-v 1.0.0-alpha \
-n # don't download, it can't handle authentication
Alternatively:
Edit project package reference
<PackageReference Include="my.lib" Version="1.0.0-alpha" />
Finally:
dotnet restore
As you can see at this link to the github community question dotnet nuget is currently not supported by the GPR and you rather have to use nuget with Mono or publish from Windows.
I hope they'll fix it in the near future myself, it's completely stopping me from taking advantage of the GPR.
You'll have to make the HTTP call yourself (other steps omitted for brevity):
jobs:
continuous-integration:
runs-on: ubuntu-latest
steps:
- name: push
run: |
for f in ./packages/*.nupkg
do
curl -vX PUT -u "<YOUR USER NAME>:${{ secrets.GITHUB_TOKEN }}" -F package=#$f https://nuget.pkg.github.com/<YOUR USER NAME>/
done
if: github.event_name == 'push'

Gitlab CI/CD yml file to build, package and deploy .net standard class library as nuget to nexus repository

I have a .net Standard (.Net Standard 2.0) class library which I want to deploy to nexus as nuget package. The private nexus repository is ready and I'm using Gitlab for code management.
In Gitlab I added the gitlab-ci.yml file which will trigger the build and the deployment but still without enough steps:
stages:
- build
- package
- deploy
build_image:
stage: build
only:
- master
script:
- echo "Restoring NuGet Packages…"
- RUN dotnet restore
- echo "Building solution…"
- RUN dotnet build --no-restore -c Release -o
package_dev:
stage: package
script:
-
deploy_dev:
stage: deploy
environment:
name: development
only:
- master
script:
-
My question is how to configure this file to trigger a build then perform packaging and deploy/push to nexus repo?
I don't know if I described it well as i'm totally new to this topic. I found some examples using MAVEN image but we are not using it.
Thanks in advance!
If I understand your question correctly, you want to create a nuget package out of your project and upload it to Nexus Repo. You can do it one step.
build-and-upload:
image: <dot-net image>
stage: build-and-upload
environment:
name: dev/test/prod
only:
- master
before_script:
- aws commands if you need to assume a deploy role.
script:
- ./scripts/nuget_publish.sh
And this is how your nuget_publish.sh will look like
dotnet build
dotnet pack
NEXUS_SOURCE=<Nexus_Source_Repo_Url>
NEXUS_API_KEY=<Nexus_Source_Repo_Api_Key>
dotnet nuget push <ProjectName>/bin/Debug/*.nupkg --source $NEXUS_SOURCE --api-key $NEXUS_API_KEY
I found a solution which works fine for me. Here is the script in case anybody faces the same issue/requirement:
In ci.yml on deploy stage:
stages:
- deploy
before_script:
- nuget restore mysolution.sln
deploy_mysolution:
stage: deploy
image:
name: crunchtime/dotnetcore-nuget-msbuild-docker
script:
- dotnet msbuild mysolution.sln /t:Clean,ReBuild /p:Configuration=Release;Platform="Any CPU"
- dotnet pack "mysolution/myproject.csproj" /p:Configuration=Release;Platform="Any CPU"
- PKGPATH=$(find myproject/bin/Release/*.nupkg)
- dotnet nuget push $PKGPATH -k $NUGET_PUSH_KEY -s https://nexus.xyz.com/repository/nuget/
only:
- master
where $NUGET_PUSH_KEY is the api key which is saved as an env variable which is applied to environments via the runner.

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?