Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
Implementation of CI/CD in .NET application Using Shell Executor on GitLab
Next article icon

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

Last Updated : 30 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 registerRegister the project with GitLab Runner 
gitlab-runner.exe startStart the runner 
gitlab-runner.exe stopStop the runner
gitlab-runner.exe statusTo 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 t0k3nRemove Runner by URL and token 
gitlab-runner unregister --all-runnersUnregister All Runners
gitlab-runner restartThis command stops and then starts the GitLab Runner service
gitlab-runner uninstallThis command stops and uninstalls the GitLab Runner from being run as a service
gitlab-runner execTo see a list of available executors, run
gitlab-runner --helpCheck a recent list of commands by executing
gitlab-runner run --helpCan see the name of the environment variable
gitlab-runner --debugTo run a command in debug mode
gitlab-runner exec shellTo 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 


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

A

amiransarimy
Improve
Article Tags :
  • GBlog
  • Computer Subject
  • Web Technologies
  • C#
  • Git
  • Dot-NET

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 f
    6 min read
    Spring Boot - Continuous Integration Using GitHub Actions
    Let's say we have two or more developers who are merging code into a single repository. There can be issues in merging code from different developers. So the Continuous Integration tools help in solving not just that but many other things like: Run an automated build after successful merging (like b
    3 min read
    How to Set Up a CI Pipeline for Ktor Using GitHub Actions?
    In this article, we'll look at how to use GitHub Actions to create a robust and effective Continuous Integration (CI) pipeline for Ktor applications. Developers have a great foundation to construct high-performance server-side apps thanks to Ktor, a versatile and lightweight Kotlin framework for bui
    6 min read
    Jenkins and GIT Integration using SSH Key
    Integrating Jenkins with Git using SSH keys is a powerful way to automate your Continuous Integration (CI) and Continuous Deployment (CD) pipelines while ensuring secure access to your Git repositories. In this article, we'll guide you through the process of setting up Jenkins to work seamlessly wit
    3 min read
    How to Publish Docker Image to Dockerhub Using Github Actions?
    Pre-requisites: GitHub Actions, Docker Hub Publishing Docker images to Docker Hub using GitHub Actions is a simple and efficient process that can automate your image-building and publishing process. What are Github Actions?GitHub Actions is a CI/CD (Continuous Integration/Continuous Deployment) plat
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences