GitHub Actions with .NET, Part 5 - Build a .NET Application and Upload to S3

Want to learn more about AWS Lambda and .NET? Check out my A Cloud Guru course on ASP.NET Web API and Lambda.

Full source code available here.

The Basics

You need a GitHub account and an AWS account. AWS offers a free tier where you can experiment at no cost. Martin Beeby of AWS, and I chatted about this in a podcast.

GitHub Secrets

The GitHub Action workflow needs a few secrets. In your repository, go to Settings, then Secrets. Add the below secrets.

Strictly speaking, the region and the bucket name might not be real secrets, but it’s an easy way to pass them to the workflow.

The Code

This application is as simple as it gets - the goal is to build a binary, zip it, and upload it to S3.

 1using System;
 3namespace GitHubActions_Upload_to_S3
 5    class Program
 6    {
 7        static void Main(string[] args)
 8        {
 9            Console.WriteLine("Hello World!");
10        }
11    }

The Workflow

To run GitHub Actions, a workflow file is needed. Add a .github/workflows directory.

Add the below code and name it with a .yaml extension. This will build the application, zip the contents of the release directory, and upload the zip to S3.

 1name: Build and Upload to S3
 4  push:
 5    branches: [ main ]
 8  build:
 9    name: Build 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 release --no-restore
27    - name: Install zip
28      uses: montudor/action-zip@v1
30    - name: Zip output
31      run: zip -qq -r bin/release/net5.0
33    # Not needed, but very handy when trying to figure out what the action is doing
34    #- name: Local file stuff
35    #  run: pwd; ls -al; find .
37    - name: Upload to S3 
38      uses: jakejarvis/s3-sync-action@master
39      with:
40        args: --acl public-read --follow-symlinks --exclude '*' --include '' 
41      env:
42        AWS_S3_BUCKET: ${{ secrets.AWS_S3_BUCKET }}
43        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
45        AWS_REGION: ${{ secrets.AWS_REGION }}

When this code is pushed to GitHub, the Action will start. You should see output that looks like this.

And in AWS, here is the bucket, with the file that was uploaded from GitHub Actions.

Full source code available here.

comments powered by Disqus