The Wayback Machine - https://web.archive.org/web/20241006101530/https://www.geeksforgeeks.org/implementation-of-ci-cd-in-net-application-using-shell-executor-on-gitlab/
Open In App

Implementation of CI/CD in .NET application Using Shell Executor on GitLab

Last Updated : 30 May, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Continuous Integration and Continuous Deployment (CI/CD) have revolutionized software development, allowing teams to automate build, test, and deployment processes for rapid and reliable software delivery. In the .NET ecosystem, implementing CI/CD pipelines is important for maintaining code quality, improving collaboration, and accelerating time-to-market.

GitLab, a popular DevOps platform, offers powerful CI/CD capabilities, including the versatile Shell Executor, which enables seamless automation of tasks within CI/CD pipelines. In this article, we’ll explore how to implement CI/CD for a .NET application using GitLab’s Shell Executor, empowering you to streamline your development workflow and enhance productivity.

Understanding CI/CD with GitLab:

Before diving into implementation details, let’s briefly review the concepts of CI/CD and how GitLab facilitates their implementation:

  • Continuous Integration (CI): CI involves automatically building and testing code changes whenever they are pushed to a shared repository.
  • Continuous Deployment (CD): CD extends CI by automating the deployment of code changes to production or other target environments.
  • GitLab Shell Executor: The Shell Executor is one of GitLab’s available executors for running CI/CD jobs. It executes commands directly on the CI runner’s host machine, providing flexibility and control over the execution environment.

Requirements

Software

Description  

Git             

This is the first requirements, to commit the changes on GitLab. It is a version control software that tracks the changing set of files.

GitLab Runner  

The work of the GitLab Runner is to pick up and execute the job (when a new commit happens).

MSBuild.exe                                         

Download MSBuild.exe and save it in your local directory, It helps to build the project when GitLab Runner pick up the job. If in a machine, Visual Studio is already installed then It will not be needed to install. It can be found C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe (find based on Visual Studio version)

Nuget.exe

Download Nuget.exe and save it, in the same directory where MSBuild is available.

 Path Configuration

Software/File

Path Description 

Git

After Successfully installation of Git on the machine. The below path needs to set to communicate with GitLab repository. Go to System Environment Variable, add these two with User Variable path

  • C:\Program Files\Git
  • C:\Program Files\Git\bin
MSBuild.exe    MSBuild.exe path will be needed in Yml file, while configuration with GitLab. For Example, C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe or where your MSBuild is available. 
Nuget.exe

Nuget.exe path will also be needed in Yml file, while configuration with GitLab. For Example, C:\Tools\Nuget\nuget.exe or where your Nuget is available. 

Mstest.exe

This is useful to run the test cases of .Net,in Visual Studio 2019, it is available in C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\mstest.exe

.gitlab-ci.yml

This file should be inside the root directory of the project which contains all the CI/CD configuration including software and script path. Here, you can mention how this repository should run. Before adding this file to the root directory, should check it is a valid yml file or not.

 
GitLab Runner Set Up

Follow the below steps to download and configure the GitLab Runner.

1. Download GitLab Runner for Windows

2. After downloading successfully, save it in your directory (For ex: C:\Tools\GitLab-Runner), and rename it with the following command

gitlab-runner.exe

3. Open Command Prompt on Admin Mode, go to GitLab-Runner directory and check the status of it using 

gitlab-runner status

4. If it is already running, stop it before registering a repository with GitLab Runner 

gitlab-runner.exe stop

5. Once GitLab Runner has successfully stopped then use the following command for the repository registration

gitlab-runner.exe register

6. When you do repository registration with GitLab Runner, the below questions have to answer.

  • Enter your GitLab instance URL: It can be different with each organization and format will be like http://gitlab.example.com                    
  • Path: Go to GitLab Account → Select repository which you want to register with runner → Settings → CI/CD → Expand Runner
  • Enter the gitlab-ci token for this runner: It will be a unique token of each project which will need while registration and it can be found    
  • Path: Go to GitLab Account → Select repository which you want to register with runner → Settings → CI/CD → Expand Runner
  • Enter the gitlab-ci description for this runner: Put Runner name(any name), which will help you to remember that which runner’ is running
  • Enter the gitlab-ci tags for this runner: It is an optional, if you want to start GitLab runner when specific tag is available in yml file.
  • Enter the executor: There will be list of several executors, and type shell(as GitLab Runner will run our system)

