How To Deploy Your Application To Azure Using GitHub Actions | CI/CD Pipeline

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone my name is Milan and today I'm going to show you how you can build a CI CD pipeline from scratch we're going to be using GitHub actions to deploy our.net6 web API to Azure where it's going to be running inside of an Azure app service instance so let's dive straight into the code and see how we can make this happen I already prepared a net 6 minimal API that we are going to be deploying to Azure through GitHub actions but first let's see what I actually have inside of our web API I'm configuring Swagger at the start and we're going to be using that as the interface for our API I'm going to remove this check to only add Swagger in development because I want it to be running when we deploy our API to Azure I only defined one minimal API endpoint and all this endpoint does is it Returns the current UTC time I'm going to run the application and quickly show you that what we have here is actually working so we are inside of our Swagger UI where we have only one endpoint that Returns the current UTC time and when I execute it we get the current qdc time in the response body if I keep executing it again and again you're going to see that the time is updating so now I want to focus on adding our CI CD pipeline using GitHub actions and then we're going to deal with deploying our API to Azure I mean the GitHub repository of our current time service and I'm going to add a GitHub action from this UI if you go to the actions tab you will get access to a bunch of pre-configured GitHub actions if you add this one it's going to include build and test steps for your.net application another one that could be interesting is deploying your container to an Azure web app but we're going to build our pipeline ourselves so I'm going to click here to set up a workflow on my own so let's see how we can define a GitHub action to publish a web API to Azure first I'm going to give our action a name I'm going to call it publish and I'm going to add an emoji because I think it's pretty cool when you can add a rocket to your GitHub action we need to define a trigger for our action so I'm going to add two triggers one is going to be workflow dispatch and this is going to allow me to run my action manually on demand and I also want to run my action whenever a push happens and I can specify which branches I want the action to trigger on and I'm going to say I only want you to run the action when there's a push to the main branch now I need to define the next section which is the jobs this is going to be our actual pipeline I need to give it a name for example publish and now I need to Define our published job first I want to say where I want this job to run in terms of the operating system I'm going to specify the latest Ubuntu version as the operating system where I want our GitHub action to execute and now I need to define a list of steps to get our application built and published to Azure now I want to set up our.net environment so I'm going to make a name step which is going to be setup.net is going to use an existing action which is going to be setup.net and I'm going to use version 3 of this action and I need to specify which.net version I want to use I'm going to say I want to use the version 6.0.x where X is the wildcard and resolves to the latest version now that I have our.net version setup we want to restore our application so I'm going to create another step which is going to be restored and I want to use the Run argument here because I want to execute my custom action so I'm going to say net restore and specify the path to our solution file which is going to be current time service Dot sln and this will take care of restoring our application now that we have our application restored we want to build it so I'm going to run.net build and specify the same path to our solution so current time service Dot sln and I want to specify the configuration for the build I want to build in the release configuration and I'm also going to specify the no restore argument because I have just restored the application in the previous step so we have the restore step the build step and now I want to publish our action so I'm going to add a new Step which is going to be publish and in the Run we want to call.net publish specify the path to our solution file current time service dot sln we want to publish in the release configuration and we're going to specify no build as the argument because we have already built the application in the previous step so this is looking good so far I think we are ready to commit our GitHub action and see if it is actually working I'm going to commit it through the UI here and we're going to go to the actions tab and see our action running so I'm inside of the action that is running right now you can see that the restore step has completed the build step completed successfully and the published step completed completing our GitHub action so the yaml file that we just defined worked properly and you can reuse this same GitHub action for any any.net web API to show you that this is actually triggering whenever I commit something to the main branch I'm going to move to our application and here I'm just going to add a simple comment so that we have something to commit so I'm going to say get UTC to describe the endpoint that we are defining in this slide let's go ahead and commit this change and you'll see that this is going to trigger our action on GitHub so I'm using GitHub actions as my git GUI and I'm going to commit our comment and push it to GitHub and this is going to trigger our action now I'm going to move to GitHub and in the actions tab you can see that we have our latest commit it is now running if I go inside we're going to see the publish action executing as you can see our action completed in 20 seconds which is pretty fast usually you want to add another step between build and publish which is going to run your automated tests but I'm going to leave that for a separate video where I'm going to show you how to run your unit tests generate a code coverage report and publish that to some service that allows you to store and view your code coverage results so be on the lookout for that now that we have our GitHub action configured and running we want to set up our Azure app service so I'm going to head over to Azure where we're going to create our app service instance so I'm here in Azure and I want to create an Azure app service to where we're going to run our web API I already prepared a resource Group where our Azure app service is going to live I'm going to specify a name for our service I'm going to try to come up with something with current time service we're actually lucky and this isn't taken I'm going to use cold as our publish the runtime stack is.net6 we want to be running on Linux and let's say in the central us I want to change the plan for our app service to be a free instance so that we don't track up any costs while testing this and I think we can go to the review and create step so whenever you're creating a service on Azure make sure that you carefully review what you're doing so again we are using a free Azure app service instance we call that current time service and we need to remember this name because we're going to use it later for deployment it's going to be running on a Linux machine somewhere in the central us so let's create this instance and this is going to take some time so be patient when you're trying this out at home so here's our Azure wrap service that I just created this is the URL where you can find our application and if I go to that URL we are going to be met with a dummy application that Azure prepares for us but we're going to replace that with our actual Swagger UI when we configure deployment for our application what I need from our Azure app service is this publish profile here we're going to be using that to configure our GitHub action to deploy our code to this Azure app service instance so I'm going to download the publish profile which is just an XML file and now we're going to update our GitHub action to use this publish profile to deploy our application I'm back in GitHub and I'm editing our GitHub action to add the deployment step to azure and we already have an action from Azure that we can use for configuring the deployment so I'm going to create a new Step here and call it deployment and the action that we are going to use is the web apps deploy action so I'm going to specify it here web apps slash deploy and I'm going to use the version 2 which is the latest version and we need to specify a few arguments here so that our deployment works correctly first we need to specify the name of our application and the argument is app name I'm going to leave it empty for just a moment then we want to specify the publish profile I'm going to add that and the last part that we need is the package which is going to be the location where our application is actually published so if you recall from a moment ago where we created our Azure app service we called it current time service so I'm going to specify that here a better approach would probably be to create an environment variable and I'm going to show you now how you can do that so right before defining the jobs I'm going to define a few environment variables the first variable that we're going to Define is the Azure web app name and I'm going to give it the value of current time service then I'm going to create a new one which is going to be the path where our application is going to be published I'm going to call it Azure web app package path and I'm going to give it a value of publish so let's use these environment variables in our pipeline so how we can specify our environment variable here is like this we're also going to do the same for our package path and we shouldn't forget to specify this in the previous step where we actually publish our application so I'm going to add the output argument and use the environment variable that we just defined above so I want our application to be published to the path that I specified here and I'm using that same path here in the deployment step and the last missing piece of the puzzle is our publish profile you should never expose this in your GitHub action because this would be leaking sensitive information in your code so how do you keep this value secret while being able to use it inside of your GitHub action before I give you the answer I'm going to need you to smash that like button and also subscribe to my channel to give me the strength to complete this video and get our application published and running on Azure so GitHub has a way for us to store sensitive information inside of GitHub secrets I'm going to open the settings tab in a new browser Tab and if you take a look at the menu here we have a secret to drop down and we want to define a new secret for our GitHub action I'm going to click the new repository secret and I'm going to place our publish profile inside of a secret I'm going to name the secret Azure publish profile and as the secret value I'm going to paste in the publish profile that I downloaded from the Azure app service dashboard so I created the action and you can see it here I called it Azure publish profile and we can access it as an environment variable inside of our GitHub actions we are back in our yaml file and we just need to specify the publish profile we're going to do that by accessing the secrets and accessing the Azure published profile secret which I just created this completes the deployment step for our Pipeline and I'm going to commit this and run our Pipeline and hopefully this is going to get our application published on azure you can see that our updated GitHub action completed successfully we managed to build and publish our web API and we properly configured our deployment step to publish our action to Azure app service to be able to do that we use the publish profile that we downloaded from the Azure app service dashboard and we store that as a GitHub secret which we used in the deployment step to be able to authorize our deployment I'm back in Azure and I'm going to open up our website again and I'm going to visit the Swagger URL so you can see that our web application is live and running on Azure if you made it this far into the video I want to congratulate you for sticking with me because now we're going to fire our first request to the API and hopefully we're going to get back our response and indeed we do get the current UTC time let's fire it a few more times to be sure that it's working I hope that you enjoyed this video of setting up a CI CD pipeline from scratch I usually only share the code with my patreon supporters but this time I'm going to make the repository open source and available to everyone I'm going to leave the link to the repository in the description of this video but still consider supporting me on patreon because creating these videos is a lot of work and every new patreon gets me one step closer to becoming a content creator full-time make sure that you take a look at these two videos that I prepared for you and until next time stay awesome
Info
Channel: Milan Jovanović
Views: 41,242
Rating: undefined out of 5
Keywords: azure github actions, github actions vs azure devops, github actions vs azure pipelines, github actions ci/cd azure, deploying to azure app service with github actions, azure web app github actions, azure app sevice github actions, azure app service, github, github actions, azure, github actions cicd, ci/cd, deployment, deployment pipeline, ci/cd pipeline github ci/cd, github actions ci/cd, azure ci/cd, azure ci/cd pipeline, github actions ci/cd pipeline
Id: QP0pi7xe24s
Channel Id: undefined
Length: 14min 33sec (873 seconds)
Published: Fri Nov 11 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.