Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I deploy nuget packages in Travis CI?

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.

like image 984
cidthecoatrack Avatar asked Jan 06 '17 16:01

cidthecoatrack


4 Answers

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:

  1. Do not forget the skip_cleanup: true - this allows you to reuse your previous build command results for your nuget package
  2. The chmod +x ./deploy/deploy.sh allows for the script to be executable
  3. 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
  4. Your build may differ (not using nunit for tests, only 1 package to publish, etc.), but the deployment process should be similar.
like image 191
cidthecoatrack Avatar answered Oct 19 '22 17:10

cidthecoatrack


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.

like image 21
John J Schultz Avatar answered Oct 19 '22 15:10

John J Schultz


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

like image 24
CamW Avatar answered Oct 19 '22 17:10

CamW


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.

like image 45
ron4ex Avatar answered Oct 19 '22 17:10

ron4ex