7. After successful registration, start the GitLab Runner 

gitlab-runner start

8. To verify that GitLab Runner has registered the respective repository and the runner has been started. Go to GitLab Account → Select repository which you want to register with runner → Settings → CI/CD → Expand Runner, There will be a green color circle will be available, and displaying message will be Runners activated for this project.  

Note: If the circle is gray, it means the runner has not started and starts again.

 Windows GitLab Runner Command

Command

Description 

gitlab-runner.exe register Register the project with GitLab Runner 
gitlab-runner.exe start Start the runner 
gitlab-runner.exe stop Stop the runner
gitlab-runner.exe status To know the status of gitlab-runner 
gitlab-runner unregister –name  test-runner   Unregister Runner of a project and replace the test-runner with your runner name and this name can be found inside the config.toml file (where your gitlab.exe) available.
gitlab-runner unregister –url http://gitlab.example.com/ –token t0k3n Remove Runner by URL and token 
gitlab-runner unregister –all-runners Unregister All Runners
gitlab-runner restart This command stops and then starts the GitLab Runner service
gitlab-runner uninstall This command stops and uninstalls the GitLab Runner from being run as a service
gitlab-runner exec To see a list of available executors, run
gitlab-runner –help Check a recent list of commands by executing
gitlab-runner run –help Can see the name of the environment variable
gitlab-runner –debug To run a command in debug mode
gitlab-runner exec shell To see a list of all available options for the shell executor, run

.gitlab-ci.yml:  This is a format of .gitlab-ci.yml and changes it based on requirements 

variables:
NUGET_PATH: 'C:\Tools\Nuget\nuget.exe'
MSBUILD_PATH: 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe'
MSTEST_PATH: 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\mstest.exe'
TEST_FOLDER: '.\test\bin\Release'

stages:
- build
- test
- deploy

before_script:
- '& "$env:NUGET_PATH" restore sourcecode\project.sln' # sourcecode\project.sln-This path includes project solution where is available and restoring
- '& "$env:NUGET_PATH" restore test\test.sln' # This path includes test solution where is available. and restoring

build_job:
stage: build
only:
- developer #this branch will run on GitLab Runner and can change it.
script:
- '& "$env:MSBUILD_PATH" sourcecode\project.sln /p:DeployOnBuild=true /p:Configuration=Release /p:Platform="Any CPU" /P:PublishProfile=FolderProfile.pubxml'
- '& "$env:MSBUILD_PATH" test\test.sln /p:DeployOnBuild=true /p:Configuration=Release /p:Platform="Any CPU" /P:PublishProfile=FolderProfile.pubxml'

artifacts:
expire_in: 365 days #artifcats will be stored only 365 days after this it will expire
paths:
- '.\sourcecode\project.sln\bin\Release\Publish\'
- '.\sourcecode\project.sln\bin\Publish\'
- '$env:TEST_FOLDER'
- '.\$env:MSTEST_PATH\*.*'

test_job:
stage: test
only:
- developer #this branch will run on GitLab Runner and can change it.
script:
- .\test\test.bat #This is bat file, if the test are written in script format

dependencies:
- build_job

deploy_job:
stage: deploy
only:
- developer #this branch will run on GitLab Runner and can change it.
script:
- 'xcopy /y /s ".\sourcecode\project.sln\bin\Release\Publish\*.*" "C:\solutionDir"' #Path where you want to store the solution


dependencies: #after successfully build, only test stage will run
- build_job
- test_job

Code Commit: After successful configuration, do commit and see the job status on GitLab. Go to Project repository → Select CI/CD menu 



Similar Reads

