Azure DevOps (CICD Pipeline) - Part 2
In my previous article, we learned how to setup Azure CI pipeline and trigger on code commit and create artifact. If you have not yet gone through my first article on Azure DevOps, then please check this link: Azure DevOps (CICD Pipeline) - Part 1
Continuous to previous article, In this we will learn how to setup CD pipeline and have auto deployment of code/artifacts on my web server.
In this demo, I am using Amazon EC2 instance for my web server and Git repository for code push. So whenever I will be commiting code to my master branch first CI pipeline will run(which we have already learned in my previous article) and just after this it will automatically trigger CD(release) pipeline which will deploy my artifact to EC2 instance(web server).
To setup CD:
Create environments(this is which contains the servers/resources where you would like to push your code/artifacts):
On creating of environment, you need to add resource and select your resource/server like in my case I want to push my code to AWS EC2, so choose virtual server:
On next click, it will give you powershell script, which you need to run on your server(ec2 machine).
Just login to EC2 and open powershell run as admin and paste this script and hit enter. It might ask you for logic credential where you can use PAT token. To get pat token, from you Azure DevOps portal create new token with full access or copy existing token.
post successful script run on you EC2, you can check your ec2 server manager, local server option, and search for services "pipeline" and it will start showing azure service there.
Also after successful script run on your server(EC2), server start showing into Azure devops environment:
With azure environment, you can also set approval and check - just click on your environment and then click on three dot(...) and select approval and check option. So that before code push to server, we want approval for that particular environment.
Create deployment group - select your ec2 registered machine(you have registered with environment), in case you did not then here also you will get same powershell script to run on your server(EC2).
Create release and select - type of deployment like you can search for IIS (in this case, I choose - IIS website and SQL database deployment)
Provide stage name:
Add artifacts where you need to select your created pipeline:
Now click on task tab or from stage, you can click on tasks. Where you will find all the default tasks added to your release and you need to configure those tasks and if some of the tasks does not require(like in my current case I do not need DB deployment), so right click on that task and either remove or disable mit.
With Deployment stage task: select application/configuration type, provide IIS website name, pool name, binding etc.
With IIS deployment task, select created deployment group where your servers registered:
With IIS web app deploy task - select "Take app offline" option, so that whenever code will deploy to IIS location it will take app offline first and will deploy the code and will again remove app from offline. This is require other you may get error ("file already in use") and your deployment will fail.
Now save the release:
Now it will start showing your new created release. Now add release to this to trigger manual or automatic code/artifact push.
Here just to check, first time I choose manual trigger
Now to run/trigger release manually, go to release just created and select stage and then deploy to stage:
Once deployment started, you can check the log by going into running release:
On successful deployment, you can go and check your EC2 IIS and see it must be auto created/hosted your website inside IIS when you run first time and also artifacts/code pushed inside IIS wwwroot folder.
fyi - On deployment, it will auto install IIS, .net framework runtime or any software which require to host that app and those software does not exists on ec2.
- In my case I used asp.net core web app to host, so everything was auto installed except .net core hosting bundle, which I had to install manually on server(ec2)
Now, to setup auto deployment trigger like I want whenever my pipeline is successful, I want my release deployment should run automatically and latest code-push/artifacts must be deployed to my servers(EC2) instead of pushing/trigger this manually:
Go to release and edit release pipeline:
Click on deployment trigger icon setup from artifact box and will open to model popup, where you need to enable the auto deployment trigger:
Now whenever you push code to master branch, first your pipeline will trigger and on pipeline successful, immediately after release deployment pipeline will start like below:
Categories/Tags: azure devops~cicd