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

01 Mar, 2021
Shell Executor is a very simple executor which helps to build the solution locally on the machine, where GitLab Runner is installed. However, it also helps to run Bash and Windows PowerShell scripts and Windows Batch is deprecated. There will be a few requirements and path configuration need to set up GitLab Runner in Shell Executor mode and start implementation on it.
Shell Executor: Shell Executor is a very simple executor that helps to build the solution locally on the machine, where GitLab Runner is installed. In this case, GitLab Runner is installed on Linux Machine, so need to install the required software in the same system.
Requirements:
Software
Description  
This is the first requirements, to commit the changes on GitLab. It is a version control software that tracks the changing set of files.
The work of the GitLab Runner is to pick up and execute the job (when a new commit happens).
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)
Download Nuget.exe and save it, in the same directory where MSBuild is available.
 Path Configuration:
Software/File
Path Description 
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
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. 
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
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
Description 
.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 
Dot-NET
Advanced Computer Subject
C#
Git
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
ML | Monte Carlo Tree Search (MCTS)
Copying Files to and from Docker Containers
Basics of API Testing Using Postman
Markov Decision Process
Getting Started with System Design
Difference between Abstract Class and Interface in C#
C# Dictionary with examples
C# | How to check whether a List contains a specified element
C# | Arrays of Strings
C# | IsNullOrEmpty() Method