Search code examples
c#.netmsbuildgithub-actions

.NET Core Github Actions MSBuild Error MSB4044


I have the following github Actions script to deploy to NuGet:

name: NuGet Publish

on:
  push:
    tags:
      - 'v*.*.*' # Specify the tag pattern, e.g., 'v1.0.0'
jobs:
  deploy:
    name: Deploy
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v2
    - name: Setup .NET
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 6.0.x
    - uses: actions/cache@v2
      id: cacheStep
      with:
        path: ~/.nuget/packages
        key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
        restore-keys: |
          ${{ runner.os }}-nuget-
    - name: Restore dependencies
      run: dotnet restore
    - name: Setup NuGet.exe for use with actions
      uses: NuGet/[email protected]
    - name: Restore NuGet
      if: steps.cacheStep.outputs.cache-hit != 'true'
      run: nuget restore Octane.sln
    - name: Set VERSION variable from tag
      run: echo "VERSION=${GITHUB_REF/refs\/tags\/v/}" >> $GITHUB_ENV
    - name: Build
      run: dotnet build OctaneEngine/OctaneEngine.csproj --no-restore --configuration Release /p:Version=${VERSION} /p:AssemblyVersion=${VERSION}
    - name: Publish NuGet package
      run: dotnet nuget push OctaneEngine/bin/Release/*.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.NUGET_API_KEY }} /p:Version=${VERSION}
      env:
        NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}

This code is failing on the build step and giving me the the error:

MSB4044: The "GenerateDepsFile" task was not given a value for the required parameter "AssemblyVersion

I tried manually specifying this in the script as well but it still isn't resolved. I have GenerateAssemblyInfo disabled in the .csproj file and have a default assembly version of 1.0.0.0 defined.


Solution

  • You're using a Windows runner (windows-latest) with its default shell i.e. pwsh (PowerShell 7.2.17 as of now). You should use commands under the run sections that are supported by the respective shell of the runner you're using.

    The following syntax is used to access the environment variables under PowerShell:

    $Env:<variable-name>
    

    You need to update the syntax for all the environment variables in your workflow:

    • $GITHUB_REF => $Env:GITHUB_REF
    • $GITHUB_ENV => $Env:GITHUB_ENV
    • $VERSION => $Env:VERSION

    After incorporating these changes, your steps would look like this:

    - name: Set VERSION variable from tag
      run: |
        $VERSION=$Env:GITHUB_REF_NAME.TrimStart("v")
        echo "VERSION=$VERSION" >> $Env:GITHUB_ENV
    
    - name: Build
      run: dotnet build OctaneEngine/OctaneEngine.csproj --no-restore --configuration Release /p:Version=$Env:VERSION /p:AssemblyVersion=$Env:VERSION
    
    - name: Publish NuGet package
      run: dotnet nuget push OctaneEngine/bin/Release/*.nupkg --source https://api.nuget.org/v3/index.json --api-key $Env:NUGET_API_KEY /p:Version=$Env:VERSION
      env:
        NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}