Github 操作 nuget 恢复 .NET 4.7.2

Github action nuget restore .NET 4.7.2

我有一个针对 .net 4.7.2 的 .net Framework C# 项目,我正在尝试使用 github 操作进行 nuget 恢复和构建。

我在 nuget 恢复时遇到以下错误:

Run nuget restore SAPtoAPLv3Check/SAPtoAPLv3Check.csproj
MSBuild auto-detection: using msbuild version '16.11.0.36601' from 'C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\MSBuild\Current\Bin'.
Cannot determine the packages folder to restore NuGet packages. Please specify either -PackagesDirectory or -SolutionDirectory.
Error: Process completed with exit code 1.

我的 github 操作如下所示:

name: Release

on:
  workflow_dispatch:

jobs:
  build:

    runs-on: windows-latest

    steps:
    - name: Checkout code.
      uses: actions/checkout@v2
          
    - name: Setup .NET
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 5.0.x

    - name: Add msbuild to PATH
      uses: microsoft/setup-msbuild@v1.0.2

    - name: Setup Nuget
      uses: Nuget/setup-nuget@v1.0.5

    - name: Restore nuget packages
      run: nuget restore SAPtoAPLv3Check/SAPtoAPLv3Check.csproj

    - name: Build
      run: msbuild SAPtoAPLv3Check/SAPtoAPLv3Check.csproj /p:Configuration=Release

我还有其他 github 操作来构建更高版本的 .net 框架,并恢复 nugets,很好。主要针对 .netcore 和 .net48x。

我应该在哪里指定 nuget 的文件夹?

nuget restore -OutputDirectory "????" SAPtoAPLv3Check/SAPtoAPLv3Check.csproj

我的.csproj

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{BF3CB6D2-503F-4ED9-96E6-6D5DD50EA201}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>SAPtoAPLv3Check</RootNamespace>
    <AssemblyName>SAPtoAPLv3Check</AssemblyName>
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="Dapper, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null">
      <HintPath>..\packages\Dapper.2.0.90\lib\net461\Dapper.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed">
      <HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="NLog, Version=5.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c">
      <HintPath>..\packages\NLog.5.0.0-preview.1\lib\net46\NLog.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="SentilanAgent.MessageV1, Version=1.2.7912.29046, Culture=neutral, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>Lib\SentilanAgent.MessageV1.dll</HintPath>
    </Reference>
    <Reference Include="System" />
    <Reference Include="System.Configuration" />
    <Reference Include="System.Core" />
    <Reference Include="System.Data" />
    <Reference Include="System.IO.Compression" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Program.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
    <Compile Include="QueryRow.cs" />
    <Compile Include="ServerAndDatabase.cs" />
  </ItemGroup>
  <ItemGroup>
    <Content Include="databases.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
    <Content Include="Lib\SentilanAgent.MessageV1.dll">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
  <ItemGroup>
    <None Include="App.config" />
    <None Include="packages.config" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
         Other similar extension points exist, see Microsoft.Common.targets.
    <Target Name="BeforeBuild">
    </Target>
    <Target Name="AfterBuild">
    </Target>
    -->
</Project>

我没有的是packages.config,这就是我所缺少的。通过 Rider 添加 nugets 没有生成一个。

我将 nuget 恢复指向 .sln 文件而不是 .csproj,它起作用了。