GitLab CI / CD指南(几乎)绝对入门

或者如何在一个轻松的编码中获得一个美丽的徽章,为您的项目做一个晚上


可能每个开发人员至少拥有一个宠物项目,在某个时候,都会出现关于带有状态,代码覆盖范围以及nuget中的程序包版本的漂亮徽章的主题的痒。在准备编写它的过程中,我在一个项目中获得了如此美丽:


结果


本文将介绍在GitLab中为.Net Core类库项目进行持续集成和交付的基本设置,并在GitLab页面中发布文档,并将收集到的包发送到Azure DevOps中的私人提要。


VS Code用作带有GitLab Workflow扩展的开发环境(用于直接从开发环境验证设置文件)。


简单的介绍


CD-这是您刚刚推送的内容,客户端已经全部掉了吗?

什么是CI / CD,为什么需要轻松使用Google。关于如何在GitLab中配置管道的完整文档也很容易找到在这里,我将简要并尽可能没有缺陷地从鸟瞰的角度描述系统的过程:


  • , merge request , - ,
  • , ,
  • ,
  • — .. ,
  • (.. ) — ( ),
  • , .

, :


  • — , , , , , , .,
  • (stage) — , 1+ ,
  • (job) — . (), , / .

, CI/CD , , , .


: ?
  • 为什么选择GitLab?

, -, GitHub'e , — . , GitHub.


  • Azure DevOps Pipelines?

— . git — , SSH- — , .


:


:


  • GitLab.

:


  • merge request,
  • merge request ,
  • Azure DevOps,
  • GitLab Pages,
  • !11

:


  • 1 —
    • ,
  • 2 —
    • , ,
  • 3 —
    • 1 — nuget- Azure DevOps
    • 2 — xmldoc GitLab Pages

!




  1. Microsoft Azure


  2. Azure DevOps




    1. Azure DevOps-一个新项目

  3. Create , . , ( -> Overview -> Azure DevOps Services)
    服务设置


  4. Atrifacts, Create feed


    1. Include packages from common public sources, nuget
      配置软件包源

  5. Connect to feed, Visual Studio, Machine Setup Source
    来源网址


  6. , Personal Access Token
    个人访问令牌



    1. — 1
    2. (scope) — Packaging/Read & Write
      创建PAT


  7. GitLab, CI/CD
    GitLab CI / CD设置


  8. Variables,


    1. — ( )
    2. — . 9
    3. Mask variable
      GitLab-新变量


.



, CI/CD GitLab .gitlab-ci.yml . , .


, YAML. , , .


docker-, . .Net Core Docker Hub. GitHub , . .Net Core 3.1,


image: mcr.microsoft.com/dotnet/core/sdk:3.1

Microsoft , .


stage'. GitLab 5 :


  • .pre — ,
  • .post — ,
  • build.pre ,
  • test — ,
  • deploy — .

, . , , , . , :


stages:
  - build
  - test
  - deploy

, . , , before_script:


before_script:
  - $PSVersionTable.PSVersion
  - dotnet --version
  - nuget help | select-string Version

, . :


dummy job:
  script:
    - echo ok

, , , , , … — bash: .PSVersion: command not found. WTF?


— runner' ( , GitLab') bash . , , :


dummy job on windows:
  script:
    - echo ok
  tags:
    - windows

! .


, , , test, . , test .


, , :


build job:
  script:
    - echo "building..."
  tags:
    - windows
  stage: build

test and cover job:
  script:
    - echo "running tests and coverage analysis..."
  tags:
    - windows
  stage: test

pack and deploy job:
  script:
    - echo "packing and pushing to nuget..."
  tags:
    - windows
  stage: deploy

pages:
  script:
    - echo "creating docs..."
  tags:
    - windows
  stage: deploy

, .



- , , . , .


: only/except rules. , only/except (merge_request, — , ) ( .. ); rules , , (when GitLab CI/CD).


— merge request, Azure DevOps — merge request , — .


, merge request:


build job:
  # snip
  only:
    - merge_request

merge request :


pack and deploy job:
  # snip
  only:
    - merge_request
    - master

, .


merge request :


  rules:
    - if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"

; rules only/except.



build job , . , , artifacts:


