GitHub Actions with .NET, Part 2 - Dependent Jobs

Full source code available here.

In the previous post, I gave a quick introduction to GitHub Actions showing how to build a small Hello World application and make the artifact available for download.

In this post, I’ll show how to build debug and release versions of the same application, with the release only being built if the debug one builds successfully. This in itself is not a whole lot of use, but in the next post, I’m going to show how to add manual approvals before performing certain jobs, like deploying built code to AWS. In that scenario, a build will occur, a user needs to inspect the output and approve deploying the artifact, now that is useful.

But for now, I’m keeping it simple without the complications of external services, secrets, environment variables, etc.

The Code

The C# is very simple, print a different piece of text depending on the build configuration.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Program
{
    static void Main(string[] args)
    {
        #if DEBUG
            Console.WriteLine("Hello World! From debug build.");
        #endif
        #if RELEASE       
            Console.WriteLine("Hello World! From release build.");
        #endif
    }
}

Two Jobs in a GitHub Action

In the previous post, there was only one job, but you can have as many as you want, and these jobs can run independently (and in parallel) or have dependencies on each other, e.g. job 2 runs only if job 1 succeeds.

In this example, I want to run the release build only if the debug build succeeds.

The overall structure of the workflow looks like this -

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
name: A workflow to build an application in debug and release

on:
  push:
    branches: [ main ]

jobs:
  debug-build:
    name: Build a debug versions of the app
    # snip..

  release-build:
    needs: debug-build
    name: Build a release versions of the app
    # snip..

In this example there are two jobs - debug-build and release-build. release-build depends on debug-build completing successfully. They both upload artifacts to which will be available for download from GitHub after the action completes.

When the code is pushed to GitHub the debug-build starts.

Once that completes successfully, the release-build starts.

When that finishes the two artifacts will be available to download.

Here is the full workflow.

 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
name: A workflow to build an application in debug and release

on:
  push:
    branches: [ main ]

jobs:
  debug-build:
    name: Build a debug versions of 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 debug --no-restore
      
    - name: Run it for fun
      run: dotnet ./bin/debug/net5.0/GitHubActionsHelloWorldConsole.dll 

    - name: Upload the debug build artifact
      uses: actions/upload-artifact@v2.2.2
      with:
        # Artifact name
        name: HelloWorldDebug #.zip will be added automatically
        path: ./bin/debug/net5.0/*.*


  release-build:
    needs: debug-build
    name: Build a release version of 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: Run it for fun
      run: dotnet ./bin/release/net5.0/GitHubActionsHelloWorldConsole.dll 
    - name: Upload the release build artifact
      uses: actions/upload-artifact@v2.2.2
      with:
        # Artifact name
        name: HelloWorldRelease #.zip will be added automatically
        path: ./bin/release/net5.0/*.*

You Can Run (Almost) Anything in a Job

You are not limited to building software in jobs, you can run other things too.

After the build step in each job I have a run command that runs the compiled code, there is no good reason to do this here, but I’ve included it to show the flexibility of what you can do.

  - name: Run it for fun
    run: dotnet ./bin/debug/net5.0/GitHubActionsHelloWorldConsole.dll 

You can also use commands that are available on the host Ubuntu operating system where the build is happening.

In the next post, I’ll show how to add a manual approval before performing a step.

Full source code available here.

comments powered by Disqus

Related