📌  相关文章
📜  在GitLab上使用Shell Executor在.NET应用程序中实现CI / CD

📅  最后修改于: 2021-05-29 21:42:40             🧑  作者: Mango

Shell Executor是一个非常简单的执行程序,可帮助在安装了GitLab Runner的计算机上本地构建解决方案。但是,它也有助于运行Bash和Windows PowerShell脚本,并且不建议使用Windows Batch。在Shell Executor模式下设置GitLab Runner并开始在其上实现将有一些要求和路径配置。

Shell Executor: Shell Executor是一个非常简单的执行程序,可帮助在安装了GitLab Runner的计算机上本地构建解决方案。在这种情况下,GitLab Runner安装在Linux机器上,因此需要在同一系统中安装所需的软件。

要求:

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.

路径配置:

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设置:请按照以下步骤下载和配置GitLab Runner。

1.下载适用于Windows的GitLab Runner

2.成功下载后,将其保存在目录中(例如:C:\ Tools \ GitLab-Runner),并使用以下命令将其重命名

gitlab-runner.exe

3.在管理模式下打开命令提示符,转到GitLab-Runner目录并使用以下命令检查其状态

gitlab-runner status

4.如果已经运行,请先停止它,然后再向GitLab Runner注册存储库

gitlab-runner.exe stop

5.一旦GitLab Runner成功停止,然后使用以下命令进行存储库注册

gitlab-runner.exe register

6.当您使用GitLab Runner注册存储库时,以下问题必须回答。

  • 输入您的GitLab实例网址:每个组织的网址可能不同,格式类似于http://gitlab.example.com
  • 路径:转到GitLab帐户→选择要向运行器注册的存储库→设置→CI / CD→扩展运行器
  • 输入该跑步者的gitlab-ci令牌:它将是每个项目的唯一令牌,注册时将需要 可以找到  
  • 路径:转到GitLab帐户→选择要向运行器注册的存储库→设置→CI / CD→扩展运行器
  • 输入该跑步者的gitlab-ci描述:输入跑步者名称(任何名称),这将帮助您记住哪个跑步者正在跑步
  • 输入该运行程序的gitlab-ci标签:如果要在yml文件中有特定标签时启动GitLab运行程序,则这是可选的。
  • 输入执行程序:将有几个执行程序的列表,并键入shell(因为GitLab Runner将运行我们的系统)

7.成功注册后,启动GitLab Runner

gitlab-runner start

8.确认GitLab Runner已经注册了各自的存储库,并且启动器已经启动。转到GitLab帐户→选择要向运行程序注册的存储库→设置→CI / CD→扩展运行程序,将有一个绿色圆圈,并且显示消息将为此项目激活运行程序。

注意:如果圆圈为灰色,则表示跑步者尚未启动,请重新启动。

Windows GitLab Runner命令

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:这是.gitlab-ci.yml的格式,并根据要求进行更改

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

代码提交:成功配置后,请进行提交并在GitLab上查看作业状态。转到项目存储库→选择CI / CD菜单