build job:
  # snip
  artifacts:
    paths:
      - path/to/build/artifacts
      - another/path
      - MyCoolLib.*/bin/Release/*

wildcards, .


, — , . .


, ( ) , .



, - , - , ( .net) . , 3 :


dotnet build
dotnet test
dotnet pack

, , - .


  1. , , -c Release
  2. , :
    1. coverlet.msbuild: dotnet add package coverlet.msbuild
    2. /p:CollectCoverage=true
    3. (. )
  3. nuget- : -o .


Coverlet :


Calculating coverage result...
  Generating report 'C:\Users\xxx\source\repos\my-project\myProject.tests\coverage.json'

+-------------+--------+--------+--------+
| Module      | Line   | Branch | Method |
+-------------+--------+--------+--------+
| project 1   | 83,24% | 66,66% | 92,1%  |
+-------------+--------+--------+--------+
| project 2   | 87,5%  | 50%    | 100%   |
+-------------+--------+--------+--------+
| project 3   | 100%   | 83,33% | 100%   |
+-------------+--------+--------+--------+

+---------+--------+--------+--------+
|         | Line   | Branch | Method |
+---------+--------+--------+--------+
| Total   | 84,27% | 65,76% | 92,94% |
+---------+--------+--------+--------+
| Average | 90,24% | 66,66% | 97,36% |
+---------+--------+--------+--------+

GitLab , . coverage; capture-, :


test and cover job:
  # snip
  coverage: /\|\s*Total\s*\|\s*(\d+[,.]\d+%)/

.



— , .


:


  1. nuget (nuget.config), : dotnet new nugetconfig


    : ( ) . , .


  2. : nuget sources add -name <name> -source <url> -username <organization> -password <gitlab variable> -configfile nuget.config -StorePasswordInClearText
    1. name — ,
    2. url — URL " ", . 6
    3. organization — Azure DevOps
    4. gitlab variable — , GitLab (" ", . 11). , $variableName
    5. -StorePasswordInClearText — ( )
    6. -verbosity detailed
  3. : nuget push -source <name> -skipduplicate -apikey <key> *.nupkg
    1. , *.nupkg.
    2. name — .
    3. key — . Azure DevOps Connect to feed az.
    4. -skipduplicate409 Conflict; .

:


  1. , , master, docfx. docfx init . .
    1. ..\public — GitLab public Pages. .. — .
  2. GitLab.
  3. pages ( GitLab Pages):
    1. :
      1. nuget install docfx.console -version 2.51.0 — docfx; .
      2. .\docfx.console.2.51.0\tools\docfx.exe .\docfx_project\docfx.json
    2. artifacts:

pages:
  # snip
  artifacts:
    paths:
      - public

docfx


. — . , , metadata.src:


{
  "metadata": [
    {
      "src": [
        {
          "src": "../",
          "files": [
            "**/*.csproj"
          ],
          "exclude":[
            "*.tests*/**"
          ]
        }
      ],
      // --- snip ---
    },
    // --- snip ---
  ],
  // --- snip ---
}

  1. metadata.src.src: "../"docfx.json, .. .
  2. metadata.src.files: ["**/*.csproj"] — , C# .
  3. metadata.src.exclude: ["*.tests*/**"] — , .tests


, , , , README .


.gitlab-ci.yml
image: mcr.microsoft.com/dotnet/core/sdk:3.1

before_script:
  - $PSVersionTable.PSVersion
  - dotnet --version
  - nuget help | select-string Version

stages:
  - build
  - test
  - deploy

build job:
  stage: build
  script:
    - dotnet build -c Release
  tags:
    - windows
  only:
    - merge_requests
    - master
  artifacts:
    paths:
      - your/path/to/binaries

test and cover job:
  stage: test
  tags:
    - windows
  script:
    - dotnet test -c Release /p:CollectCoverage=true
  coverage: /\|\s*Total\s*\|\s*(\d+[,.]\d+%)/
  only:
    - merge_requests
    - master

pack and deploy job:
  stage: deploy
  tags:
    - windows
  script:
    - dotnet pack -c Release -o .
    - dotnet new nugetconfig
    - nuget sources add -name feedName -source https://pkgs.dev.azure.com/your-organization/_packaging/your-feed/nuget/v3/index.json -username your-organization -password $nugetFeedToken -configfile nuget.config -StorePasswordInClearText
    - nuget push -source feedName -skipduplicate -apikey az *.nupkg
  only:
    - master

pages:
  tags:
    - windows
  stage: deploy
  script:
    - nuget install docfx.console -version 2.51.0
    - $env:path = "$env:path;$($(get-location).Path)"
    - .\docfx.console.2.51.0\tools\docfx.exe .\docfx\docfx.json
  artifacts:
    paths:
      - public
  only:
    - master


!


GitLab CI/CD Gtntral pipelines:


GitLab中的徽章


Shields.io — , .


![  Shields.io](https://img.shields.io/badge/custom-badge-blue)

Shields.io的示例


Azure DevOps Artifacts . Azure DevOps Create badge markdown-:


在Azure DevOps上创建徽章


Azure DevOps徽章信息




, YAML — .


, windows , / ( ). , :


.common_tags: &common_tags
  tags:
    - windows
.common_only: &common_only
  only:
    - merge_requests
    - master

:


build job:
  <<: *common_tags
  <<: *common_only

, .



, — ; Version, . , , , .


— , . , — , .


, release (v./ver./version) <version number> (rev./revision <revision>)?, , dotnet pack. — .


:


#       
$rx = "release\s+(v\.?|ver\.?|version)\s*(?<maj>\d+)(?<min>\.\d+)?(?<rel>\.\d+)?\s*((rev\.?|revision)?\s+(?<rev>[a-zA-Z0-9-_]+))?"
#     ,      GitLab' 
$found = $env:CI_COMMIT_MESSAGE -match $rx
#   - 
if (!$found) { Write-Output "no release info found, aborting"; exit }
#     
$maj = $matches['maj']
$min = $matches['min']
#      -  ,  -  
if ($matches.ContainsKey('rel')) { $rel = $matches['rel'] } else { $rel = ".$(get-date -format "yyyy")" }
#     -    
$bld = $(get-date -format "MMdd")
#       -    
if ($matches.ContainsKey('rev')) { $rev = "-$($matches['rev'])" } else { $rev = '' }
#    
$version = "$maj$min$rel.$bld$rev"
#  
dotnet pack -c Release -o . /p:Version=$version

pack and deploy job .



- , powershell , , , .


当然,与阅读本指南后相比,GitLab CI / CD的功能更加广泛且涉及面更广- 绝对不是甚至还有Auto DevOps,允许


自动检测,构建,测试,部署和监视您的应用程序

现在,我打算配置管道,以使用Pulumi在Azure中部署应用程序并自动检测目标环境,这将在下一篇文章中介绍。


All Articles