This blog is for those users who want to automate the deployment process of ASP.Net application into AWS through CI/CD pipeline using Jenkins.
- Jenkins Server with Windows Slave attached.
- Visual Studio 2019 for ASP.Net Web application creation
- AWS Account
- Let’s get started with deployment by creating a sample ASP.Net Web application in Visual Studio 2019.
Note:- Disable “HTTPS” for now while selecting MVC application type.
- Now press ctrl + f5 (or) ctrl + fn + f5 to build and run the application. A new tab with application will be opened like below.
With this, now we have a web application ready and deployed on the local IIS Server (IIS Express launched by Visual Studio).
Now, let’s move on to Setting up Jenkins Server to run a Jenkins. Assuming you already have Jenkins server running in your local machine, we are not going to discuss how to deploy Jenkins server here.
- Adding windows slave machine to Jenkins server. After logging into Jenkins Server, navigate to “Manage Jenkins >> Manage Nodes and Clouds
Note:- Make sure the remote directory is already created on your windows slave machine, update the label (which will be used in the pipeline), and update the usage and in the above image.
Finally, Click on Save.
- Click on the newly created agent and execute the command from the local windows machine command prompt. Make sure the “agent.jar” file is downloaded already, navigate to the downloaded path, and execute the command.
Also, make sure java is installed and the path is set properly in your local windows machine.
you can click here to download the agent.jar file.
- Once you run the command, you will see the below screen with status as Connected.
- You can also see the agent is now populated with the details.
- We have set up a Jenkins windows agent. Now let’s move on to setting up the path of “msbuild.exe” in Jenkins master.
- First, install the “MSBuild” plugin.
“msbuild.exe” is the application that will be invoked in the background when we build the application through Visual Studio.
Here, we are going to build the asp.net web application using command line instead of visual studio. If we install the visual studio, it will by default install the “msbuild.exe” file also.
Let’s capture the path of “msbuild.exe” in your local machine and add it into Jenkins “Manage Jenkins >> Global Tools Configuration”
Set the path till the “bin” folder only.
- Now, let’s create a pipeline project in the Jenkins and use “Pipeline Script from SCM” under Pipeline Definition.
You can find the Jenkins file here – https://github.com/sajjasgit/aspnetaws/blob/master/Jenkinsfile
You must create an s3 bucket and make sure the credentials added to the Jenkins must have s3 write permissions before proceeding further.
withCredentials([[ $class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', // dev credentials credentialsId: 'AWSCRED', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY' ]])
In order to work with AWS credentials, you must also install “Amazon EC2 plugin” in Jenkins.
- Now we have a Jenkins pipeline setup and ready to build. Let’s add AWS credentials in Jenkins under “Manage Jenkins >> Manage Credentials >> domain (Global).
Also, make sure the global credentials ID added under “Manage Credentials” matches with the “credentialsId” filed in the pipeline script.
- Let’s click on “build”. Here you should expect the “stage(‘Update Auto Scaling to pull latest artifact’)” stage to fail, because there is no auto scaling group available with this name.
- But, you should be able to see that the artifact is pushed to the s3 bucket.
- Now it’s time to create an Environment to host the web application in AWS Windows EC2 instances.
- Assuming you already have the latest terraform installed in your local machine, run the “terraform init” and “terraform apply”.
Here is the path for terraform scripting to create infrastructure and bootstrap windows EC2 instances to deploy asp.net applications.
Here “DeployAspNet.ps1” is the script file that will be used as userdata script for EC2 instances during launch. It will also pull the asp.net build artifact from the s3 bucket and deploy it into Windows EC2 instance.
This will create a LoadBalancer with an auto scaling group. You should be able to access the asp.net web application now with LoadBalancer endpoint.
- Now when there is a change in web application code, you just need to push the changes into the central repo and let Jenkins build it by configuring a trigger. This will scale down the auto scaling group and scale up the instances again. When new instances are being provisioned, the new artifact will be deployed into Windows instances.
That’s all for this demo. Hope you find it useful.
By – Sudhakara Rao Sajja, DevOps Engineer, DigitalOnUs