Using GitHub Actions For CI/CD with .NET Core 5 and AWS Elastic Beanstalk
For a freelance project I am working on, I had a requirement host a server on AWS. Since I have a background with .NET Core and C#, I wanted to use my existing skills and knowledge to build a server application. Also, I needed to use a Windows server because of DLL requirement for System.Drawing.
Unfortunately for me, the AWS docs for deploying .NET Core apps are lacking and outdated, so after hours of scrounging the internet I finally came up with an GitHub Actions yml file to make my app deployments seamless. I am going to share my file with you in hopes that I can save you time and effort.
Note: This is for a Windows server on ELB. If you want to use Linux, you can skip this manifest file, and make a slight change to the yml file that I describe at the end of the article to get it working. For this particular project I needed a windows machine, which made it a little more complicated.
The aws-windows-deployment-manifest.json file
The first thing you will need is a aws-windows-deployment-manifest.json file in the root of your project. This will tell ELB how to host your server for Windows. Note the “site.zip”. We will use that name in the yml file for the GitHub action, but you can customize as you like.
The GitHub Action — CI/CD
If you aren’t familiar with GitHub actions, please read up on them here. For sake of brevity I will assume you understand them.
There’s a lot to unpack here, but I will go over the major topics first. In order for ELB to process our application files, it expects a zip folder with the manifest we declared above along with a nested zipped folder with the published output (a bunch of dlls) from your .NET Core app.
On line 32, we run dotnet commands to create a published folder of our application, then on line 35 we zip all the files together (manifest and zipped app) and place them in the root directory. On line 46, we take the zipped folder that contains the manifest file and the zipped publish folder and push those up to ELB.
Lines 40 thru 43 and 45 relate to your AWS configuration. I chose to store my AWS Access Key and Secret in GitHub secrets to secure my information and make it easy to access.
If you want to deploy to a Linux machine, you will need to update line 32 to publish for “ -r linux-x64 “ instead of “ -r win10–x64.” Also, you will want to upload just a zip of your published results, not a zip contained within a zip by updating line 35.
The Result
Now, any time you push changes to your master branch, your ELB environment should automatically update! It only takes a few minutes for me, depends how large your application is.
I hope you enjoyed this article, please help me out by leaving some claps and following me if you found this useful. Happy coding!