Implementation of CI/CD in Java application(Linux) Using Shell and Docker Executor on GitLab
There are many executors available to implement CI/CD with GitLab Runner. However, Shell and Docker are more popular among them, and we can easily configure a repository with these runners. These runners can be chosen based on the requirements and availability of the resources. This article mainly focuses on Shell and Docker executors of Java Linux
6 min read
How To Create A .gitlab-ci.yml File in GitLab?
GitLab CI/CD automates the process of building, testing, and deploying code, ensuring your software is always in a releasable state. At the heart of GitLab's CI/CD pipelines is the .gitlab-ci.yml file, which defines the stages, jobs, and commands that need to be executed in a CI/CD pipeline. In this article, we’ll walk through how to create a .gitl
6 min read
Difference Between Bind Shell and Reverse Shell
A shell is a program that interprets our commands and gives the written commands to the operating system. It acts as an interface between the user and the operating system. It takes input from the keyboard and gives it to the OS, and the terminal lets you type commands and interact with the shell. When to Use Reverse Shell and Bind Shell?Both rever
6 min read
Install GitLab By Using Docker
GitLab is a popular DevOps platform that provides a complete suite of tools for managing your software development lifecycle. It includes features like version control, CI/CD pipelines, and project management, making it an important tool for many development teams. In this article, we will walk you through the steps to install GitLab using Docker.
3 min read
Setup GitLab Repository On Windows 10
GitLab is a place where we can manage our git repositories. It can be considered as an alternate of GitHub and Bitbucket. Currently, Gitlab is offering four versions namely Core, Starter, Premium, and Ultimate. Core: For small and personal projects. Free of cost.Starter: For small and personal projects who need professional support. It is paid.Prem
2 min read
How to Delete Branch in Gitlab?
When working with Git, it's common to create branches to isolate changes and work on new features or fixes. However, once a branch has served its purpose, it's often necessary to delete it to keep your repository clean and organized. In this article, we'll see the process of deleting a Git branch using Git commands locally on your machine. Using Gi
2 min read
How To Clone a Repository From Gitlab?
Cloning a repository in Git involves creating a local copy of a project from a remote server. This allows you to work on the project on your local machine and later push your changes back to the remote repository. GitLab, one of the popular platforms for hosting Git repositories, provides a straightforward way to clone repositories. PrerequisitesGi
3 min read
Managing Git Repositories with GitLab
GitLab is a powerful platform for managing Git repositories, offering a list of features that provide collaboration, code review, continuous integration/continuous deployment (CI/CD), and more. This guide will walk you through Managing Git Repositories with GitLab. Table of Content How to Access GitLab?Uploading a code file in the repositoryCreatin
3 min read
Difference Between GitLab and GitHub
Version control systems are important tools for managing code changes and collaboration. GitLab and GitHub are two of the most popular platforms in this space. However, they have key differences that can influence which one is the best fit for your project. In this article, we will explore the differences between GitLab and GitHub, highlighting the
3 min read
Ultimate Guide to Advanced Git and GitHub: Overview of GitLab & Bitbucket
In software development, mastering advanced tools and techniques is important for efficiency and collaboration. Git, GitHub, GitLab, and Bitbucket offer features beyond basic version control, like rebasing, cherry-picking, stashing, submodules, and Git hooks in Git, as well as GitHub Actions, GitLab CI/CD pipelines, and Bitbucket Pipelines. This ar
5 min read
Permissions And Roles in GitLab
One of the important settings of GitLab is its permission and role system, which decides what users can and cannot do within a project or group. Understanding GitLab’s permission model is important for managing access, collaboration, and security within your teams and projects. This article will explore the various permissions and roles available i
5 min read
How To Create A Personal Access Token in GitLab?
A personal access token (PAT) in GitLab is an alternative to using a password for authenticating GitLab API requests, Git operations, and other integrations. These tokens provide more control and security, allowing you to define specific scopes and expiration dates. In this article, we'll cover how to create a personal access token in GitLab, inclu
4 min read
GitLab Upgrade Path
Upgrading GitLab is an important task for maintaining the security, stability, and performance of your GitLab instance. It’s important to follow the recommended upgrade paths to ensure a smooth transition without breaking your setup. GitLab regularly releases new versions, including major, minor, and patch updates, each introducing new features, en
4 min read
Introduction To GitLab Flow
While Git itself offers a lot of flexibility, managing workflows can become complex, especially when dealing with different environments like development, staging, and production. This is where GitLab Flow comes into play, providing a structured approach to handling branching and deployment in a way that aligns with your development and deployment
4 min read
How To Remove A GitLab Project?
Removing a GitLab project can be necessary when you no longer need it, or if you want to clean up your workspace. Deleting a project in GitLab is straightforward but requires caution since it permanently erases all data, including repositories, issues, and merge requests associated with the project. In this guide, we will walk you through the steps
3 min read
Gitlab CLI
GitLab, a leading platform for version control, offers a range of tools for managing your repositories, CI/CD pipelines, and more. The GitLab Command Line Interface (CLI) is one of the most powerful tools provided by GitLab and helps to interact with GitLab services directly from the terminal. The GitLab CLI simplifies tasks like managing repositor
5 min read
Integrate GitLab with Jira
Integrating GitLab with Jira brings seamless collaboration between development and project management teams by enabling real-time updates, automated workflows, and efficient issue tracking. With this integration, you can link GitLab commits, branches, and merge requests to Jira issues, enhancing visibility and productivity. Why Integrate GitLab wit
3 min read
GitLab CI/CD Variables
GitLab CI/CD variables allow you to customize your CI/CD pipelines by defining and managing key values used in the build, deployment, and operational processes. These variables act as placeholders for dynamic data, such as environment-specific settings, sensitive information like passwords, or any other data that might change across different stage
4 min read
How To Access The GitLab API?
GitLab is a powerful platform that provides a set of tools for software development, including version control, CI/CD pipelines, and project management. One of its most powerful features is the GitLab API, which allows you to interact with GitLab's functionalities. Whether you want to automate tasks, integrate with other services, or fetch data, th
3 min read
GitLab - SSH Key Setup
Setting up SSH keys for GitLab is an important step for secure and password-less authentication when interacting with your repositories. SSH (Secure Shell) keys provide a secure way of logging into a server and are widely used for automated processes and secure communication between GitLab and your local machine. In this guide, we'll see the proces
4 min read
How to Fix Permission Denied (public key) With Gitlab?
The "Permission denied (public key)" error is a common issue encountered when using Git with GitLab on Windows. This error occurs when GitLab is unable to authenticate your system using SSH (Secure Shell). This usually happens due to a misconfiguration of SSH keys, where either the key is missing, not added to the SSH agent, or not linked to your G
3 min read
Forking Git Repository From GitHub to GitLab
Forking Git Repository from GitHub to GitLab allows you to make changes, experiment, or contribute to a project without affecting the source. This is useful when you want to work on open-source projects or collaborate with teams across different platforms. In this article, we'll see how to Fork Git Repository From GitHub to GitLab, maintaining all
4 min read
How To Pass GitLab Artifacts to Another Stage?
In GitLab CI/CD, artifacts are files or directories generated by a job and shared with subsequent jobs in later stages of the pipeline. Artifacts play an important role in building, testing, and deploying software, allowing data to persist across different stages of the CI/CD process. In this article, we will see how to pass GitLab artifacts to ano
3 min read
How To Migrate From GitLab to GitHub?
Migrating from GitLab to GitHub can be a strategic move for organizations looking to use GitHub’s extensive ecosystem, advanced CI/CD capabilities, or better integration with their existing workflows. Whether you're moving a single repository or an entire organization, this guide will help you navigate the migration process smoothly. Table of Conte
6 min read
Import An Existing Git Project Into GitLab?
Import An Existing Git Project Into GitLab involves setting up a remote GitLab repository and pushing your local project to it. In this article, we will walk you through the steps required to push an existing project to GitLab. Why Import Your Project to GitLab?Pushing your project to GitLab allows you to use GitLab’s powerful version control, coll
4 min read
How To Clone All Projects Of A Group At Once in GitLab?
Cloning all projects of a group at once in GitLab can save you time and effort, especially when dealing with multiple repositories under a single group. GitLab does not provide a direct built-in feature for cloning all group projects at once, but you can achieve this using GitLab's API or scripts. In this article, we will walk you through different
4 min read
How To Change Default Branch in Gitlab?
In GitLab, the default branch is the main working branch for your project, typically set as "main" or "master". This branch serves as the primary point for collaboration, code reviews, and deployments. However, there are times when you may need to change the default branch to reflect naming conventions, feature developments, or team preferences. Ch
3 min read
How To Install GitLab on Windows?
GitLab is a popular DevOps platform that provides a robust solution for source code management, continuous integration/continuous deployment (CI/CD), and other development workflows. While GitLab is typically hosted on Linux servers, it can also be installed on Windows using a virtual machine or Docker. In this article, we will walk you through the
4 min read
How To Check The Version Of GitLab?
GitLab is a web-based DevOps platform that provides development teams with the tools to collaborate, automate repetitive tasks and build faster and better software. In this article, we will explore how to check the version of GitLab. There are multiple ways to check the version of GitLab installed on the system: Table of Content 1. Using the User I
3 min read
How To Create And Manage GitLab Groups?
GitLab is a powerful platform that enables teams to collaborate on code, manage CI/CD pipelines, and track project progress. For organizations with multiple projects, effective project organization and access control are important. GitLab Groups help you organize related projects under a single entity and manage permissions for multiple users acros
6 min read