GitHub Actions with .NET, Part 2 - Dependent Jobs
Full source code available here.
- Part 1 - Hello World and Downloading the Artifact
- Part 2 - Dependant Jobs
- Part 3 - Manual Approvals
- Part 4 - Building an S3 bucket with Pulumi
- Part 5 - Build a .NET Application and Upload to S3
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.
1class Program
2{
3 static void Main(string[] args)
4 {
5 #if DEBUG
6 Console.WriteLine("Hello World! From debug build.");
7 #endif
8 #if RELEASE
9 Console.WriteLine("Hello World! From release build.");
10 #endif
11 }
12}
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 -
1name: A workflow to build an application in debug and release
2
3on:
4 push:
5 branches: [ main ]
6
7jobs:
8 debug-build:
9 name: Build a debug versions of the app
10 # snip..
11
12 release-build:
13 needs: debug-build
14 name: Build a release versions of the app
15 # 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.
1name: A workflow to build an application in debug and release
2
3on:
4 push:
5 branches: [ main ]
6
7jobs:
8 debug-build:
9 name: Build a debug versions of the app
10 runs-on: ubuntu-latest
11
12 steps:
13 - name: Checkout source code
14 uses: actions/checkout@v2
15
16 - name: Setup .NET
17 uses: actions/setup-dotnet@v1
18 with:
19 dotnet-version: 5.0.x
20
21 - name: Restore dependencies
22 run: dotnet restore
23
24 - name: Build
25 run: dotnet build -c debug --no-restore
26
27 - name: Run it for fun
28 run: dotnet ./bin/debug/net5.0/GitHubActionsHelloWorldConsole.dll
29
30 - name: Upload the debug build artifact
31 uses: actions/upload-artifact@v2.2.2
32 with:
33 # Artifact name
34 name: HelloWorldDebug #.zip will be added automatically
35 path: ./bin/debug/net5.0/*.*
36
37
38 release-build:
39 needs: debug-build
40 name: Build a release version of the app
41 runs-on: ubuntu-latest
42
43 steps:
44 - name: Checkout source code
45 uses: actions/checkout@v2
46
47 - name: Setup .NET
48 uses: actions/setup-dotnet@v1
49 with:
50 dotnet-version: 5.0.x
51
52 - name: Restore dependencies
53 run: dotnet restore
54
55 - name: Build
56 run: dotnet build -c release --no-restore
57
58 - name: Run it for fun
59 run: dotnet ./bin/release/net5.0/GitHubActionsHelloWorldConsole.dll
60 - name: Upload the release build artifact
61 uses: actions/upload-artifact@v2.2.2
62 with:
63 # Artifact name
64 name: HelloWorldRelease #.zip will be added automatically
65 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.