Use GitLab to do .NET 4.8 CI/CD
Overview
Precondition
You already have a GitLab (GitLab Saas or GitLab self-managed). This document is for .NET 4.X, if you want to build .NET core or .NET 5/6/7/..., you can use docker to run GitLab Runner.
Procedure
Step 1 - Prepare .NET build environment
- First, you need to prepare an environment to build .NET application. The easiest solution is to install Visual Studio. (Please let me know if I can build a .NET build environment without Visual Studio, thanks.)
- But it's strange, MSBuild.exe is not included in PATH after the installation. Please put
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin
into your PATH.
Step 2 - Install Git client
Download and install Git for Windows. The portable version is ok, but because Git Runner is a Windows service, please add git.exe into PATH.
Step 3 - Install GitLab Runner
-
Download GitLab Runner for Windows.
-
Rename
gitlab-runner-windows-amd64.exe
togitlab-runner.exe
. -
Get the token to run GitLab Runner.
-
Generate GitLab Runner configuration file (config.toml) and run the command to register.
1gitlab-runner.exe register --url https://gitlab.com/ --registration-token $REGISTRATION_TOKEN
-
Modify config.toml
- Because Gitlab Runner doesn't support Windows shell after version 13, you need to use PowerShell, you have 2 options:
- If you use Power Shell, please open config.toml and rename 'pwsh' to 'powershell'.
- If you use Power Shell Core, you don't need to do anything.
- If you find your log is garbled (ex: your Windows server is not an English version), please add
chcp 65001
to change the encoding to UTF-8. - If you use a portable Git, you need to modify
$env:Path
in config.toml. (comment line 13 and uncomment line 14)
1listen_address = "[::]:9252" 2concurrent = 4 3check_interval = 0 4 5[session_server] 6 session_timeout = 1800 7[[runners]] 8 name = "Windows Runner 01 (not docker)" 9 url = "https://gitlab.com/" 10 token = "******************" 11 executor = "shell" 12 shell = "powershell" 13 pre_clone_script = " chcp 65001\n $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding\n " 14 # pre_clone_script = " chcp 65001\n $env:Path = \"C:\\Gitlab\\PortableGit\\cmd;$env:Path\"\n\t $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding\n " 15 pre_build_script = " chcp 65001\n " 16 [runners.custom_build_dir] 17 [runners.cache] 18 [runners.cache.s3] 19 [runners.cache.gcs] 20 [runners.cache.azure]
- Because Gitlab Runner doesn't support Windows shell after version 13, you need to use PowerShell, you have 2 options:
-
Register GitLab Runner as a Windows service and start it.
- Run the command
gitlab-runner install
to install as a Windows service. - Run the command
gitlab-runner start
to start the service.
- Run the command
-
remember, if you modify the file, please execute
gitlab-runner.exe restart
to restart GitLab runner. -
If you always see the Credential Helper Selector, please choose "no helper" and "Always use this from now on".
Step 4 - Write .gitlab-ci.yml
- Edit .gitlab-ci.yml, the following is a sample:
1stages: 2 - build 3 - test 4build: 5 stage: build 6 script: 7 - "dotnet build" 8 artifacts: 9 paths: 10 - .\test 11test: 12 stage: test 13 script: 14 - "dotnet test"
Step 5 - Start to build/test/deploy code (on local machine)
- Before you start to run CI/CD on your GitLab, you can try it on your local machine. It's easier to debug in a local environment. Please change the directory to the location of .gitlab-ci.yml and execute this command.
1C:\Gitlab\builds\j5AFD9Qz\0\gis\commission_moi_dtm> gitlab-runner.exe exec shell build 2Runtime platform arch=amd64 os=windows pid=13728 revision=e91107dd version=14.5.2 3Running with gitlab-runner 14.5.2 (e91107dd) 4Preparing the "shell" executor 5Using Shell executor... 6executor not supported job=1 project=0 referee=metrics 7Preparing environment 8Running on GITRUNNER01... 9DEPRECATION: CMD shell is deprecated and will no longer be supported 10Getting source from Git repository 11Fetching changes... 12Initialized empty Git repository in C:/Gitlab/builds/j5AFD9Qz/0/gis/**********/builds/0/project-0/.git/ 13Created fresh repository. 14Checking out 1ff057d4 as HEAD... 15...
Step 6 - Start to test CI/CD (on GitLab)
If everything is ok, you can commit .gitlab-ci.yml and GitLab should run it automatically.
SAST (Static Application Security Testing)
GitLab can check your source code for known vulnerabilities, unfortunately, it only supports Linux containers, and Windows containers are not yet supported. (reference: https://docs.gitlab.com/ee/user/application_security/sast/)
Posts in this Series
- Run SonarQube on Linux Docker with Mono to scan .NET 4.8 Code
- GitLab SonarQube Integration with .NET
- Use Grafana to Manage SonarQube KPI
- Use Grafana to Manage GitLab CI/CD Pipelines
- Use GitLab to do .NET 4.8 CI/CD
- GitLab Checkmarx CI/CD Integration
- GitLab Checkmarx SonarQube Integration