How to build and deploy a .Net application with Jenkins and AWS using devops automations?
Some guy asked this on github but due to dowvotes, the question was deleted :(
Anyway I think it was a good question because .Net is not like the other languages which were linux driven from the beginning and use devops is super easy. Also in some platforms .net is discriminated I think because of Microsoft. For example in heroku, c# is not suppoerted
Everything is linux and Netcore know it
This is the corner stone. If the technology is not well supported on linux, the history ended without having begun.
With Netframwork, linux was not fully supported. The only option to deploy an application was a windows server. A human should open the visual studio, load the code, generate the build files and deploy them to IIS Server
This was a c# disadvantage versus java, nodejs, python, php, go, ruby, etc. With Mono framework there was a chance but so complicated
That’s why Microsoft with all its money, created Netframework and Dotnet.
So remember this:
- Netframework = Old school and destined to die, thank to Windows servers
- Netcore = C# Revamp to allow linux deployments. Late to the party, but the worst thing to do is nothing.
Netcore and dotnet
With Netframework and Dotnet, we can build a c# application like maven in Java :)
dotnet restore "TestApi.csproj"
dotnet build "TestApi.csproj" -c Release -o /app/build
dotnet publish "TestApi.csproj" -c Release -o /app/publish
cd /app/publish
dotnet TestApi.dll
Whit these commands, you will have a Netcore application running on Linux without Windows IIS. It uses a new server called Kestrel:
- https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-7.0
- https://www.infoworld.com/article/3670792/kestrel-the-microsoft-web-server-you-should-be-using.html
Docker
If the technology is compatible with linux, you could use docker to automate even more.
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80/tcp
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["TestApi.csproj", "./"]
RUN dotnet restore "TestApi.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "TestApi.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "TestApi.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TestApi.dll"]
Full source code here: https://github.com/usil/challenge_microservice_template
Goal: You should achieve this : docker build ...
and docker run ...
. The app should work in your localhost before the devops automation
The most basic steps
If your application Netcore application is dockerized, follow these steps to understand how to deploy it into aws using jenkins
#1 Jenkins scripted pipeline
Create a hello world in jenkins using scripted pipeline: hello world
#2 Webhook
The cornerstone is webhooks, so research about it. Check my post https://jrichardsz.github.io/devops/devops-with-git-and-jenkins-using-webhooks
Goal: You should achieve this : some git push should print a hello world in jenkins.
#3 Build a docker image
Instead of a hello world message (point 1) you should launch the git clone and the docker build commands to build an image in the local disk of jenkins
Add the commands in your jenkins pipeline: docker build ...
Goal: Confirm that your image exist running docker images
in jenkins
#4 Push a docker image
Configure a basic docker image repository like this in aws or use some public like https://hub.docker.com/
Push the docker image to your docker image repository:
- https://stackoverflow.com/questions/28349392/how-to-push-a-docker-image-to-a-private-repository
- https://jsta.github.io/r-docker-tutorial/04-Dockerhub.html
Add the commands in your jenkins pipeline: docker push ...
Goal: Check that your local docker image was upload to your docker registry
Try to use a public image to avoid docker login.
#5 AWS EC2 Server
Since your are learning, you should start with the most basic way, so create an ec2, install docker
https://www.thesunflowerlab.com/jenkins-aws-ec2-instance-ssh/
Goal: Perform some docker commands from a remote shell (your laptop), to the created ec2 like : docker ps
#6 Pull the docker image and run it
Getting back to the jenkins job, after the , connect to the ec2 and perform:
- docker pull … you image
- docker run … -p 80:abcd …
Goal: Open the default public http domain of your ec2 and your netcore application should be load
Summary
The previous steps is just to learn the basics. If you achieve this you could gradually use more advanced tools:
- AWS ECR : docker repository
- docker orchestration tools (kubernetes, aws beanstalk, etc) to avoid the manually dockerd api expose
- Configuration manager to handle your netcore (or any language) variables (secret or not). I developed a ready to use tool: https://github.com/jrichardsz-software-architect-tools/configurator
- A lot devops and infrastructure tools in aws