C# ASP.NET 5 - CI/CD With GitHub Actions - Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Applause] [Music] [Applause] [Music] okay with that with that let's switch to english because up until now we have worked in microsoft azure and i have not recorded this part because this part contains a lot of student information personally identifiable informations and so on and i don't want to make that public for those of you who are watching uh in the next exercise we are going to implement continuous integration for an asp.net core 5 application continuous integration means that we are going to use a build server to build and compile our application and then we are going also to run some unit tests we are not focusing on c-sharp here so the focus is on the continuous integration and deployment part deployment means the third step after building and testing is that we take our application and we deploy it automatically to azure so if you want to follow along the third step you definitely need an azure subscription our students you students you all have access to a playground to a free playground with 100 dollar free benefits in azure we have activated that together today for those students who were app who are absent today please talk to your colleague to your fellow students they will describe and show you how you can activate your azure benefits external viewers you need to get to an azure subscription if you are a student you can get 100 for free please use google to find the azure student benefits good now here we have a pretty basic asp.net core web application it's just the regular auto generated web application which shows us the weather forecast no code written it's not in the focus of our work today to take a look at c-sharp at least not now if we have enough time later we can talk about these things later we want to automate the building of this application we want to compile our application automatically using a build system and the build system that we are going to use is called github actions github actions is a feature from github which allows you to automatically build your code into executables what does that mean in the context of c sharp that means that your c sharp code is compiled into dlls with intermediate language without you having to compile that on your local machine if you use for instance c plus plus or rust or go this will compile your source code into machine language if you use typescript maybe angular react view then this workflow can turn your typescript code into compiled and minified javascript code that you can then use to build your mobile app or your your web app or progressive web app whatever you want to build and the same is true for all other languages like python java and so on so why is continuous integration so important because the old saying goes friends don't let friends publish from visual studio that means if you are here in visual studio yes i showed you that you can press right click use publish and use the interactive ui to publish your code for instance in azure that means you compile locally you test locally you publish locally directly into azure and that is something that is met with friends don't let friends use visual studio to publish applications that is something that you shouldn't do in real world if you're just learning and just experimenting there is nothing bad in trying publishing that to azure and we have done that in the previous lesson i didn't record that one you can try it if you can't if you didn't follow along uh live in today's lesson you can try it but in real world you should not do that why because multiple people work together maybe they have different computers maybe one has a mac one has a windows machine one has a linux machine they are they're configured differently and sometimes things doesn't don't work as expected there should be a single truth a single source of truth for the source code that is the source code that lives in github and there should be a single environment which is defined which is standardized which is always available and this environment should do the compiling the testing and the publishing if your computer works fine everything is good if your computer is screwed up and i don't know you have a lot of malware installed and the malware manipulated your c-sharp compiler and compiles malware into your application that would be a huge security problem if you would deploy publish directly from your computer therefore we don't do that the thing what we do is we just commit and push code to github and github will provide the necessary build servers which are free off malware which are standardized which are always available and from there we publish to our cloud environment this is the reason why we use continuous deployment it's reliable it's repeatable and it's secure deploying from your local computer is unsecure and sometimes unreliable and sometimes not always repeatable because things tend to break okay maybe it works on one machine but your colleague has a different setting and it doesn't work on his machine that's a problem if you have to deploy you must be able to deploy with github continuous integration or any kind of continuous integration you can always deploy because all you need to do is check in the code this is what we want to do here good now this these github actions uh if i scroll down a little bit are luckily you see it here very well suited for students see that one you get 2 000 build minutes even for private repositories so if you build your diploma thesis or your multi-year projects and you would like to make them private because you work together with some project partners from commercial project partners you can still use the things that i'm going to show you now if you work on an open source project and your repository is public don't worry about the 2000 minutes github will donate all the necessary cpu minutes to your project for free if you have a public repository everything is fine you see that one free we don't have to pay anything if you scroll here to the right you can see that you can choose whether you want to build on linux windows or mac os forget the prices they are just for more than these three minutes per month okay but the the things that i want to point out is you get free build servers on linux on windows and on mac os so even if you work together on a mac os application and you don't have a mac os operating system to build it you can still build it by just checking in the code and the build will run on github for instance today we are going to use linux build servers this is what we are going to use together okay so this is github actions now what do you have to do if you want to activate github actions the good news is nothing it's just there all you have to deploy is a single file you deploy a continuous integration specification in the form of a waml file in a very special folder and i'm going to show you how this works okay so please follow along go into your github account and i will do that here on my machine too so please now follow along okay please create a new repository and this repository you can give it any name i will call it uh hotel leo live ci cd but you can call it whatever you like okay i will make it a public repository public repository create here you immediately see actions popping up this is github actions so if we go to github actions here you will see currently nothing is installed we could set up a very very simple workflow which just says hello world but this is nothing that we would like to do we would like to set up a workflow ourselves but don't click on this link i will show you how to do that step by step nice please copy the link here this one copy it and as usual clone the repository on your local computer navigate into the empty folder which you have just cloned to keep things simple please copy your generated code from visual studio so the code that we just together worked on it's just a final new project for those who couldn't follow along in the last lesson because you were absent just go to visual studio and create a brand new asp.net core web api with net5 okay and that's it that's all you have to do we have done that together here it is and i will just take this folder cut it out here and put it into my newly cloned folder yes there would be other possibilities to directly add github as in remote to an existing directory and so on but a lot of things can go wrong so the the most easy way of doing that is clone the empty repository copy in the code that you would like to deploy and build and we are good please do not yet commit open this folder that you have just cloned and filled with your auto generated visual studio solution in visual studio code open it in visual studio code or any other text editor you can use another text editor too the first thing that we always add you know that is a git ignore file dot git ignore now there are two possibilities either you go to your browser navigate to git dot io type in visual studio create and copy the code that is one option but today i would like to show you a different option i'm not 100 sure if i've showed you that before there is a super useful plugin in visual studio code plugins extensions can be found here and if you look for git ignore you will find this plugin here you see 800 000 downloads with five stars that's really impressive and this is exactly the thing that you would like to install because what you then can do without leaving visual studio code at all you can press f1 look for kit ignore you see add git ignore this is the one that we just installed use visual studio here say yes put it into my git ignore and you're done so this is exactly the same as going to git ignore dot io and copy pasting the git ignore into your dot git ignore file but this is just a shortcut we don't need to leave which will studio code anymore we can do it directly here and you will get exactly the same templates last but not least please add the dot vs folder into your git ignore because we don't want to check in the visual studio settings i'm not sure if they are auto generated here just to make sure now in visual studio code uh please interrupt me if i'm too fast if you have any problems or questions please interrupt me okay now here in visual studio code if we go into the the integrated kit um the kit integration sorry we see the code nothing special we can add the code and we can just add a message add code and hit the check use push and the code should be up in github if you want to use any other kit client as usual feel free to do so if you want to use the command line with git add and hit commit and get push please do so that's not the topic of today's lesson somehow you have to get your code up in wish up in in github with the solution and with the api project okay and the click no file of course still we don't have any actions actions are still empty here that will be the next step okay i will wait for two or three minutes until everybody managed to add the get ignore file and push the codes to get up great then we can continue now we would like to add continuous integration to our project how do we do that well the first thing that i really encourage you to do is to install another plugin another extension you can go to the extensions and look for github actions if you wait for a second you will see that there are multiple uh implementations here for github actions okay so you can install whatever you like if you take a look here we have one 510 000 installs here uh let me quickly look i'm not sure if this is what i wanted to have i have installed historically this one um yeah choose one choose whatever you like any of these github actions thing i just saw that there is one which is even more popular and it has very good ratings so you know what i will try this one so i will just uh uninstall this one it's not bad it's a good thing it it works very well but i always try to try new things and today i would like to try this one all of them are quite well so again this action works fine but today i would like to run this one okay good this extension will help us it will support us when writing a new continuous integration workflow because we have to type in some code and this code of course follows a certain syntax and this plugin helps us with intellisense and documentation and things like that good let's go into the file explorer and in order to create the github action for continuous deployment sorry continuous integration is the first step we are going to create a new folder this folder is called dot github this folder belongs to github inside of this folder we create a subfolder which is called workflows because we are creating a build workflow and inside of this workflows folder we are creating a whammel file so a kind of json without curly braces if you have never heard about waml you will definitely understand the file structure it's not complicated i will call it ci cd you can give it any name but i will use ci-cd.waml and that is now a github action specification we have to give github a kind of recipe of how to build our application how to compile the source code into executables in our case dlls now please follow along the first thing that we specify is we specify the name you see the plugin now gives us nice intellisense it says this is the name of the workflow i will call it continuous integration and deployment you can use any name that you like the next thing that we are going to do is we are going to specify when this action should run on inside of the on we specify push because we would like to run this continuous the integration process whenever somebody pushes codes to our repository and then we specify branches and here we can say main that means whenever someone pushes codes to the main branch then we want to run our continuous integration process here please take a look at the documentation here you see the link to the documentation or here branches here is you the link to various cheat sheets or read the documentation there are many many options that you have here you can include branches exclude branches you can include directories exclude directories you can use wildcards and many many other things more so if you have multiple projects for instance frontend web application and a back-end asp.net core application you can here select that this process is only for the back-end or only for the front-end there are lots of options you can read the documentation on your own i just want to give you an idea how this all works together and then you can fill out the blanks and read the details in the documentations now let's start with our jobs more complex projects might have multiple jobs in our very simple example here we have a single job because we just want to compile.net give the job a name i will call it build the next one is now we have to specify on which platform our build should run and that is called runs on here on runs on we can select which operating system and which operating system version we would like to use and we can use if you type u like ubuntu you get the versions which are available for ubuntu and i'm going to be brave i'm saying hey always use the latest version of ubuntu ubuntu latest and as soon as we have specified the name of the job and the platform on which we would like to run it we can go on and define the necessary steps again there are lots of additional options which you can specify here you can work with environment variable variables you can use so-called secrets in your build for instance database passwords or things like that you can what else can you do you can define multiple platforms so you can build it on windows and linux you can define so-called strategy matrices and in this matrix you can define that it should run as the name suggests in a matrix of different configurations with different settings for instance please again read the documentation github actions i i want to make sure that you have the right impression github actions is not a trivial thing you can build really really elaborate projects here i want to keep it simple because this is a beginner's course but take a look at the documentation there are a lot of action a lot of options here now the steps which steps are available let's go into the browser here and just google for github actions marketplace if we go here to marketplace and then we go here to actions then we see that there are impressive 7063 different um different actions here that we can use inside of our project that is pretty impressive so there are many many many actions for nearly every language that you can imagine do you want to do something with java go for java do you want to do something with dotnet go4.net there are so many different things here i will i would like to show you uh some of them and the most generic action which just says run the bash script run a script okay but if you want to take a look at it if you if you work in your final year projects or your two year projects take a look at the actions it is very likely that for your platform and your programming language there are dedicated actions and every of these actions you can then take a look at it and read about it for instance here read about it how it can be used and how you should use this and so on and so on i will show you the most generic ones and to give you an impression how it works the first one the one that we always use is the so-called checkout action here checkout and whenever you see here verified creator see that one and this number of stars you can rely on this one it is very stable it works very nice and if you go down you see that this is called actions slash checkout there is currently the version 2 which is relevant for us and here you see the documentation of what you can do with this action there are many many different options luckily we only need a few of them let me show you how we work on that first one give it a name the step name check out code and of course if you like you can use emojis so if you use the um the the bell for instance here like in uh like in a hotel when you go to the clerk and you use the bell here check out code you can use any emojis that you like and they will show up in the builds too so let's have a little bit of fun and add an emoji here then after the name uses and here we now use the action that we would like to use to build our code or sorry to check out the code and if you can remember it was called actions slash checkout at v2 of course we could have copied this from here too it's fine and you know what the good news is there are reasonably reasonable default values for all the different parameters which are available here so the default values are perfectly fine we will go for just that read the documentation if you need to set up details and if you need to influence the behavior of this action or all the other actions that we are going to use in details okay but for us that's perfectly fine next we need to make sure that the that our version of net that we are going to use is installed on the build server these build servers are rather generic machines they have a lot of software installed on them but not every every version of net is available so we need to make sure that our version of of.net is really installed on this server and for that we are going to use another important action here it is the setup.net action this one i up i somehow messed it up uh github action uh setup.net i am sure this one exists i'm absolutely sure okay for whatever reason where is it on the market place why didn't i find it okay maybe i should have just entered setup.net core whatever this is the name that we are going to use this see that one and you see here that we can specify the version of net core that we would like to use so i am now lazy here so i'm going to just copy this one but i will give you a moment if you would like to type it in on your own so let's do that one let's that one of course we need to use a name again setup.net five and what is the correct emoji let's use the wrench here okay that is okay and here we are going to say we want to use the latest version of 5 because in this code we are a little bit proud that we are always using the latest version of.net so of course we are going to use dotnet 5. i will wait a minute so you can catch up and enter this second action good so i assume that everybody has entered this part of the build process now we can go on and really build our application and just to make sure that everybody is aware of how we can build our net application in the command line we have covered that a few months ago typically visual studio does that for us so let me quickly refresh your memory that you can remember how we build applications on the command line the first step that we always need to do is a.net restore dotnet restore fetches all the necessary nougat packages from nougat.org and installs them locally what do i mean with that just to recap in the cs project you have referenced a certain sdk and maybe you have also added some additional nuget packages like entity framework and so on and this dotnet restore fetches the sdks and the and the dependencies from nougat.org so this is the first step that we need to do dot net restore the second one is oops sorry i want to go here the second one is dotnet builds now the dotnet build command accepts uh a bunch of uh a bunch of parameters the first parameter is dash c the configuration we can for instance say that we don't want to have a debug version we want to have a release version and in order to make this building a little bit faster we can tell dot net build that it doesn't need to restore again because we did the restore in the previous step so this would as you can see build everything nice build succeeded exactly what we wanted last step we currently don't have it but i want to make sure that you have the complete picture is a net test here see net test means that we run all the unit tests currently we don't have unit tests so all we do to get warm and to get get comfortable with github actions is we are going to restore and build our application good so let's do that name restore dependencies which nougat this time i'm going to use that one and here i'm not going to use any action us i'm not going to reference any specific action why because with this action up here we have already installed.net so we can now absolutely rely on the fact that dotnet is installed all we need is to run a command we can specify run here and here we just say dotnet restore so this is the simplest way of doing a step just run in this case a shell command bash command and we know that it will succeed or it will likely succeed but.net we know that it is present because we setup.net before this was the restore step and last but not least build app uh let's use the rocket icon here okay run oops which one dot net build release release no restore if you are not sure what this dot net stuff supports of course you can get help so just type in net with help and you will get all the necessary information you see it and for instance if you want to know what dot net build can do just type net build help and you will get all the necessary things for instance you will see that this dash c is the configuration this is the release version and here you see the no restore that means we don't need to restore nougat packages before because we've done it already and that's it that's our continuous integration script it will now build a restore and build our app it's not super useful because okay it builds it but at least we know that that the code doesn't contain syntax errors don't worry we will add unit tests we will add deployment later on for now this is fine let's check in this ci cicd waml so go here edit add ci cd pipeline check push and let's go oops here to our browser window now if i refresh it here it seems that nothing has changed right a lot of things have changed if you go to actions here you will see continuous integration and something went wrong let me see maybe i have a typo here let's see dot git hub slash workflows that is correct and here i have my wamo file and it's here and it looks good is this just me i see now what i have done wrong you see this is still old settings so it has not been switched to main branch already and guess what here in the filter i specified main branch instead of master branch so please make sure you know github is currently in the process of changing the defaults from master to main that makes very much sense you know the discussion comes from the black life matters movement and it makes very much sense to rename these things from master to main but unfortunately i haven't done that yet i apologize for that but yeah here i did it but here i didn't do it and therefore our continuous integration didn't run and that's perfectly fine because we have a filter on the main branch so let's quickly fix that go here into our visual studio code and put a master here let's check that one in switch main to master fix branch filter yeah that one is better push let's see second try actions oh much better you see now we have a workflow run so if i go here click and then we will see starting your workflow run github is currently looking up a server for us ah github has found the server for us and you see checkout code already worked it's now installing onthefly.net 5 on the build server it did the restore pretty quickly it did the build pretty quickly and we are done so if i click on it you see zero warnings zero arrows that's exactly what i wanted to have and we have a green check so the build process worked perfectly fine so now i will wait for another few minutes so to make sure that everybody could successfully write the yml file check it in and see that the action really built the application before we maybe we can take a short break because we now worked for an hour approximately again after the break and therefore we'll do a short break and everybody can use the brake to catch up if you are still lagging a little bit behind but what should you take away from this hour here it's very simple to start github actions which is the continuous integration platform of github all you need to do is you need to add a file to the folder dot github workflows and here you specify the steps that must be taken in order to run the build and test and so on so this is what i wanted to show you here and then you can use the ui in the web portal to check your builds and check whether everything is fine okay these are the most basic steps uh let me check the clock here it's 10 15 we are going to continue at 10 30 and then we are going to add some unit tests and see how we can add unit tests and last step before we dive into more c-sharp we are going to to to deploy everything in azure to automate the deployment from checking in until cloud computing okay so 15 minutes break and please make sure that your build runs i will quickly fetch another cup of coffee then i will be right back on my my chair here and feel free to ask questions 10 30 we will continue see you in a few minutes
Info
Channel: Rainer Stropek
Views: 3,471
Rating: undefined out of 5
Keywords: C#, CSharp, ASP.NET, ASP.NET Core, .NET, .NET 5, GitHub, GitHub Actions, CI/CD, Continuous Integration, Continuous Deployment, DevOps, Cloud Computing, Azure, HTL Leonding
Id: R5ppadIsGbA
Channel Id: undefined
Length: 37min 18sec (2238 seconds)
Published: Sat Feb 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.