Setting Up GitHub Actions

I have some Open Source projects that I maintain in my “free” time.  One of them is recurrence calculator – .NET library to computer recurrence date based on Outlook style rules: https://github.com/SergeyBarskiy/RecurrenceCalculator.  I was adding support for .NET 6, and decided to invest into some automation.  I wanted to setup two builds: one when I create pull request that builds and runs tests.  Then other when I merge into master, which in addition will also push to nuget.org.  I found a few articles and GitHub documentation, but nothing was specifying full flow.  So, as I worked thought this task, I wanted to document entire action for the next person.

To create actions, you simply need to create yaml file for each one in your repo in .github/workflows folder

Action1 – build and test on pull request – main.yml

name: Build

on:
   pull_request:
     branches: [ master ]
   workflow_dispatch:

jobs:
   build:
     env:
       BUILD_CONFIG: ‘Release’
       SOLUTION: ‘RecurrenceCalculator.sln’
    
     runs-on: ubuntu-latest

    steps:
       – uses: actions/checkout@v2

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

      – name: Restore dependencies
         working-directory: ./RecurrenceCalculator
         run: nuget restore $SOLUTION

      – name: Setup .NET
         uses: actions/setup-dotnet@v1
         with:
           dotnet-version: 6.0.*

      – name: Build
         working-directory: ./RecurrenceCalculator
         run: dotnet build $SOLUTION –configuration $BUILD_CONFIG –no-restore

      – name: Run tests
         working-directory: ./RecurrenceCalculator
         run: dotnet test $SOLUTION /p:Configuration=$BUILD_CONFIG –no-restore –no-build –verbosity normal

To explain a few things:

On section states to run on pull request against master branch.  It will execute on that branch I want to merge to master.  Plus on workflow dispatch, which means I can manually click a button to run it.  Main step is build.  I am setting up a couple of environmental variable to avoid repeating the same thing – build configuration and solution name.  Then there are step to:

  • Get source code from the branch – checkout action
  • Download and setup NuGet.  I could use dotnet restore instead
  • Run nuget restore.  Same comment – could use dotnet restore command instead
  • Download and setup .net exe and environment
  • Run build
  • Run tests

Action 2 – build, test and publish to NuGet.org when merged into master build-publish.yml

name: BuildAndPublish

on:
   push:
     branches: [ master ]
     paths-ignore:
       – ‘.github/**’
   workflow_dispatch:

jobs:
   build:
     env:
       BUILD_CONFIG: ‘Release’
       SOLUTION: ‘RecurrenceCalculator.sln’
    
     runs-on: ubuntu-latest

    steps:
       – uses: actions/checkout@v2

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

      – name: Restore dependencies
         working-directory: ./RecurrenceCalculator
         run: nuget restore $SOLUTION

      – name: Setup .NET
         uses: actions/setup-dotnet@v1
         with:
           dotnet-version: 6.0.*

      – name: Build
         working-directory: ./RecurrenceCalculator
         run: dotnet build $SOLUTION –configuration $BUILD_CONFIG –no-restore

      – name: Run tests
         working-directory: ./RecurrenceCalculator
         run: dotnet test $SOLUTION /p:Configuration=$BUILD_CONFIG –no-restore –no-build –verbosity normal
        
       – name: Push to nuget
         working-directory: ./RecurrenceCalculator
         run: dotnet nuget push **/*.nupkg -s https://api.nuget.org/v3/index.json -k ${{secrets.NUGET_API_KEY}}       

The only additional step is to push to nuget.org.  I did have to add a secret to the repo with the NuGet.org publish key and the secret’s name of NUGET_API_KEY

Enjoy