Dealing with Environment Variables in .NET Core in Azure Devops CI/CD pipeline

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone today i will show you how i will be dealing with the environment variables in the local host as well as in the devops pipeline let me tell you why we need to look into the environment manage configurations your production level applications may not only depends upon the development branch it might have keyway branch it might have a city level branch or it might have any other types of friends then it will go to the product level configuration broad level repositories and web apps so when we will build the application in the development time uh we will not face suppose we are just connecting to a database microsoft sql server database we will not going to connect with the production level branches will definitely going to get connected with dev level suppose we are in the qa so we will be going into the qa databases so manually changing all these environment level configurations is very tedious task so how we will be dealing it we will look into it in this video so i have created a sample web api project in our azure devops or you can see the web api name youtube sample web api here if i will go here this is the wavy pair created so let me just show you something over in this app settings.json we will always going to store any kind of environment level details or any kind of secrets or keys over here so though the secrets are not to be stored over here but there are high chances that you are storing over here suppose i have a sql connection string let's just uh have a sql connection string this is common level connection string now uh suppose we have dev environment we have qa environment we have production level engine so we have to generate three application app settings to just store all those values over there i will just create it right now add new items or make sure that there should be a json file this should not be a class level file or any other stops app settings dot dave dot json development is already there so we can come here can i write it like qa dot json so qa.json got connected now store the file over here this is it should be keyway to just segregate it from the common app setting and right now i'll again create another app setting file just copy pasting it there you go so this would be broad level broad dot yes everything looks fine till now i will write the exact sql connection string over here to the production level just changing it to prod let's just see what exactly will be fetching suppose we will create a value controller over here let me add a controller it's a dot net core wave api methodology so you will have controller over here so let it be values controller just by naming convention so it will collect the controllers i will create a gate method similar to this just copy pasting i'm not doing anything over here just giving a route value get app settings following the dotnet core design principle it should return should be an async task and i'm just returning i action result over here and it should return something over here so to get the application setting value we have to inject the configuration level so i'll inject it in constructor so the implementation is inside i configuration configuration i'll just quickly add a class level variable over here let it be config and i'll just copy paste this thing over here so everything got set up now i will collect the sql connection string value let's see what kind of value it is uh actually getting in in terms of localhost running time variable sql string is equal to config so what is the exact name over here it's similar it's same in all the sections which should be same just remember making true same i'll return okay sql connections so we will see what exactly it is written i'll just put a breakpoint over here let's see just just checking that whether i have created a breakpoint no just made a breakpoint over there it should be apa slash values slash your route name so how this api slash values will come just i'll just show you this is the value controller by naming convention it is api then controller name dot net automatically invokes this naming convention so the values controller will be there so controller will be will not going to be in your naming convention it should be only values so apis less values then your route method let's see now it came over here like that my visual studio is not responding right now so it can we can see that so this is actually taking the common level control connection stream so if i will just run it see this is the common level configuration connection string so in development environment we we really don't need this connection string we should need the dev level environment so if i will go here i'll check over there oh okay i have not added it over here i'll just add it so that you should see exactly what is going on it should be there so this was showing the common level configuration this did not take anything from the app settings.qa.production or the development so suppose i am just working in development environment right now and i really need to disconnect with the development database so how to read these values this is very tedious if i will again go to the app settings.json and in common settings in common app setting i will just copy paste and replace this sql connection thing this is very tedious so we should do some manipulation over here so that we can inject this app setting start development level key value pairs into our dot net for avp whenever it runs so how to do that let's go to the program file this is the main method it will be invoked in each time whenever this api runs so here we can actually invoke so how to invoke it just get the configuration details by using configuration binder builder just make sure that it should be builder it should not be binder dot add json file as this is application dot settings i have settings.json file so i just need this value over here dot json dot build one of the most easiest way to what exactly it is doing is it is actually collecting it is config values of this subsettings now if i will do it like i will create an class level environment class level variable called as environment static string env over here class level environment this cnb should take something from the common label app settings.json so that we can use it in our further dynamic allocations of the json files so how we will look into it so here it should pass something let's go to application website install json over here so we will remove it from there and we will just create an environment name enb name let it be name here we will check we will write as key way suppose we just we just want to point it to the qa well in the latter of the section we will look into it how we will be dealing with these things also this thing should not be hardcoded over here we will deal with in the pipeline level so for the sake of right now we can go here and we are just writing it in the name so i'll go here i'll just type this name to get the value now after this thing the dot net code wave api should inject this common label upside installations as well as their setting.jsons in which this environment thing is coming up so i'll just inject it over here dot dotnet provides a configure app configurations which actually takes two parameters to sorry it's a delegate so it's actually taking a delegate you can give any kind of name over here it should just go here everything looks fine i'll just write it like this config dot add json file first it should inject the common level application settings we'll just copy paste from you oops now it should add the dynamic app settings file right now this environment is pointing to the keyway so it should inject the q a level app settings this thing this part dot same thing just i just need to add json file over here same thing i just need to copy paste it i'll not waste my time as i'm showing it practically it will take a lot of time to write i'll just make a string interpolation over here then here i'll create the environment i think everything goes well over here i'll put a breakpoint and let's see what exact value we are reaching through this environment value environment variables so now we are just fetching the environment name just let's see it's keyway it's perfectly fine it's the common app settings and there we have made it as keyway now we will go to here i will put a breakpoint over here and we will check if we we can inject it a lot yes it's getting injected so now the dot net code app is running now we will go to the same location slash api slash values get have setting values whatever the name convention we have followed get app settings it's coming here now it's actually getting the common connection string from keyway so for development similar thing you have to just change the naming convention over here over here to dev environment so right now it's everything looks fine oh it's everything is fine over here we really don't need any other things but you see whenever you are changing something it should be die it should be the global way global it should change something in global way so right now there will be some developers task or some support label stuffs tasks to change these things to development or to change these things to qa and again redeploy the code so it should not be done like that we will look into it how we will proceed further i'll just make this changes i will just push all those quotes to my solution my github i have configured dot key tigner file okay so i think the git heat node file has not been configured properly we will look into it later for now i'm just checking it change add app settings for dave qa and broad branch always give a good comment so everybody can understand it now the code got committed i'll just i just want to sync it oh unable to push the remote repository because local branch is behind the remote branch just i'll just pull everything there is no changes i'll look into the repository just refresh it see whether you have the app settings yes there you go you have three applications over here i'll just rebuild it so let's uh go to the branching level terminologies here i only have a master configurations just i'm just going to the branch here i have a master level configurations so suppose i have development uh i i have actually created three web apps in the azure portal just let me show you so this is the resource group i have created and these are three web apps for the development for the production level for the qa level everything looks very pretty and everything looks so everything is linux based web applications as dot net core is platform independent we can delete linux based platforms right now i have already merged my code to the master branch and here if i will see in the master branch i have already got all those things but to just publish it to the web app we can actually go over here if i want to just publish it over here i can publish it manually i'm just showing you for the sake of this thing but i will not publish it manually i will just show you the steps so here you can choose the app service plans linux as i have linux based web apps here it will show you the resource group here you can publish it in any of these web apps just you need to click the finish button but but again it's a manual process right and there will be a person sitting over there and it should be he should be the responsible person to publish it so now i will do it i will automate this entire job so how exactly we should automate it i'll just show you i'll go to our pipelines i will create a pipeline for this dot net core level web api so what exactly it is doing whenever it is publishing it whenever if i will go here if i do start it will actually do three things it will restore all the configurations all the nuget packages and all those dependencies then it will again rebuild then you will again it will create a gif file and it will just send those zip file to the particular particular configuration file particular web app so i have three wave of over there so here i am just doing it but i will do it and i will create an automated pipeline so that's called as continuation integration pipeline so i'll create that pipeline go to over here pipelines pipelines created product pipeline so here i can write my ml code for the actual repository git there is bitbucket there is github there is github enterprise server and everything over there but we will choose the classic editor mode it's the simplest version so my repository is as your git you have to choose by yourself whether it will be github or whether it will be github enterprise for your project level so mine is in azure repose gate so i'll choose it i'll just choose it for the master branch for the master branch i am doing this pipeline so all the codes in the master branch only got chipped and it will be built and it will be stored in a zip file so we will look into it so this is a dot net core project i will just choose the dot net core over there dot net core i'll choose it now it actually gives you a small pipeline pipeline related stuffs it's actually a ml code but microsoft azure device provides you a lot of flexibilities in doing this thing and it has automated all those things so that you just give some variable names over here some naming convention and it will give you the ml exactly i mean level code will look into the ml level code data so the naming convention i'm just following is uh sample youtube sample what was the youtube sample web apa now it is a build process so what exactly is in police agent pool will have the responsibility to take your entire code and create a build so in localhost what we are dealing is in your computer there should be an agent which is responsible responsible to build your entire build configurations and publish it so that is ms build over in our pro in windows os so here microsoft provides you an isn't pull which comes from the microsoft azure device we can choose it here it can do a lot of tasks for you so we can do a manual we can create a manual agent pull but that would be a tedious task again uh most of the cases as your pipeline season pool can do everything for you but the only catch over here is you have to wait so that the agent pull should be available for your deployment for your build process so for the agent specification as we are just dealing with the dot net code application we can go for ubuntu or we can go for any windows level environment i am just sticking with ubuntu 16.0403 file parameters would be cs project and we don't have any test project over here i'll just deselect it remove it so what exactly we can do is this is a production level pipeline so you can actually create a test test ml file over here you can create a code coverage you can create a vulnerability checks over here i'm not just going into that those things but we can actually build everything over here right now i'm just going for this restore so what all those labels dot net code deals with each it first restores everything all those new get packages and dependencies then it will actually reveal the entire project then it will publish it then it will create a zip file and it will store it in some location in localhost if we will go over here we have already made a build so if i will go over here you can check it over just give some time so that it will be open so here if i will go over here build debug code 3.1 you can see the entire project level structures over here we have a sample.exe file this is actually the the zip file should contain all these things so this should be deployed into your azure cloud web apps so what exactly it is doing it is doing the same thing it is doing the same thing it is first restoring the cs project file then it is just building everything this display name you can actually change all those things but command is built now what exactly it is doing is it is taking the build configuration from the variables here we have build configurations here we have actually named it it's not actually named by us it's actually named by the microsoft we can actually change it release or debug mode or build platform for any cpu or 64 bit or 86 64 bit or any kind of cpu or 86 bit also sorry 32 bit also now i am going to the restore build then publish and here it's actually taking the build configuration it is creating a output path and it is publishing all those things over there now this thing is actually creating a gif file it is collecting all those files from the those paths and it is collecting and creating a g file named as drop and it is storing it let me just save it i can just save it or cue it just writing a comment building build pipeline now everything looks fine my branch is faster so i'll just run it and let's check what exactly it is doing now i'm going to the agent job you can see this thing what exactly it is doing now it's been cute this list let's wait as we are using microsoft's agent so if it's not available you have to just wait that's all now we can really go here we can see how initialized job how it's been checking out everything from the github now it is restoring everything and if we can go over here we can see what things got restored and what things again got built over here so now the artifacts has been published if i'll i'll go over here everything looks fine success so here artifact has been published i will go over here and you can see the drop file so for the sake of time i'll just show you what exactly inside this artifacts i already have see same things whatever we have seen there and it created a dll class as well for your web app now we will move on to the next part now the drop file has been already generated so we really here we are actually what we are doing is we are actually manu manually just triggering this job so we don't really need to do that we can just go over here in our edit pipeline edit so you will have a trigger option over here so here we can use enable continuous integration it will do the automation whenever we will tick it so there will be a branch configuration over here whenever there will be changes in this branch it will generate a build file and it will store the latest jeep file into that artifact storage so right now i will do it i will create an enable continuous integration i will just have only one branch i will create those things and i'll just save it so how to look into it how exactly it is working or not we'll go over here we'll go over here we'll type something over here in our values controller suppose we will type and comment just checking the master branch pipeline builder i'll just push this file i have i'll just change this file and i'll push this to the master branch see i already have it changed so i'll just check if the branch ci is working i'll just push it now as we have changed something in the master branch and there we have already triggered we have already created a continuous continuous integrations we can go to the pipeline and we can see let's see this pipeline has already been run now if i will go here you can check this is actually generating another gif file latest chip file and why this pinga trigger as we have already just merged something into our branch into our master branch so this actually got succeeded if i will go over here yes that effect has already been produced so this is the ci level pipelines now we can actually go to the cd what exactly is continuous deployment is suppose we have depth we have here you if you will see we have dev production qa so these files doesn't have that g file right now so g file has been made and it's stored in the azure devops storage so we just need to extract the g file and we just need to copy all those things to here but just make sure that the application setting which are being which we have already actually made in the development branch it should actually point to the dev application setting in the production it should point to the production application setting and in the qa it should point to the q a level application settings so how to do that we will build a release pipeline so if i will go here in the release section new pipeline so here everything is already got shot set up by the azure device this is the this is the way more easiest thing as your devops is giving us so here what exactly is doing i'm just closing it i'll show you from the scratch so artifacts has already been made and it has been stored in this single storage file in azure devops so we just need to grab those files from here so project label is fine project is same so we just need to get the build so this is our build name we'll collect the latest gif file and we'll add it now the g file has already been connected to this pipeline now we have already three servers for dev a qa for production so we will create three stages over here not three stages we will create uh three stages just i will add and one stage over here and it's an azure app service deployment i'll just choose that thing i'll just use the name as dave over here and i'll just save it similarly i will i'll just uh make sure to change the jobs so what exactly we are actually doing in this job is we'll fetch the jeep files and we will just put it into the azure web apps over here it is the day web apps so how to fetch we will just initiate our azure subscription it's a free trial subscriptions i have i'll just authorize it it will ask me for the sign actually actually it creates a service connection so your file is in azure devops and your web apps is in azure portal so it has to there should be some connection so that it can actually fetch those files from your repos and it will uh take those files to your azure portal web apps so i'm just doing it so sign in got completed now if i will look into it the app type is web app on windows but we have already created these things in linux servers so in windows i really don't find any app service name see this is telling that no results form if i will just choose web app on linux it will definitely fetch those three web apps see now i will choose the website web api test dev and i will just save it now i'll just create a release over here let me show you what exactly goes i'll go to the release pipeline you can actually check that it has been cured it will collect the zip file which has been made when we change the master branch it it will collect that zip file and it will store it into the dev web app server now the task has been initiated we'll just wait to make this task to get complete for the time of being we can go to here we just need to copy this value now i'll copy the url over here i'll go over here and i will wait to make sure that it's been deployed successfully you can actually we can actually go and see the logs over here why it's taking so many times or is it really got succeed or not we can actually refresh it and see so finally the task got succeeded now the g file has been deployed to the to this web web api test hyphen dev so we will go here we will copy paste the vp address slash api slash values then we just need to get the exact value i'll go to the values controller i will get this route path i hope i am doing right so see what exactly it is doing is it is actually extracting this is a common level connection string hyphen qa this is not a common level configurations i actually missed to delete it it's actually collecting the connection string of keyway why this thing happened because in our deployment pipeline we did not tell the azure tables to actually go to this app settings.json and change this environment name this has been hard hardcoded over here that it will always going to point to the qa it has not been changed so if i will go to the depths to the developer pro azure devops i'll go to the pipeline release pipeline mainly if i'll go and edit see there i have not declared anything anything so that that application setting will be replaced because it's been hard coded over here and it will always going to point to the qa so how to replace those things from the pipeline level if i will go here there is a variable sections over here i can actually add something over here and i will just collect these well this key and i will just paste it over here so it actually replaced should actually reflect to the development branch i'll just write development that should be equal to the application settings file name app settings.development.json this development should be replaced as we have actually written in the program.cs5 that this environment should collect the value coming from this part but this part should come from the pipeline now i have already written it as development but see there is scope over here now it's actually pointing to the entire pipeline but we actually don't need that we actually need to point it for the dev environment only so here i'll just select the dev staging now just save it let's see what happens i have just already saved i will create a release over here no it should not work because if i will go here in the pipeline i have not written anything over here so that it will actually uh it will actually change the environment variables from the pipeline to the app settings.json file see if i will show you the aml over here you can see that there is only one task there is only one step and that is for the deployment process and that's all so for that reason i will create another thing over here what exactly i need is i just want to replace those values in the app settings dot json so i can just go it over here and i will just search it file transform file transfer now see replaced replace tokens with variables values in xml or json configuration files i'll just go here i'll add it and i will go here the file format is json type just i will check and the target files what i am actually doing is star star slash app settings dot json it will actually search the entire folder entire solution folder it will collect the app settings dot json file and it will actually change the environment name over there so everything looks fine but this thing should be made before the deployment goes so it should come here now the pipeline looks fine i'll just try to save it i don't know why this save button is not showing i'll just check if this got completed no sometime it happens so i'll just re-generate that file file transform i'll go over here i'll add that thing and here i'll choose the json format the file type should be search in the all solution folder go for the app settings dot json i hope i actually i have absolutely in the correct spellings now everything looks fine i'll just save it once this thing got saved i'll just move it to up and i will again save it so now the deletes got complete i'll just create a release and i'll just see whether we can face the development level configuration values or not we can wait until the day branch gets succeeded the task just got started other things looks fine over here we have already copied and this should actually this should get the value what exactly it should get i'll go over here it should collect these values as i am just injecting my environment name from the release pipeline to here this place now the program.cs file will be injecting those things here so this environment will collect the development and this will actually generate the dynamic application app settings dot json file injections now if i will go to the pipelines it got succeeded now i will just try to fetch and see this actually pointing to the diff branch right now similarly i can go for the keyway i just need to copy these things the same thing exact same thing just go here i just need to change the web applications to qa i will just clone it i'll copy it now i'll go to this job task everything looks fine everything is same over in this file transform i'll just change the app settings.json over here i'll go here and i'll just change it so that it should point to the qa branch everything looks fine for me just let's try and yes if we will go to the variable it will auto generate the file from those things i'll go over here if i'll go over here i will just need to change the name over its keyway just save it as qa over here if i will go here right now it will show you keyway staging and i just need to change this thing to point it to qa now i'll save it everything looks fine i'll create it oh i'll just go for the production level pipeline also from here that should be much more easy task go over here i will change this thing to broad so this should print to the production and this should point to the production level web apps everything looks fine over here over here now i'm going to the variable now see variable has a production level scope i'll just change it to prod now it got saved everything looks fine for me if i'll go here i can shape but this entire pipeline will be automatically deployed after dev there will be qa after qa there will be production but we really don't need in our uh real time scenario we never approve the production level uh pipeline should be injected automatically from the qa so what if there is some bug so it will automatically go to the production so we really never approve this thing so how to remediate it so we will add a pre-deployment conditions over here so here actually you have something pre-deployment approvals i will just write it as yes so it should be your manager it should be a high-level people so who should take the responsibility you have already added to into your azure devops groups so you can write their names over here but i only have my own username so i'll just write those things over here now we'll just save it so what exactly i'm doing is if i'll just save it i'll go over here so in dev and in qa it will be automatically deployed after dave it will go to the qa but in broad it will actually need some approval it will be manually approved after that only we can fetched so i'll just show you into the create release pipeline you will get to understand i'll go here and it's been cured so let's wait for two to three minutes at least now dave is going on now it got completed now see the qa got triggered automatically we can go to there actually we will check whether everything looks good or not see it's actually working fine now we will go for the qa pipeline qa url i'll go here i will copy paste that url this one i'll create a new tab here i'll paste it i'll just paste the route path over here let's see whether the release got completed or not it's been still in process so if if what i can show you is what will happen if i will click it there will be no files available over there until it got released so you you really can't you really can't go here and check is the website is available or not the website is not available right now as the release process is going on once that will be succeeded we can go and check see the website is not available for now i'll just refresh it you can go here and check the logs run on agents successfully deployed everything looks fine now java has been finalized we can go to the pipeline over here i'll just copy i'll just paste the keyway url see now it is pointing to the qa it's pointing here it is pointing to the div here it is pointing to the keyway with same single code base now if i will go to the release pipeline see we have already given the pre-approval conditions to be enabled so it actually sends a mail to your repo to your to the consent person if i will show you to here outlook see here i have already got a pre-deployment approval is pending so i can go from here to view approval and see this is the release pipeline i can approve it here i'll just add a comment approved now i'll approve this hopefully everything is looking good and it's been cured if i will go to the production level web app prod i'll just copy it i'll just paste it over here under our path till now the pipeline has been cured and it's been progressing so we can't access the production web app we'll just wait package deployment using zip deploy initiated after that it will successfully complete the production level web app deployment successfully deployed to the web packages to app service now it will give you the url over here now it gives you the url the job got complete and now we can actually go to the production level and see this is the common level connection string and this is actually indicating to the prod the only one thing is remaining over here so if you will see we are actually manually deploying it manually in manually just uh triggering the release so what we can do here is here we can do the continuous deployment process here i can go over here or i can schedule something over here i just need to go over here i'll just need to have a continuous deployment trigger over here let's see i've already made it as enabled copy paste it now i'll just for the sake of showing you i'll just go here i'll just save it change something over here whatever sing string i am just getting i'll just add something over here i'll create a string interpolation sql string checking the entire ci cd now what should happen is when i will merge it to your my master branch the master branch will create the zip file automatically and when that g file will be created my release pipeline should automatically get triggered there should be no manual triggering process will go on it should be entirely automatic if i'll go here i'll just go to the team explorer here changes here there is a value controller change for check ci as well as cd it got pushed now if i will go over here over the pipelines we can see that just now it got triggered it will be a little bit uh time consuming task so i'll just want to show you the entire process entire end-to-end process so that you can build your own projects you can just completely make your production level projects by using this configuration now there isn't job card completed the artifacts got published now if i will go release see there process has already been begun if i will go here i'll check this release file this not manually triggered this has been automatically triggered day process got completed if i will go here this is my dev web app once it will complete we will see what exactly the value is here we have already changed over there to write it like the string as well as checking the entire ci cd pipeline so it should give you some different value well let's see if they've got succeeded we can now check now see the entire value is getting reflected over here now if i will go to the keyway this one is your keyway we have to wait until it gets complete it got succeeded if i'll go to keyway now see it's actually pointing towards qa and it's actually got the latest values checking the entire ci cd now if i will go to the go to the pipeline i will approve it approved approval got complete it's been cured just i'll see the broad web app for the last time now the task got initiated we can go here and we can see the progress everything looks fine now the artifacts is being downloaded now the deployment is going on you can see over here what exactly file transform is doing so if you will go over here see this is being written like substituting the value on key environment name with broad this value got substituted where it got substituted in app setting.json over here if i will close this it has already been finalized i will go here for the broad branch it's pointing to the pro now if i will just check see the entire connection string as well as the latest value we have changed checking the entire csd is being reflected so this is the entire production level workflow you can implement thanks for watching this video just if you like this please subscribe to this channel thanks for being all of your support bye
Info
Channel: Anshuman Dikshit
Views: 2,576
Rating: undefined out of 5
Keywords: Azure, DevOps, .NET Core, WebAPI, c#, pipeline, microsoft
Id: giMx53pqDrY
Channel Id: undefined
Length: 66min 51sec (4011 seconds)
Published: Fri Nov 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.