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
 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    }

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
 4  push:
 5    branches: [ main ]
 8  debug-build:
 9    name: Build a debug versions of the app
10    runs-on: ubuntu-latest
12    steps:
13    - name: Checkout source code
14      uses: actions/checkout@v2
16    - name: Setup .NET
17      uses: actions/setup-dotnet@v1
18      with:
19        dotnet-version: 5.0.x
21    - name: Restore dependencies
22      run: dotnet restore
24    - name: Build
25      run: dotnet build -c debug --no-restore
27    - name: Upload the debug build artifact
28      uses: actions/upload-artifact@v2.2.2
29      with:
30        # Artifact name
31        name: HelloWorldDebug will be added automatically
32        path: ./bin/debug/net5.0/*.*
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
41    steps:
42    - name: Checkout source code
43      uses: actions/checkout@v2
45    - name: Setup .NET
46      uses: actions/setup-dotnet@v1
47      with:
48        dotnet-version: 5.0.x
50    - name: Restore dependencies
51      run: dotnet restore
53    - name: Build
54      run: dotnet build -c release --no-restore
56    - name: Upload the release build artifact
57      uses: actions/upload-artifact@v2.2.2
58      with:
59        # Artifact name
60        name: HelloWorldRelease 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