GitHub Actions with .NET, Part 3 - Manual Approvals

Full source code available here.

In the previous post, I showed how to make a job dependant on another job, in that example, a release build would only be performed if a debug build succeeded - that was a very contrived scenario, but showed the mechanics of forcing one thing to happen prior to another.

In this post, I’m going to expand on that and add a manual approval step for the release build, requiring a designated person to approve the second job.

GitHub Environments

To add an approver to a job, the repository has to be either public or you (the developer) needs to have an Enterprise account, the Team account is not enough.

In the public repository go to Settings, then Environment, and add a “release” environment.

The Code

I’m using the same C# as in the previous post, very simple.

 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}

The Workflow

The workflow remains almost the same as the previous post, with the addition of just two lines, 37 and 38. The environment name must match the one chosen above.

 1name: A workflow to build an application in debug and release, with manual approval for the release build
 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: Upload the debug build artifact
28      uses: actions/upload-artifact@v2.2.2
29      with:
30        # Artifact name
31        name: HelloWorldDebug #.zip will be added automatically
32        path: ./bin/debug/net5.0/*.*
33
34  release-build:
35    needs: debug-build
36    name: Build a release version of the app
37    environment:
38      name: release
39    runs-on: ubuntu-latest
40
41    steps:
42    - name: Checkout source code
43      uses: actions/checkout@v2
44    
45    - name: Setup .NET
46      uses: actions/setup-dotnet@v1
47      with:
48        dotnet-version: 5.0.x
49    
50    - name: Restore dependencies
51      run: dotnet restore
52    
53    - name: Build
54      run: dotnet build -c release --no-restore
55
56    - name: Upload the release build artifact
57      uses: actions/upload-artifact@v2.2.2
58      with:
59        # Artifact name
60        name: HelloWorldRelease #.zip will be added automatically
61        path: ./bin/release/net5.0/*.*

Running it

When I push to main or make a PR to main the workflow will be kicked off.

Once the first job completes, manual approval will be required before moving on to the second job.

I am the designated user who can approve the next job, so I do.

With the approval in place, the second job runs and completes.

The info about the review is shown, and artifacts are available for download.

Job done! In the next post, I’ll show how to use Pulumi with GitHub Actions to create AWS S3 infrastructure and upload a zip.

Full source code available here.

comments powered by Disqus

Related