Start with installing .NET 8 SDK

sudo apt-get update

sudo apt-get install -y dotnet-sdk-8.0

Check dotnet version with:

dotnet --version

System Service

Create service file for .NET application

sudo nano /etc/systemd/system/SERVICE_NAME.service

Example service file below replace SERVICE_NAME as desired:

[Unit]
Description=SERVICE_NAME API
[Service]
WorkingDirectory=/var/www/SERVICE_NAME
ExecStart=/usr/bin/dotnet  /var/www/SERVICE_NAME/SERVICE_NAME.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=SERVICE_NAME-app
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target

Run service with:

sudo systemctl enable SERVICE_NAME.service
sudo systemctl start
SERVICE_NAME.service

Create Jenkins Project

Click New Item and create a freestyle project on Jenkins dashboard.

jenkins1.PNG

In Source Code Management choose Git and enter details.

jenkins2.PNG

Select GitHub hook trigger for GITScm polling from Build Triggers.

jenkins4.PNG

Click Add Build Step > Execute Shell and write shell details as shown in below.

jenkins5.PNG

-p:RunAnalyzers=false is added on this example because my service is getting built very slow.

Save changes and move to the next step.

Enable webhook on your GitHub repository settings. Use your {Jenkins IP}:8080

git-hook.PNG

After you push a new commit on selected branch, Jenkins should build your .NET application and publish it.

Check your service status with:

sudo systemctl start SERVICE_NAME.service

If there is any problems you can check your service logs with:

sudo journalctl -fu SERVICE_NAME.service

Bonus: How to solve write permission error on ubuntu for .NET service

 

 

Comments


Comments are closed