楼窗晚风

烟霭也似的遐思和怅惘。

  1. 1. Background
  2. 2. How it works
  3. 3. Getting started
    1. 3.1. Grab a Nuget API token
    2. 3.2. Create a Nuget config file
    3. 3.3. Create an Action workflow

Background

Recently I have complaint about Nuget gallery’s stupid design that you have to re-sign in to upload a new package everytime, so I asked some guys, and to my surprise, they don’t even use Nuget gallery website to deploy a new packge, it’s too much more elegent and cool way than using stupid nuget gallery, after several attmpts, I’m one of these cool guy now.

How it works

Basically once you pushed something to the Action branch, the Action will run, and build a package then upload it to nuget (or other sources). Which means, you’ll have another branch for updating pushes, and when it ready to release a new package, make a pull request to the default branch so action will go.

Getting started

Since I have a low proficiency in bash/linux and GitHub Actions, the procedures below may not be decent.

Grab a Nuget API token

You can grab your Nuget API token here: https://www.nuget.org/account/apikeys

But you’ll have to select a valid scope for this key, you can select all the packages you’v uploaded or just some of them. Notably you can only see this key once hence you suppose to store it safely.

In term of using this token, it should be added to Settings -> Secrets -> Actions -> Repository secrets, after that you can access this value in workflow by syntax like ${{ secrets.NUGET_KEY }}. In this case, I store the Nuget API token as NUGET_KEY in the secrets.

Create a Nuget config file

For some unknown reason (for me), it does work if I directly using dotnet nuget add source in workflow to add the nuget publishing source, that’s why a compromised way is here: make a file named NuGet.config in project source directory.

Content of this file be like:

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

Create an Action workflow

Simply create the suggested workflows for .NET, or you can create your own, that doesn’t matter.

png 1

The initial content of .NET workflow template will look like:

name: .NET

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

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Setup .NET
      uses: actions/setup-dotnet@v2
      with:
        dotnet-version: 6.0.x
    - name: Restore dependencies
      run: dotnet restore
    - name: Build
      run: dotnet build --no-restore
    - name: Test
      run: dotnet test --no-build --verbosity normal

We don’t discuss detail of this yml file here, what are we gonna do is simply add a step after Test, whatever you name it.

- name: Push package
        run: |
          # Grab some versioning information
          commithash=$(git rev-parse --short HEAD)
          currtime=$(date +%s)
          echo "commit hash is $commithash"
          echo "time is $currtime"

          # Enter project directory
          cd Manganese

          # Build package
          dotnet restore
          dotnet build

          # Pack a nuget package with all the default config
          dotnet pack

          # Enter package output directory
          cd bin/Debug

          # Getting file named with .nupkg extension
          file=""
          cdir=`ls ./*.nupkg`
          for eachfile in $cdir
          do
            file=$eachfile
          done
        
          # Push it to source "nuget"
          dotnet nuget push $file --api-key ${{ secrets.NUGET_KEY  }} --source "nuget"

Now you can simply push some stuff to your action branch, the action will run and push your package to nuget gallery.

Author : AHpx
This blog is under a CC BY-NC-SA 3.0 Unported License
Link to this article : https://sinoahpx.github.io/2022/07/27/Automatically-deploy-nuget-package-via-GitHub-Actions/

This article was last updated on days ago, and the information described in the article may have changed.