GitHub Actions with .NET, Part 1 - Hello World and Downloading the Artifact

Full source code available here.

This is the first of a few posts on GitHub Actions. This first will be fairly basic, compile a Hello World application, zip up the binaries, and make them available to download.

In future posts, I’ll show how to add approvals and deploying to AWS, but for now, simple is best.

I have a tiny console application that prints “Hello World”.

1
2
3
4
5
6
7
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");
    }
}

Nothing to it!

The fun is in the GitHub Action workflow.

Adding the GitHub Actions Workflow

First off, you need to have your code in a git repo and the repo has to hosted in GitHub, accounts there are free for personal use.

Add this directory structure to your repo .github\workflows. In that directory add a file named build.yml. The file will contain instructions on how to build the application. If you are not familiar with yaml, it can be a little daunting at first. So this example should get you on the way - it will build the source, zip the compiled files in the release directory and make that zip available for download.

The workflow is triggered when there is a push to the main branch, i.e. if you commit and push to main, or if you push to a different branch, perform a pull request, and merge.

I’ll breakdown the workflow in a moment but here is the whole thing -

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
name: A workflow to build an application

on:
  push:
    branches: [ main ]

jobs:
  build:
    name: Build the app
    runs-on: ubuntu-latest

    steps:
    - name: Checkout source code
      uses: actions/checkout@v2
    
    - name: Setup .NET
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 5.0.x
    
    - name: Restore dependencies
      run: dotnet restore
    
    - name: Build
      run: dotnet build -c release --no-restore
      
    - name: Upload a Build Artifact
      uses: actions/upload-artifact@v2.2.2
      with:
        # Artifact name
        name: GitHubActionsHelloWorldConsoleDownloadableArtifact #.zip will be added automatically
        path: bin/release/net5.0/*.*

Piece by Piece

These first lines define the name of the workflow - “A workflow to build an application” in this case, and that it should be triggered when there is a push to the main branch, i.e. if you commit and push to main, or if you push to a different branch, perform a pull request, and merge.

1
2
3
4
5
name: A workflow to build an application

on:
  push:
    branches: [ main ]

The next portion of the file gets into the meat of what I want to do, build my application. I’m stating that the build job should happen on the latest Ubuntu image available.

1
2
3
4
jobs:
  build:
    name: Build the app
    runs-on: ubuntu-latest

This portion of the file checks out the source and performs the build using the latest version of .NET 5.0.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    steps:
    - name: Checkout source code
      uses: actions/checkout@v2
    
    - name: Setup .NET
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 5.0.x
    
    - name: Restore dependencies
      run: dotnet restore
    
    - name: Build
      run: dotnet build -c release --no-restore

At this point I have a compiled application sitting in a directory on a Ubuntu container running somewhere in the GitHub cloud. To download it to my computer I first have to upload it from the container to an accessible GitHub location.

1
2
3
4
5
6
    - name: Upload a Build Artifact
      uses: actions/upload-artifact@v2.2.2
      with:
        # Artifact name
        name: GitHubActionsHelloWorldConsoleDownloadableArtifact #.zip will be added automatically
        path: ./bin/release/net5.0/*.*

When you commit this code to the main branch GitHub Actions will kick off the workflow. You should see output something like the below.

The zip file with the compiled code can be downloaded by clicking on the link.

Full source code available here.

comments powered by Disqus

Related