如何在Travis CI中部署nuget包?

时间:2017-01-06 16:11:03

标签: deployment nuget travis-ci

我有一个运行Travis CI的nuget包。这是我的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

理想情况下,当它在主分支上运行时,如果成功,它将根据需要部署nuget包。解决方案中已有Nuget项目,每个包都包含Package.nuspecNuGet.config个文件。我试过让它自己部署并且没有取得多大成功 - 通常我遇到了身份验证方面的问题,但并不是唯一的问题。我想知道是否有人在Travis部署了这样的nuget包,以及他们是如何做到的。

4 个答案:

答案 0 :(得分:13)

经过大量的练习和实验,我终于找到了解决方案。

<强> .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

以下是一些需要记住的关键事项:

  1. 不要忘记skip_cleanup: true - 这可以让您重复使用 您以前的构建命令结果为您的nuget包
  2. chmod +x ./deploy/deploy.sh允许脚本可执行
  3. 将您的API密钥和来源设置为Travis环境变量。特别是对于API密钥,请确保它们标记为不在输出中显示
  4. 您的构建可能不同(不使用nunit进行测试,只使用1个软件包进行发布等),但部署过程应该类似。

答案 1 :(得分:2)

我的.travis.yml如下所示,它正确部署到nuget,仅从master分支进行部署。设置NUGET_API环境变量,使其仅适用于master分支,并且在构建中不可见。

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

答案 2 :(得分:1)

接受的答案对我不起作用(我不知道为什么)。以下是有效的。

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是Travis中定义的环境变量。

答案 3 :(得分:0)

我试图用aspnetcore网站解决.travis.yml问题。 dotnet CLI现在支持用于nuget push和执行单元测试的命令。我还确定使用.travis.yml只是调用外部bash脚本的方法使事情变得简单得多:

.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

希望这对使用.NET Core 2.2及更高版本的人有所帮助。