4

I'm trying to configure my Github repository in order to automatically have a NuGet package built and pushed to both nuget.org and github.com. So what I want is that each time a commit is made on the master branch, or another branch is merged into the master, github publishes a new Nuget package of the head of the master to both Nuget and Github.

NuGet

  1. On my nuget organization account, I generated an access token (username - API keys - Create)
  2. On Github (select your organization - View organization - Settings tab - Secrets) I added a secret with the name PUBLISH_TO_NUGET_ORG and my nuget access token

Github

  1. On my personal account, I generated an access token (Account - Settings - Developer settings - Personal access tokens - generate)
  2. On Github I added a secret with the name PUBLISH_TO_GITHUB_COM and my github access token

These are the scopes for my Github access token:

Scopes for my Github access token

Setup

In my github repository I've setup an action to restore, build, test, pack and publish:

name: .NET Core

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 3.1.301
    - name: Install dependencies
      run: dotnet restore
    - name: Build
      run: dotnet build --configuration Release --no-restore
    - name: Test
      run: dotnet test --no-restore --verbosity normal
#    - name: Publish
#      uses: brandedoutcast/publish-nuget@v2.5.2
#      with:
#          PROJECT_FILE_PATH: MintPlayer.SeasonChecker/MintPlayer.SeasonChecker.csproj
#          NUGET_KEY: ${{secrets.PUBLISH_TO_NUGET_ORG}}
#          INCLUDE_SYMBOLS: true
    - name: Pack
      run: dotnet pack --no-build --configuration Release MintPlayer.SeasonChecker/MintPlayer.SeasonChecker.csproj --output .
    - name: PushNuget
      run: dotnet nuget push *.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{secrets.PUBLISH_TO_NUGET_ORG}} --skip-duplicate
    - name: AddGithubSource
      run: dotnet nuget add source --username PieterjanDeClippel --password ${{secrets.PUBLISH_TO_GITHUB_COM}} --name github https://nuget.pkg.github.com/MintPlayer/index.json
    - name: PushGithub
      run: dotnet nuget push *.nupkg --source github --skip-duplicate

The push to nuget.org works fine, but the push to my GitHub feed fails with an Unauthorized error.

I've taken a look at some plugins like this one, and I want to embed this into my action in order not to build my project multiple times.

First take:

dotnet nuget push *.nupkg --source https://nuget.pkg.github.com/MintPlayer/index.json --api-key ${{secrets.PUBLISH_TO_GITHUB_COM}} --skip-duplicate

Result: warn : Your request could not be authenticated by the GitHub Packages service. Please ensure your access token is valid and has the appropriate scopes configured.

warn : Your request could not be authenticated by the GitHub Packages service. Please ensure your access token is valid and has the appropriate scopes configured.

Second take with multiple commands:

dotnet nuget add source --username PieterjanDeClippel --password ${{secrets.PUBLISH_TO_GITHUB_COM}} --name github https://nuget.pkg.github.com/MintPlayer/index.json
dotnet nuget push *.nupkg --source github --skip-duplicate

This one fails with the following (obvious) message:

error: Password encryption is not supported on .NET Core for this platform. The following feed try to use an encrypted password: 'github'. You can use a clear text password as a workaround.
error:   Encryption is not supported on non-Windows platforms.

Does anyone have any experience with automated publishing of Nuget packages to Github?

Link to action configuration file

Edit

I tried sending a POST request:

And I'm getting my user information, so my access token definitely works.

Edit

I also tried running the command on my computer, replacing the token with my own and that as well does work.

Pieterjan
  • 2,738
  • 4
  • 28
  • 55
  • how do you auto increment nuget package version? – user1034912 Oct 22 '22 at 23:17
  • Atm I don't. You could add a msbuild task to make the last number of the version to be the date `yyyyMMddHHii` and provide a `` tag along, which generates the full `` tag – Pieterjan Oct 23 '22 at 02:43
  • [Here's](https://stackoverflow.com/a/22640077/8941307) an MSBuild snippet to generate the datetime.now, just prefix it with some `` tag and produce it as the `` tag: `$(VersionPrefix).$([System.DateTime]::Now.ToString(yyyyMMddHHii))` – Pieterjan Oct 23 '22 at 02:58

2 Answers2

2

Turns out I was missing a nuget.config file in my Solution

https://github.community/t/github-actions-automatically-push-nuget-package/128242/4

nuget.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
  </packageSources>
</configuration>

And my workflow file:

name: .NET Core

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - name: Checkout
      uses: actions/checkout@v2
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1.5.0
      with:
        dotnet-version: 3.1.301
        # Authenticates packages to push to GPR
        source-url: https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json
      env:
        NUGET_AUTH_TOKEN: '%NUGET_AUTH_TOKEN%'
    - name: Install dependencies
      run: dotnet restore
    - name: Build
      run: dotnet build --configuration Release --no-restore
    - name: Test
      run: dotnet test --no-restore --verbosity normal
    - name: Pack
      run: dotnet pack --no-build --configuration Release
    - name: PushNuget
      run: dotnet nuget push **/*.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.PUBLISH_TO_NUGET_ORG }} --skip-duplicate
    - name: PushGithub
      # The github token is automatically being pulled from the workflow
      run: dotnet nuget push **/*.nupkg --no-symbols --skip-duplicate
      env:
        NUGET_AUTH_TOKEN: ${{ github.token }}
Pieterjan
  • 2,738
  • 4
  • 28
  • 55
0

Per the github actions docs

<packageSourceCredentials>
    <github>
        <add key="Username" value="USERNAME" />
        <add key="ClearTextPassword" value="TOKEN" />
    </github>
</packageSourceCredentials>

So I think you just need to set -StorePasswordInClearText in your nuget add source command as you are currently encrypting the token

References:

Github Actions - https://docs.github.com/en/packages/using-github-packages-with-your-projects-ecosystem/configuring-dotnet-cli-for-use-with-github-packages#authenticating-to-github-packages

Nuget Config Docs - https://learn.microsoft.com/en-us/nuget/reference/nuget-config-file#packagesourcecredentials

Edward Romero
  • 2,905
  • 1
  • 5
  • 17