Automating Nuget Package Push With .NetCore RC2

浪尽此生 提交于 2019-12-21 05:41:20

问题


I am currently working on a .NET Core library that I am going to use as a NuGet package in another project.

I have been able to successfully package the project using the "dotnet pack" command in the project directory, and upload that package to MyGet.

I would prefer to automate this process of pushing the NuGet package by using the "nuget push" command.

My issue is that the "scripts" property defined in the project.json file does not seem to be executed on pack or build. I expected that these scripts would be executed when the corresponding event occurs, but they seem to have no effect as I do not see anything output to console when I build, with or without he verbose tag.

I understand that MyGet is able to update a package feed based on a Git repository, but I would like to understand if there is some issue with executing a script currently using a project.json. Ideally I want to use the nuget push command after pack successfully executes.

Here is my project.json file :

{
  "version": "0.0.1",
  "scripts": {
    "postbuild": [ "echo build" ],
    "prebuild": "echo build",
    "postpack": "echo build",
    "postpublish": "echo build",
    "postrestore": "echo build",
    "prepack": "echo build",
    "prepare": "echo build",
    "prepublish": "echo build",
    "prerestore": "echo build"
  },

  "dependencies": {
    "NETStandard.Library": "1.5.0-rc2-24027"
  },

  "frameworks": {
    "netstandard1.5": {
    }
  },
  "buildOptions": {
    "allowUnsafe": false,
    "debugType": "portable",
    "emitEntryPoint": false,
    "xmlDoc": false
  },
  "commands": { },
  "packOptions": {
    "files": {
      "include": "%project:Directory%/bin/release/*.nupkg"
    }
  },
  "configurations": {
    "Debug": {
      "buildOptions": {
        "define": [ "DEBUG", "TRACE" ]
      }
    },
    "Release": {
      "buildOptions": {
        "define": [ ]
      }

    }
  }

}

回答1:


RC2 replaced prebuild and postbuild with precompile and postcompile.

You can use the postcompile to automatically generate the nupkg and to push the package to a nuget server using

"scripts": {
    "postcompile": [
  "dotnet pack --no-build",
  "\"%project:Directory%\\..\\..\\nuget.exe\" push \"%project:Directory%\\bin\\%compile:Configuration%\\%project:Name%.%project:Version%.nupkg\" -source nugetserver -ApiKey key"
]
  }

This will automatically call the dotnet pack using the project.json file that exists in the project directory. Then it will push the nuget package to the specified nuget server.

Unfortunately there is no variable to specifiy the build configuration therefore in the above path you will have to manually change it when you switch between debug and release configuration.

The above uses %compile:Configuration% to specify the current build configuration.

The answer to the current build configuration comes from How to run scripts based on solution configuration in ASP.NET Core RC2

Visual Studio 2017

In Visual Studio 2017 you can use the dotnet nuget push command by editing the csproj file and using the following command

<Target Name="PushPackage" AfterTargets="Pack">
    <Exec Command="dotnet nuget push &quot;$(MSBuildProjectDirectory)\bin\$(Configuration)\$(AssemblyName).$(Version).nupkg&quot; -s nugetserver -k apikey" />
</Target>


来源:https://stackoverflow.com/questions/37380716/automating-nuget-package-push-with-netcore-rc2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!