Skip to content
Related Articles

Related Articles

Implementation of CI/CD in .NET application Using Shell Executor on GitLab
  • Last Updated : 01 Mar, 2021
GeeksforGeeks - Summer Carnival Banner

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  



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 

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :