Deployment 如何在Travis CI中部署nuget包?

Deployment 如何在Travis CI中部署nuget包?,deployment,nuget,travis-ci,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

我有一个运行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.nuspec
Nuget.config
文件。我已经尝试让它自己部署,但并没有取得多大成功——通常我会遇到身份验证方面的问题,但不是唯一的问题。我想知道这里是否有人在Travis中部署过这样的nuget软件包,以及他们是如何做到的。

经过大量的修改和实验,我终于找到了解决方案

.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
language: csharp
solution: ./SolutionName.sln
mono: none
dotnet: 3.1
script:
- chmod +x ./deploy.sh 
- ./deploy.sh
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
#!/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
以下是一些需要记住的关键事项:

  • 不要忘记
    跳过\u cleanup:true
    -这允许您重新使用 您的nuget软件包的上一个生成命令结果
  • chmod+x./deploy/deploy.sh允许脚本可执行
  • 将API密钥和源作为Travis环境变量。特别是对于API键,请确保它们被标记为不显示在输出中
  • 您的构建可能会有所不同(不使用nunit进行测试,只发布一个包,等等),但部署过程应该类似

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

    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\u SOURCE
    $NUGET\u API\u KEY
    是Travis中定义的环境变量。

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

    .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
    
    language: csharp
    solution: ./SolutionName.sln
    mono: none
    dotnet: 3.1
    script:
    - chmod +x ./deploy.sh 
    - ./deploy.sh
    
    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
    
    #!/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及更高版本的用户有所帮助。

    My.travis.yml如下所示,它正确部署到nuget,并且只从主分支进行部署。NUGET_API环境变量设置为仅应用于主分支,并且在生成中不可见

    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
    

    你好。我希望您已经找到了解决问题的方法,但如果没有,我将推荐这篇文章,谢谢methgaard-这篇文章很有帮助,但不幸的是,它确实有不足之处。它不允许我指定只发布特定的分支(主分支),也不允许我从nuspec文件中获取构建版本(可能可行,只是不太明显),关于发布特定分支,这应该通过
    .travis.yaml
    中的分支标记来完成。分支标记只在
    deploy
    部分工作,正如我的研究所示,而本文使用
    script
    部分来完成部署。您看过
    标记上的
    了吗?我还必须在我的“脚本”中添加第一步更新NuGet自身的命令a部分:``-sudo-NuGet update-Self``否则NuGet不会接受我的包,因为客户端的版本太旧。是否有办法在推送到NuGet之前测试版本号是否已更改?如何管理NuGet包的版本?您是使用源推送手动更新它,还是有办法自动升级版本?