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

📅  最后修改于: 2021-10-24 12:55:44             🧑  作者: Mango

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

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

要求:

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 实例 URL:每个组织可能会有所不同,格式将类似于 http://gitlab.example.com
  • 路径:转到 GitLab 帐户 → 选择要向 runner 注册的存储库 → 设置 → CI/CD → 展开 Runner
  • 输入此跑步者的 gitlab-ci 令牌:它将是每个项目的唯一令牌,注册时需要 并且可以找到  
  • 路径:转到 GitLab 帐户 → 选择要向 runner 注册的存储库 → 设置 → CI/CD → 展开 Runner
  • 输入此跑步者的 gitlab-ci 描述: Put Runner name(any name),这将帮助您记住哪个跑步者正在运行
  • 输入此运行程序的 gitlab-ci 标签:这是可选的,如果您想在 yml 文件中提供特定标签时启动 GitLab 运行程序。
  • 进入执行器:会有几个执行器的列表,然后输入shell(因为GitLab Runner将运行我们的系统)

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

gitlab-runner start

8.验证 GitLab Runner 是否已注册相应的存储库并且 runner 已启动。转到 GitLab 帐户 → 选择要向 runner 注册的存储库 → 设置 → CI/CD → 展开 Runner,将出现一个绿色圆圈可用,并显示消息将为此项目激活 Runners。

注意:如果圆圈为灰色,则表示跑步者尚未开始并重新开始。

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

Code Commit:配置成功后,在GitLab上commit并查看作业状态。转到项目存储库 → 选择 CI/CD 菜单