SquareOps

Release automation for .NET projects using gitlab pipelines

About

Release automation for .NET projects
Release automation for .NET projects using Gitlab pipelines, This is Continues integration & Delivery pipleine tutorial for a basic .net application deployment using Gitlab.

Industries

Share Via

Introduction

This article can be used as a quick reference for setting up code release pipeline for .net based projects. Gitlab pipelines is used here , but the solution can be generalised for other CI/CD automation tools available in market

Set Up a Gitlab Runner Machine

Gitlab runner is a build agent where all the build operations happen . If you are using gitlab.com for your git repositories , then configuring a runner in your VPC is a valid candidate for security sustenance

  1. Launch a windows server ( 2016 base server AWS AMI ) 
  2. Verify .NET installation ( 4.6 is installed by default in this AMI )
  3. Download and Install nuget in C:\buildtools. Add it to system path
  4. Configure the gitlab runner ( follow this link )
  5. Download binary to C:\gitlab
  6. Go to Group settings and fetch runner registration token to be used in next steps

NOTE: It is a good practice to register a common group runner for all the projects in a group.

7. Register the runner with gitlab.com

Verify Runner status on gitlab.com ( Group > Settings > CICD > Runners )

8. Install Git on this server from here.

9. Download build tools from Visual Studio Microsoft

NOTE: Installing build tools requires reboot

NOTE: Make sure to restart gitlab runner service after all the utilities are added to system path. Else changes will not be picked up by the service

Configure Application Servers

Application servers also need to be configured to support remote deployment via msdeploy command . This link can be referenced as an in-depth guide .

  1. Use Microsoft Web platform installer to download and install Deploy tools 2.1: Microsoft Platform
  2. Windows Server 2008 supports Web Deploy Publishing using Remote Agent. This requires user credentials with Administrative privileges. Follow this link for setup.
  3. Windows Server 2016 supports Web Deploy Publishing using Web Deploy Handler. This is more secure approach as compared to Remote agent .Follow this link for setup.

Configure gitlab pipelines

GItlab pipelines is now ready to use registered runner for build and deployment actions.

Gitlab CICD environment variables should be used to securely inject sensitive information in pipeline.

This is a sample yaml file which can be placed in project root directory

/Y can be replaced while manually testing deploy command. It runs the deploy command with -whatif flag which executed the deploy command in dry run mode/M represents the machine address where site has to be deployed .

https://172.31.1.224/MSDeployAgentService : Address when when remote agent is used
https://172.31.1.224:8172/msdeploy.axd : Address when Web deploy handler is used

NOTE: notice the username and password injection in deploy script . both are CICD environment variables added in project settings and password is masked to avoid exposing value in build logs

Frequently asked questions

What is GitLab CI/CD?

GitLab CI/CD is a continuous integration and deployment tool integrated with GitLab, enabling automated workflows for building, testing, and deploying code changes.

How can I set up a GitLab CI/CD pipeline for a .NET project?

Define a .gitlab-ci.yml file in your .NET project repository, specifying stages like build, test, and deploy. Each stage will contain scripts to run .NET commands like dotnet build and dotnet test.

How do I build a .NET project in GitLab CI/CD?

In the .gitlab-ci.yml file, create a build job that uses a .NET Docker image. Run the dotnet build command to compile the project.

How can I automate versioning in GitLab CI/CD for .NET releases?

Use Git tags, commit messages, or a versioning tool like GitVersion in the pipeline to dynamically update the version number for each release.

How can I run tests on .NET projects in GitLab pipelines?

Define a test job in the pipeline file that uses dotnet test to run unit tests. This can be included as a separate stage before deployment to ensure quality.

What’s the best way to handle NuGet dependencies in GitLab CI/CD?

Use dotnet restore in the pipeline to download dependencies from NuGet. You can configure NuGet sources in a NuGet.config file if needed.

How can I create a release artifact for a .NET project in GitLab?

In the build stage, use dotnet publish to package the application, specifying an output directory. This published folder can then be stored as a pipeline artifact for deployment.

How can I deploy a .NET application to a server from GitLab CI/CD?

Use SSH or GitLab’s CI/CD environment variables to securely deploy the published artifacts to the target server, or use deployment tools like Octopus Deploy.

How can I manage secrets in GitLab CI/CD for .NET project deployments?

GitLab CI/CD provides a secure environment for storing sensitive information. You can add secrets like database credentials as CI/CD variables, accessible during pipeline runs.

How do I set up automatic deployment on GitLab CI/CD for a .NET project?

In the .gitlab-ci.yml file, set a deploy job that triggers on specific branches, tags, or GitLab releases, so the project is automatically built and deployed when changes are pushed to these branches.

Related Posts